之前調用過別的公司的接口上傳數據,但是遇到個問題就是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
