关于调用接口 Connection reset 问题(使用代理调接口)
之前调用过别的公司的接口上传数据,但是遇到个问题就是Connection reset,查阅了网上的各种资料,说什么的都有,主要意思就是说发布接口和调用接口的某些配置不一样,但是这个怎么说呢,单方面没办法解决,只能是双方协调,但是还不一定能够解决,因为我遇到的情况根本不一样,废话不多说,进入主题。
先说我遇到的情况:
1、公司网络有限制,需要使用代理
2、调用接口访问的是公网地址
3、调用接口一直返回Connection reset
如果你的情况和我类似,建议你试试我的办法。
我先试了一下,该端口连接从设置了代理的浏览器是能够访问的,放在eclipse代码中就是不行,然后写了一个测试类,测试通过url从网上下载图片,结果还是报错Connection reset,这个时候就说明公司网络有问题,因为eclipse 需要设置一下java vm代理:菜单栏 run -> run Configurations -> (右侧)Arguments -> vm arguments ,填入: -Dhttp.proxyHost=代理ip -Dhttp.proxyPort=代理端口 -> apply,然后就会发现,可以从网上下载图片了。
但是这个时候调用接口还是不行的,所以还不是解决办法,接下来使用代理访问端口就完美解决了:
一、GET方式
1 /** 2 * @描述:HttpURLConnection 接口调用 GET方式 3 * @param strUrl 请求地址 4 * @param param 请求参数拼接 5 * @return 请求结果集 6 */ 7 public static String httpURLConectionGET(String strUrl, String param) { 8 StringBuffer sb = new StringBuffer(""); 9 10 BufferedReader br =null; 11 HttpURLConnection connection =null; 12 try { 13 strUrl = strUrl + "?" + param.trim(); 14 URL url = new URL(strUrl); // 把字符串转换为URL请求地址 15 16 // 实例化本地代理对象 17 Proxy proxy= new Proxy(Proxy.Type.HTTP,new InetSocketAddress(proxyIp,proxyPort)); 18 Authenticator.setDefault(new SimpleAuthenticator(proxyUserName,proxyPassword)); 19 20 connection = (HttpURLConnection) url.openConnection(proxy);// 打开连接 21 connection.setConnectTimeout(60000); 22 connection.setDoOutput(true); 23 connection.connect();// 连接会话 24 25 if(connection.getResponseCode()==200){ 26 // 获取输入流 27 br = new BufferedReader(new InputStreamReader(connection.getInputStream())); 28 String line; 29 while ((line = br.readLine()) != null) {// 循环读取流 30 sb.append(line); 31 } 32 }else{ 33 System.out.println("请求失败!HTTP Status:"+connection.getResponseCode()); 34 } 35 36 } catch (Exception e) { 37 e.printStackTrace(); 38 System.out.println("失败!"); 39 }finally{ 40 try { 41 if(br != null){ 42 br.close();// 关闭流 43 } 44 if(connection != null){ 45 connection.disconnect();// 断开连接 46 } 47 } catch (IOException e) { 48 e.printStackTrace(); 49 } 50 51 } 52 return sb.toString(); 53 }
二、POST方式
1 /** 2 * 将字符串发送到指定url地址 3 * @param url 请求地址 4 * @param content 请求内容 5 * @return 请求结果集 6 */ 7 public static String httpPost(String url, String content) { 8 String strResult = ""; 9 CloseableHttpClient httpclient =null; 10 HttpEntity resEntity; 11 CloseableHttpResponse response; 12 try { 13 StringEntity myEntity = new StringEntity(content, "UTF-8"); 14 HttpPost httpPost = new HttpPost(url); 15 16 17 CredentialsProvider credsProvider = new BasicCredentialsProvider(); 18 credsProvider.setCredentials(new AuthScope(proxyIp, proxyPort), 19 new UsernamePasswordCredentials(proxyUserName, proxyPassword)); 20 21 HttpHost proxy = new HttpHost(proxyIp,proxyPort); 22 23 RequestConfig requestConfig = RequestConfig.custom().setProxy(proxy) 24 .setConnectTimeout(120000).setConnectionRequestTimeout(120000) 25 .setSocketTimeout(120000).build(); 26 27 httpclient = HttpClients.custom() 28 .setDefaultCredentialsProvider(credsProvider) 29 .setDefaultRequestConfig(requestConfig) 30 .build(); 31 32 httpPost.addHeader("Content-Type", "text/xml; charset=UTF-8"); 33 httpPost.setEntity(myEntity); 34 response = httpclient.execute(httpPost); 35 36 resEntity = response.getEntity(); 37 if (response.getStatusLine().getStatusCode()==200 && resEntity != null) { 38 strResult = EntityUtils.toString(resEntity, "UTF-8"); 39 } 40 response.close(); 41 } catch (Exception e) { 42 e.printStackTrace(); 43 System.out.println("接口异常:" + e.getMessage()); 44 } finally { 45 if (httpclient != null) { 46 try { 47 httpclient.close(); 48 } catch (IOException e) { 49 e.printStackTrace(); 50 System.out.println("关闭httpclient异常:" + e.getMessage()); 51 } 52 } 53 } 54 return strResult; 55 }
三、POST方式 (接口调用参数拼接)
1 /** 2 * @描述:接口调用参数拼接POST方式 3 * @param strUrl 请求地址 4 * @param param 请求参数拼接 5 * @return 请求结果集 6 */ 7 public static String httpURLConectionParamPOST(String strUrl, String param) { 8 StringBuffer sb = new StringBuffer(""); 9 BufferedReader br =null; 10 HttpURLConnection connection =null; 11 Proxy proxy = null; 12 try { 13 strUrl = strUrl + "?" + param.trim(); 14 URL url = new URL(strUrl); // 把字符串转换为URL请求地址 15 16 proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress(proxyIp,proxyPort)); // 实例化本地代理对象 17 18 Authenticator.setDefault(new SimpleAuthenticator(proxyUserName,proxyPassword)); 19 20 connection = (HttpURLConnection) url.openConnection(proxy);// 打开连接 21 22 23 connection.setConnectTimeout(60000); 24 connection.setDoOutput(true); 25 connection.setUseCaches(false); 26 connection.setRequestMethod("POST"); 27 connection.connect();// 连接会话 28 29 30 if(connection.getResponseCode()==200){ 31 // 获取输入流 32 br = new BufferedReader(new InputStreamReader(connection.getInputStream())); 33 String line; 34 while ((line = br.readLine()) != null) {// 循环读取流 35 sb.append(line); 36 } 37 }else{ 38 System.out.println("请求失败!HTTP Status:"+connection.getResponseCode()); 39 } 40 41 } catch (Exception e) { 42 e.printStackTrace(); 43 System.out.println("请求异常!"); 44 }finally{ 45 try { 46 if(br != null){ 47 br.close();// 关闭流 48 } 49 if(connection != null){ 50 connection.disconnect();// 断开连接 51 } 52 } catch (IOException e) { 53 e.printStackTrace(); 54 System.out.println("连接关闭异常!"); 55 } 56 57 } 58 return sb.toString(); 59 }
* 注:本博文代码参考 https://blog.csdn.net/u012909738/article/details/79298021