HttpURLConnection的关闭链接写在finally里防止报错不关闭占用资源

 
HttpURLConnection httpConnection=null;
		try {
			byte[] signBytes;
			respEncode = Base64.encode(text.getBytes("UTF-8"));
			signBytes = "message";
			
			// 建立一个HttpURLConnection
			httpConnection = (HttpURLConnection) new URL(pathUrl).openConnection();
			httpConnection.setRequestMethod("POST");
			httpConnection.setDoOutput(true);
			httpConnection.setDoInput(true);
			httpConnection.setAllowUserInteraction(true);
			httpConnection.setReadTimeout(120000);
			httpConnection.setConnectTimeout(120000);
			httpConnection.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
			httpConnection.connect();
			
			// 发送数据
			OutputStream outputStream = httpConnection.getOutputStream();
			byte[] buffer = text.getBytes("UTF-8"); // 平台生成的XML串
			outputStream.write(buffer);
			outputStream.flush();
			outputStream.close();
			// 接收数据数据
			@SuppressWarnings("resource")
			Scanner scanner = new Scanner(httpConnection.getInputStream(), "UTF-8");
			while (scanner.hasNextLine())
			{
				responseStr+=scanner.nextLine();
			}
			System.out.println(responseStr);
			return responseStr;
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
            if(httpConnection!=null)
			    httpConnection.disconnect();
		}

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用HttpURLConnection转发get请求的Java代码,其中包含了设置请求头和参数的方法: ```java import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.Map; public class HttpUtil { public static String sendGet(String url, Map<String, String> headers, Map<String, String> params) throws Exception { StringBuilder result = new StringBuilder(); String urlNameString = url + "?" + getUrlParams(params); URL realUrl = new URL(urlNameString); HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); for (Map.Entry<String, String> entry : headers.entrySet()) { connection.setRequestProperty(entry.getKey(), entry.getValue()); } connection.connect(); if (connection.getResponseCode() == 200) { BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = in.readLine()) != null) { result.append(line); } in.close(); } connection.disconnect(); return result.toString(); } private static String getUrlParams(Map<String, String> params) { StringBuilder result = new StringBuilder(); for (Map.Entry<String, String> entry : params.entrySet()) { result.append(entry.getKey()).append("=").append(entry.getValue()).append("&"); } String paramsString = result.toString(); if (paramsString.endsWith("&")) { paramsString = paramsString.substring(0, paramsString.length() - 1); } return paramsString; } } ``` 如果你收到了405错误,这意味着你正在尝试使用不允许的HTTP方法。在这种情况下,你需要检查你的请求方法是否正确。如果你使用的是GET方法,那么你需要检查你的URL是否正确,并且你需要确保你没有在请求中包含任何不必要的参数。如果你使用的是POST方法,那么你需要确保你已经正确地设置了请求头和请求体,并且你需要确保你的请求体中包含了所有必要的参数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值