cn.hutool.http.HttpException: Connection reset 问题解决

在使用Hutool的HTTP工具类进行接口调用时,本地和测试环境正常,但在生产环境中遇到HttpException,具体表现为Connectionreset。排查后发现并非超时或网络问题,而是由于对方服务器的Nginx配置将PUT请求重置,改为POST请求后问题解决。
摘要由CSDN通过智能技术生成

场景

  • 项目A给项目B接口发送数据;
  • 项目使用的是hutool的http工具类;
  • 本地、测试环境均正常,生产出现了错误,明细:
cn.hutool.http.HttpException: Connection reset
	at cn.hutool.http.HttpResponse.init(HttpResponse.java:495)
	at cn.hutool.http.HttpResponse.initWithDisconnect(HttpResponse.java:468)
	at cn.hutool.http.HttpResponse.<init>(HttpResponse.java:81)
	at cn.hutool.http.HttpRequest.doExecute(HttpRequest.java:1130)
	at cn.hutool.http.HttpRequest.execute(HttpRequest.java:1012)
	at cn.hutool.http.HttpRequest.execute(HttpRequest.java:988)
	at com.ylx.review.service.impl.YlxPrePatientServiceImpl.sendYlxStatus(YlxPrePatientServiceImpl.java:142)
	at com.ylx.web.controller.review.YlxPrePatientController.editSave(YlxPrePatientController.java:260)
	at com.ylx.web.controller.review.YlxPrePatientController$$FastClassBySpringCGLIB$$2d13b7af.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:771)
.......
Caused by: java.net.SocketException: Connection reset
	at java.net.SocketInputStream.read(SocketInputStream.java:210)
	at java.net.SocketInputStream.read(SocketInputStream.java:141)
	at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
	at java.io.BufferedInputStream.read1(BufferedInputStream.java:286)
	at java.io.BufferedInputStream.read(BufferedInputStream.java:345)
	at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:704)
	at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:647)
	at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:675)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1569)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
	at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
	at cn.hutool.http.HttpConnection.responseCode(HttpConnection.java:470)
	at cn.hutool.http.HttpResponse.init(HttpResponse.java:492)
	... 104 common frames omitted

我的排查步骤(仅供参考)

  1. 生产环境接口测试没有问题;
  2. 在服务器同样可以ping通生产环境,排除白名单问题;
  3. 使用timeout()设置的超时时间,同样也记录了方法调用时间,均没有问题,并不是超时所导致的异常;

我的解决方法

因为对方服务器接口为put方法,所以我的调用写法为HttpRequest.put(xxxUrl)

这种写法是没什么问题的,但对方服务器的nginx会把接收到的put方法重置,最后统一修改为post请求即可;

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

素人岳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值