springboot RestTemplate post 两个tcp数据包 webserver报错
创始人
2024-05-30 10:32:42
0

问题

springboot RestTemplate post请求外部接口时,报错误;

org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://ip:port": 
Unexpected end of file from server; nested exception is java.net.SocketException: Unexpected end of file from server

同样的请求内容使用postman则不报错,使用wireshark抓包后发现,RestTemplate 发送的http虽然未达到tcp MSS(默认的1460字节),但仍然分为两个tcp数据包
在这里插入图片描述

而postman使用了一个tcp数据包,我们知道,应用层是无法控制tcp数据包数量的。
默认的,resttemplate使用java.net.HttpURLConnection发送请求,

The default constructor uses java.net.HttpURLConnection to perform requests. You can switch to a different HTTP library with an implementation of ClientHttpRequestFactory. There is built-in support
for the following:
Apache HttpComponents
Netty
OkHttp

从源码得知,HttpURLConnection在发送http body之前会先发送header并且调用了flush方法,

org.springframework.http.client.SimpleBufferingClientHttpRequest
protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException {
//将http body写入到输出流FileCopyUtils.copy(bufferedOutput, this.connection.getOutputStream());}	

这里的 this.connection,是jdk的内部实现sun.net.www.protocol.http.HttpURLConnection,每次调用getOutputStream()就会调用flush方法

sun.net.www.http.HttpClient
public void writeRequests(MessageHeader head,PosterOutputStream pos) throws IOException {requests = head;//将http header写入到输出流中requests.print(serverOutput);poster = pos;if (poster != null)poster.writeTo(serverOutput);//这一行很重要   serverOutput.flush();}

而将RestTemplate实现换为apache httpclient后,请求接口也是正常的,抓包后发现发送了一个tcp数据包。查源码发现,apache httpclient往流中写入header后并未flush,可能就是这个原因引起的,apache httpclient最终使用的org.apache.http.impl.conn.DefaultClientConnection发送请求。

public void sendRequestHeader(final HttpRequest request) throws HttpException, IOException {if (log.isDebugEnabled()) {log.debug("Sending request: " + request.getRequestLine());}super.sendRequestHeader(request);if (headerLog.isDebugEnabled()) {headerLog.debug(">> " + request.getRequestLine().toString());final Header[] headers = request.getAllHeaders();for (final Header header : headers) {headerLog.debug(">> " + header.toString());}}}
org.apache.http.impl.AbstractHttpClientConnection
public void sendRequestHeader(final HttpRequest request)throws HttpException, IOException {Args.notNull(request, "HTTP request");assertOpen();this.requestWriter.write(request);this.metrics.incrementRequestCount();}
org.apache.http.impl.io.AbstractMessageWriterpublic void write(final T message) throws IOException, HttpException {Args.notNull(message, "HTTP message");//写完http header后,并未调用flushwriteHeadLine(message);for (final HeaderIterator it = message.headerIterator(); it.hasNext(); ) {final Header header = it.nextHeader();this.sessionBuffer.writeLine(lineFormatter.formatHeader(this.lineBuf, header));}this.lineBuf.clear();this.sessionBuffer.writeLine(this.lineBuf);}

回顾flush方法

翻阅API,java.io.OutputStream

/*** Flushes this output stream and forces any buffered output bytes* to be written out. The general contract of flush is* that calling it is an indication that, if any bytes previously* written have been buffered by the implementation of the output* stream, such bytes should immediately be written to their* intended destination.* 

* If the intended destination of this stream is an abstraction provided by* the underlying operating system, for example a file, then flushing the* stream guarantees only that bytes previously written to the stream are* passed to the operating system for writing; it does not guarantee that* they are actually written to a physical device such as a disk drive.*

* The flush method of OutputStream does nothing.** @exception IOException if an I/O error occurs.*/public void flush() throws IOException {}

翻译过来就是强制将输出流中缓存的字节数据写入到目的地;如果目的地是一个文件,那么只能确保缓存字节写入到操作系统不能确保实际写入到磁盘;

解决

推测对方的web server实现有问题,不支持一个post请求两个tcp数据包,最终使用apache httpclient来作为RestTemplate的实现方式。

RestTemplate template = new RestTemplate(new HttpComponentsClientHttpRequestFactory());

相关内容

热门资讯

女朋友总是对我疑神疑鬼,可我把... 女朋友总是对我疑神疑鬼,可我把手机都给她查了,为什么还是不相信我?我觉得这是因为他缺乏安全感,需要时...
为什么选择养老护理这个专业? 为什么选择养老护理这个专业?护理员是养老机构里为长者提供专业、科学照护的一线力量,不仅需要爱心、责任...
终极是什么境界? 终极是什么境界?终极是尽头的意思,在一个领域或范围内已达到极致,无法超越。终极是最高绝顶境界
联户联院平安共建书写群众满意答... 转自:法治日报□ 本报记者   孙立昊洋 马金顺  □ 本报通讯员 周瑞儒  “2024年以来,全市...
走马什么什么 走马什么什么走马什么什么:走马观花走马上任走马看花走马章台走马赴任走马到任走马承受走马之任走马观灯走...
怀旧服巫术之刃值得买吗 怀旧服巫术之刃值得买吗巫师之刃值得买。巫师之刃自带夸眼特效输出不输血喉魔剑,部分玩家甚至认为它是冰法...
情侣之间应当如何相处才好呢? 情侣之间应当如何相处才好呢?情侣之间应该相互平等的相处,互相对对方着想,互相关心,互相爱护,只有用心...
校园感情 校园感情你们见面就和原来一样,别再谈这类话题就好了,如果你真的喜欢他,那你就默默的为他奉献,但是建立...
佛祖在菩提树下修行,参悟出‘平... 佛祖在菩提树下修行,参悟出‘平常心是佛’。请问这个典故的详细内容有一个学僧到法堂请示禅师道:“禅师!...
兔子换了新环境几天不吃不喝不动... 兔子换了新环境几天不吃不喝不动怎么办?有可能是在运输过程中受到惊吓了,让它适应新环境休息一下就可以了...