1

I encountered a SocketTimeoutException error while using the apache http client, but after adding request header expect: 100-continue, the request was successful. I repeat the request many times and the result is the same.

Here are my http request info:

Post method

Content-Type: application/json

Content-Length: 1277

my service type: spring-based web service

their service type: aps.net-based web service

Below is the error log:

java.net.SocketTimeoutException : Read timed out
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
    at java.net.SocketInputStream.read(SocketInputStream.java:171)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at org.apache.http.impl.io.SessionInputBufferImpl.streamRead(SessionInputBufferImpl.java:137)
    at org.apache.http.impl.io.SessionInputBufferImpl.fillBuffer(SessionInputBufferImpl.java:153)
    at org.apache.http.impl.io.SessionInputBufferImpl.readLine(SessionInputBufferImpl.java:282)
    at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:138)
    at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:56)
    at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:259)
    at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:163)
    at org.apache.http.impl.conn.CPoolProxy.receiveResponseHeader(CPoolProxy.java:165)
    at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:273)
    at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:125)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:272)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)

I know that after adding the request header expect: 100-continue, an http request will be sent before the real http request. But I can't understand why this header can solve my SocketTimeoutException error. Help me, thanks.

yuan
  • 43
  • 7

1 Answers1

0

I've found very interesting your question. First time that I heard about such header Expect ... After read about it, using these links:

  1. Expect Header
  2. Continue-100 Response
  3. Expect Headerpurpose/use
  4. Expect header in ASP net

My conclusion is that aps.net-based web service is expecting such header ... if you don't send it, the service (or in this case, the application server), instead of sending a HTTP Error code, drops the established connection and you end getting a SocketTimeoutException when reading any response (NOTE: this is just speculative. Using other HTTP clients and network sniffers can clarify this more) ...

Carlitos Way
  • 3,279
  • 20
  • 30
  • I tried using postman and curl to send the same http request, and they both succeeded. In addition, I found that postman did not carry 'Expect: 100-continue', and curl carried 'Expect: 100-continue'. So it seems to have nothing to do with 'Expect: 100-continue', I'm still confused. – yuan Nov 05 '21 at 01:23