
正文
关于调用接口 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方式 /**
* @描述:HttpURLConnection 接口调用 GET方式
* @param strUrl 请求地址
* @param param 请求参数拼接
* @return 请求结果集
*/
public static String httpURLConectionGET(String strUrl, String param) {
StringBuffer sb = new StringBuffer(""); BufferedReader br =null;
HttpURLConnection connection =null;
try {
strUrl = strUrl + "?" + param.trim();
URL url = new URL(strUrl); // 把字符串转换为URL请求地址 // 实例化本地代理对象
Proxy proxy= new Proxy(Proxy.Type.HTTP,new InetSocketAddress(proxyIp,proxyPort));
Authenticator.setDefault(new SimpleAuthenticator(proxyUserName,proxyPassword)); connection = (HttpURLConnection) url.openConnection(proxy);// 打开连接
connection.setConnectTimeout(60000);
connection.setDoOutput(true);
connection.connect();// 连接会话 if(connection.getResponseCode()==200){
// 获取输入流
br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = br.readLine()) != null) {// 循环读取流
sb.append(line);
}
}else{
System.out.println("请求失败!HTTP Status:"+connection.getResponseCode());
} } catch (Exception e) {
e.printStackTrace();
System.out.println("失败!");
}finally{
try {
if(br != null){
br.close();// 关闭流
}
if(connection != null){
connection.disconnect();// 断开连接
}
} catch (IOException e) {
e.printStackTrace();
} }
return sb.toString();
}
二、POST方式 /**
* 将字符串发送到指定url地址
* @param url 请求地址
* @param content 请求内容
* @return 请求结果集
*/
public static String httpPost(String url, String content) {
String strResult = "";
CloseableHttpClient httpclient =null;
HttpEntity resEntity;
CloseableHttpResponse response;
try {
StringEntity myEntity = new StringEntity(content, "UTF-8");
HttpPost httpPost = new HttpPost(url); CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(proxyIp, proxyPort),
new UsernamePasswordCredentials(proxyUserName, proxyPassword)); HttpHost proxy = new HttpHost(proxyIp,proxyPort); RequestConfig requestConfig = RequestConfig.custom().setProxy(proxy)
.setConnectTimeout(120000).setConnectionRequestTimeout(120000)
.setSocketTimeout(120000).build(); httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.setDefaultRequestConfig(requestConfig)
.build(); httpPost.addHeader("Content-Type", "text/xml; charset=UTF-8");
httpPost.setEntity(myEntity);
response = httpclient.execute(httpPost); resEntity = response.getEntity();
if (response.getStatusLine().getStatusCode()==200 && resEntity != null) {
strResult = EntityUtils.toString(resEntity, "UTF-8");
}
response.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("接口异常:" + e.getMessage());
} finally {
if (httpclient != null) {
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("关闭httpclient异常:" + e.getMessage());
}
}
}
return strResult;
}
三、POST方式 (接口调用参数拼接) /**
* @描述:接口调用参数拼接POST方式
* @param strUrl 请求地址
* @param param 请求参数拼接
* @return 请求结果集
*/
public static String httpURLConectionParamPOST(String strUrl, String param) {
StringBuffer sb = new StringBuffer("");
BufferedReader br =null;
HttpURLConnection connection =null;
Proxy proxy = null;
try {
strUrl = strUrl + "?" + param.trim();
URL url = new URL(strUrl); // 把字符串转换为URL请求地址 proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress(proxyIp,proxyPort)); // 实例化本地代理对象 Authenticator.setDefault(new SimpleAuthenticator(proxyUserName,proxyPassword)); connection = (HttpURLConnection) url.openConnection(proxy);// 打开连接 connection.setConnectTimeout(60000);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.connect();// 连接会话 if(connection.getResponseCode()==200){
// 获取输入流
br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = br.readLine()) != null) {// 循环读取流
sb.append(line);
}
}else{
System.out.println("请求失败!HTTP Status:"+connection.getResponseCode());
} } catch (Exception e) {
e.printStackTrace();
System.out.println("请求异常!");
}finally{
try {
if(br != null){
br.close();// 关闭流
}
if(connection != null){
connection.disconnect();// 断开连接
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("连接关闭异常!");
} }
return sb.toString();
}
/**
* @描述:HttpURLConnection 接口调用 GET方式
* @param strUrl 请求地址
* @param param 请求参数拼接
* @return 请求结果集
*/
public static String httpURLConectionGET(String strUrl, String param) {
StringBuffer sb = new StringBuffer(""); BufferedReader br =null;
HttpURLConnection connection =null;
try {
strUrl = strUrl + "?" + param.trim();
URL url = new URL(strUrl); // 把字符串转换为URL请求地址 // 实例化本地代理对象
Proxy proxy= new Proxy(Proxy.Type.HTTP,new InetSocketAddress(proxyIp,proxyPort));
Authenticator.setDefault(new SimpleAuthenticator(proxyUserName,proxyPassword)); connection = (HttpURLConnection) url.openConnection(proxy);// 打开连接
connection.setConnectTimeout(60000);
connection.setDoOutput(true);
connection.connect();// 连接会话 if(connection.getResponseCode()==200){
// 获取输入流
br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = br.readLine()) != null) {// 循环读取流
sb.append(line);
}
}else{
System.out.println("请求失败!HTTP Status:"+connection.getResponseCode());
} } catch (Exception e) {
e.printStackTrace();
System.out.println("失败!");
}finally{
try {
if(br != null){
br.close();// 关闭流
}
if(connection != null){
connection.disconnect();// 断开连接
}
} catch (IOException e) {
e.printStackTrace();
} }
return sb.toString();
}
/**
* 将字符串发送到指定url地址
* @param url 请求地址
* @param content 请求内容
* @return 请求结果集
*/
public static String httpPost(String url, String content) {
String strResult = "";
CloseableHttpClient httpclient =null;
HttpEntity resEntity;
CloseableHttpResponse response;
try {
StringEntity myEntity = new StringEntity(content, "UTF-8");
HttpPost httpPost = new HttpPost(url); CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(proxyIp, proxyPort),
new UsernamePasswordCredentials(proxyUserName, proxyPassword)); HttpHost proxy = new HttpHost(proxyIp,proxyPort); RequestConfig requestConfig = RequestConfig.custom().setProxy(proxy)
.setConnectTimeout(120000).setConnectionRequestTimeout(120000)
.setSocketTimeout(120000).build(); httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.setDefaultRequestConfig(requestConfig)
.build(); httpPost.addHeader("Content-Type", "text/xml; charset=UTF-8");
httpPost.setEntity(myEntity);
response = httpclient.execute(httpPost); resEntity = response.getEntity();
if (response.getStatusLine().getStatusCode()==200 && resEntity != null) {
strResult = EntityUtils.toString(resEntity, "UTF-8");
}
response.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("接口异常:" + e.getMessage());
} finally {
if (httpclient != null) {
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("关闭httpclient异常:" + e.getMessage());
}
}
}
return strResult;
}
三、POST方式 (接口调用参数拼接) /**
* @描述:接口调用参数拼接POST方式
* @param strUrl 请求地址
* @param param 请求参数拼接
* @return 请求结果集
*/
public static String httpURLConectionParamPOST(String strUrl, String param) {
StringBuffer sb = new StringBuffer("");
BufferedReader br =null;
HttpURLConnection connection =null;
Proxy proxy = null;
try {
strUrl = strUrl + "?" + param.trim();
URL url = new URL(strUrl); // 把字符串转换为URL请求地址 proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress(proxyIp,proxyPort)); // 实例化本地代理对象 Authenticator.setDefault(new SimpleAuthenticator(proxyUserName,proxyPassword)); connection = (HttpURLConnection) url.openConnection(proxy);// 打开连接 connection.setConnectTimeout(60000);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.connect();// 连接会话 if(connection.getResponseCode()==200){
// 获取输入流
br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = br.readLine()) != null) {// 循环读取流
sb.append(line);
}
}else{
System.out.println("请求失败!HTTP Status:"+connection.getResponseCode());
} } catch (Exception e) {
e.printStackTrace();
System.out.println("请求异常!");
}finally{
try {
if(br != null){
br.close();// 关闭流
}
if(connection != null){
connection.disconnect();// 断开连接
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("连接关闭异常!");
} }
return sb.toString();
}
/**
* @描述:接口调用参数拼接POST方式
* @param strUrl 请求地址
* @param param 请求参数拼接
* @return 请求结果集
*/
public static String httpURLConectionParamPOST(String strUrl, String param) {
StringBuffer sb = new StringBuffer("");
BufferedReader br =null;
HttpURLConnection connection =null;
Proxy proxy = null;
try {
strUrl = strUrl + "?" + param.trim();
URL url = new URL(strUrl); // 把字符串转换为URL请求地址 proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress(proxyIp,proxyPort)); // 实例化本地代理对象 Authenticator.setDefault(new SimpleAuthenticator(proxyUserName,proxyPassword)); connection = (HttpURLConnection) url.openConnection(proxy);// 打开连接 connection.setConnectTimeout(60000);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.connect();// 连接会话 if(connection.getResponseCode()==200){
// 获取输入流
br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = br.readLine()) != null) {// 循环读取流
sb.append(line);
}
}else{
System.out.println("请求失败!HTTP Status:"+connection.getResponseCode());
} } catch (Exception e) {
e.printStackTrace();
System.out.println("请求异常!");
}finally{
try {
if(br != null){
br.close();// 关闭流
}
if(connection != null){
connection.disconnect();// 断开连接
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("连接关闭异常!");
} }
return sb.toString();
}
* 注:本博文代码参考 https://blog.csdn.net/u012909738/article/details/79298021






