关于调用接口 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();
}
* 注:本博文代码参考 https://blog.csdn.net/u012909738/article/details/79298021
关于调用接口 Connection reset 问题(使用代理调接口)的更多相关文章
- jmeter测试文件上传接口报错:connection reset by peer: socket write error
最近在对文件上传接口性能测试时,设置150线程数并发时,总会出现以下错误:connection reset by peer: socket write error 在网上搜索了一下,得到的原因有这些: ...
- Java socket 说明 以及web 出现java.net.SocketException:(Connection reset或者Connectreset by peer:Socket write error)的解释
另外http://www.cnblogs.com/fengmk2/archive/2007/01/15/using-Socket.html可供参考 一Java socket 说明 所谓socket ...
- SSLv3协议、TLSv1.2协议配置不对导致javax.ws.rs.ProcessingException: java.net.SocketException: Connection reset
SSl:Secure Sockets Layer 安全套接层 TLS:Transport Layer Security传输层安全 是为网络通信提供安全及数据完整性的一种安全协议.TLS与SSL在传输层 ...
- connection reset 分析解决(转载)
文章转自:https://my.oschina.net/xionghui/blog/508758;记录下来以便以后复习查阅; 在使用HttpClient调用后台resetful服务时,“Connect ...
- Connection reset原因分析和解决方案
在使用HttpClient调用后台resetful服务时,“Connection reset”是一个比较常见的问题,有同学跟我私信说被这个问题困扰很久了,今天就来分析下,希望能帮到大家.例如我们线上的 ...
- 重新分析connection reset by peer, socket write error错误原因
上次写<connection reset by peer, socket write error问题排查>已经过去大半年,当时把问题"敷衍"过去了. 但是此后每隔一段时 ...
- 短连接时出现connection reset问题的原因
网上摘取的感觉有用的文章,保存下来,让大家学习交流! 在使用HttpClient调用后台resetful服务时,“Connection reset”是一个比较常见的问题,有同学跟我私信说被这个问题困扰 ...
- 转:Connection reset原因分析和解决方案
在使用HttpClient调用后台resetful服务时,“Connection reset”是一个比较常见的问题,有同学跟我私信说被这个问题困扰很久了,今天就来分析下,希望能帮到大家.例如我们线上的 ...
- [Think In Java]基础拾遗1 - 对象初始化、垃圾回收器、继承、组合、代理、接口、抽象类
目录 第一章 对象导论第二章 一切都是对象第三章 操作符第四章 控制执行流程第五章 初始化与清理第六章 访问权限控制第七章 复用类第九章 接口 第一章 对象导论 1. 对象的数据位于何处? 有两种方式 ...
随机推荐
- 吴裕雄 python 机器学习——支持向量机线性回归SVR模型
import numpy as np import matplotlib.pyplot as plt from sklearn import datasets, linear_model,svm fr ...
- 前端——语言——Core JS——《The good part》读书笔记——第九,十章节(Style,Good Features)
第九章节 本章节不再介绍知识点,而是作者在提倡大家培养良好的编码习惯,使用Good parts of JS,避免Bad parts of JS.它是一篇文章. 本文的1-3段阐述应用在开发过程中总会遇 ...
- Good Bye 2019E(点坐标缩小一半以区分奇偶性)
设某个点的坐标为(x,y),根据坐标奇偶性公可分为四类,0表示偶数,1表示奇数,(0,0),(0,1),(1,0),(1,1). 如果所有点的坐标都属于一类,那么它们之间的距离都是4的倍数,无法分辨. ...
- 使用jenkins 实现 .net core项目自动发布到 docker
在Docker内运行Jenkins pull镜像 docker pull jenkins/jenkins:lts Dockerfile FROM jenkins/jenkins:lts USER r ...
- git按需过滤提交文件的一个细节
问题场景 用git管理代码时,作为git小白的我总会遇到一些无法理解的问题,在请教了一些高手后终于解开了疑惑,参考以下场景: 1.比如我们已在电脑1上完成用vs编辑项目.添加.提交到服务器的完整流程, ...
- 安装nodejs时提示Leaving directory
在按照标准的编译命令./configure =>make =>make install 在make的时候发生错误: ../deps/v8/src/base/platform/mutex.h ...
- Ip2Region IP转化地址位置
Ip2Region有中文和数据结构支持,是一个很好的第三方ip转换工具. java: <dependency> <groupId>org.lionsoul</groupI ...
- springboot多模块项目打war包
一.父模块配置 1,指定pakaging:pom 2,指定编译的版本:如下图: <properties> <project.build.sourceEncoding>UTF-8 ...
- iOS 开发之 SDWebImage 底层实现原理分析
SDWebImage 是一个比较流行的用于网络图片缓存的第三方类库.这个类库提供了一个支持缓存的图片下载器.为了方便操作者调用,它提供了很多 UI 组件的类别,例如:UIImageView.UIBut ...
- Ubuntu 安装 uWSGI
uWSGI官方网址: https://pypi.org/project/uWSGI/ 使用如下命令安装: pip install uWSGI 报如下错: Collecting uWSGI Using ...