急!!!HttpURLConnection请求方式 Connection reset的问题

Code_Noting 2018-09-04 04:44:26
public static String sendPost2(String urlParam, Map<String, Object> params, String charset,String bill_no) {
StringBuffer resultBuffer = null;
// 构建请求参数
StringBuffer sbParams = new StringBuffer();
if (params != null && params.size() > 0) {
for (Entry<String, Object> e : params.entrySet()) {
sbParams.append(e.getKey());
sbParams.append("=");
sbParams.append(e.getValue());
sbParams.append("&");
}
}
HttpURLConnection con = null;
OutputStreamWriter osw = null;
BufferedReader br = null;
// 发送请求
try {
URL url = new URL(urlParam);
con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");// 设定请求的方法为"POST",默认是GET
con.setDoOutput(true);// http正文内,因此需要设为true, 默认情况下是false
con.setDoInput(true);// 设置是否从httpUrlConnection读入,默认情况下是true
con.setUseCaches(false);//Post 请求不能使用缓存

// 设定传送的内容类型是可序列化的java对象
// (如果不设此项,在传送序列化对象时,当WEB服务默认的不是这种类型时可能抛java.io.EOFException)
con.setRequestProperty("accept", "*/*");
// con.setRequestProperty("connection", "Keep-Alive");
con.setRequestProperty("token", "839mkHas0we093s262Wfmsa+fds23ia13dfkRidMNo92Ul01266qasfDn636OoZ99xzZ5550ToMyHoo396332LxvIEbs520CxZ0109wLf");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
if (sbParams != null && sbParams.length() > 0) {
osw = new OutputStreamWriter(con.getOutputStream(), charset);
osw.write(sbParams.substring(0, sbParams.length()-1));
osw.flush();
}
// 读取返回内容
resultBuffer = new StringBuffer();
int contentLength = Integer.parseInt(con.getHeaderField("Content-Length"));
if (contentLength > 0) {
br = new BufferedReader(new InputStreamReader(con.getInputStream(), charset));
String temp;
while ((temp = br.readLine()) != null) {
resultBuffer.append(temp);
}

}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (osw != null) {
try {
osw.close();
} catch (IOException e) {
osw = null;
itfEsbVo.setReturndata(itfEsbVo.getReturndata()+" --OutputStreamWriter.close:"+e.getMessage());
throw new RuntimeException(e);
} finally {
if (con != null) {
con.disconnect();
con = null;
}
}
}
if (br != null) {
try {
br.close();
} catch (IOException e) {
br = null;
itfEsbVo.setReturndata(itfEsbVo.getReturndata()+" --BufferedReader.close:"+e.getMessage());
throw new RuntimeException(e);
} finally {
if (con != null) {
con.disconnect();
con = null;
}
}
}
}
// 调用接口的相应结果
System.out.println(resultBuffer);
return resultBuffer.toString();
}
...全文
3724 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Code_Noting 2021-03-12
  • 打赏
  • 举报
回复
引用 8 楼 longma0305 的回复:
老哥,我最近也遇到了这个问题,系统重启时候接口是正常的,但是过一段时间就Connection reset了,这个间隔时间也不是固定的,请问你解决了嘛
这个问题就是请求放和被请求方的TCP连接方式不一样,统一一下就行了,你可以参考一下这篇文章https://blog.csdn.net/cwclw/article/details/52797131
JoeJosepher 2021-03-12
  • 打赏
  • 举报
回复
老哥,我最近也遇到了这个问题,系统重启时候接口是正常的,但是过一段时间就Connection reset了,这个间隔时间也不是固定的,请问你解决了嘛
johnson_mar 2018-09-06
  • 打赏
  • 举报
回复
引用 4 楼 Code_Noting 的回复:
服务器地址是可以访问的,就是调用的时候会刚开始成功,后面报了Connection reset的错误之后,后面就一直是这个错误了
reset是请求端断开了,和服务端没关系啊
Code_Noting 2018-09-05
  • 打赏
  • 举报
回复
引用 2 楼 qq_35261789 的回复:
用其他工具测试 看服务地址有没有问题呢
服务器地址是可以访问的,就是调用的时候会刚开始成功,后面报了Connection reset的错误之后,后面就一直是这个错误了
Code_Noting 2018-09-05
  • 打赏
  • 举报
回复 1
服务器地址是可以访问的,就是调用的时候会刚开始成功,后面报了Connection reset的错误之后,后面就一直是这个错误了
阳光越来越暖 2018-09-04
  • 打赏
  • 举报
回复
用其他工具测试 看服务地址有没有问题呢
Code_Noting 2018-09-04
  • 打赏
  • 举报
回复
有没有大神讲解一下怎么解决这个问题

67,549

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧