On Wed, 2018-10-10 at 22:41 +0500, Adam Retter wrote: > I am using the Maven httpclient 4.5.6 artifact and trying to make a > PUT > request to a server. The server for some URLs requires > authentication, it > is impossible to know which URLs do and don't ahead of time. I only > want to > send the credentials if the server replies 401 Unauthorized. > > I am trying to do a HTTP PUT to the server. If the server replies > HTTP 401 > Unauthorized, then I plan to retry with the authorization > credentials. > > I have found rather a strange issue, and I am not sure if the fault > is with > the Apache HTTP Client or the server. However, I get different > behaviour in > the Apache HTTP Client depending on how fast I can stream the data to > the > server. > > What I am seeing looks like this: > > 1. The Client opens a HTTP connection to the server > 2. The Client starts sending data to the server > 3. The Server starts receiving the data from the client, and > determines > that the client is not authorized. > 4. The Server responds HTTP 401 Unauthorized to the client > 5. The server closes the connection. > > At the client end when I only have a small amount of data to PUT, or > use a > fast mechanism to write the data, all is fine and Apache HTTP Client > gives > me the HTTP Response 401 from the server. All is good! > > However, at the client end when I have a lot of data to PUT, or use a > slow > mechanism to write the data, the Apache HTTP Client is still writting > data > to the server when (5) occurs. The operating system then raises a > "Broken > pipe (Write failed)" error, which Java relays to the Apache HTTP > Client as > a SocketException. I then see errors like the following in my > application: >
This issue is known as out of sequence response. Such behavior is valid according to the HTTP spec but is difficult to accommodate for when using the classic (blocking) i/o model as blocking connections are not able to write data and react to input events at the same time. You have two options 1. Turn on 'expect-continue' handshake. It is intended precisely for such situations. 2. Migrate to a non-blocking HTTP client such as Apache HttpAsyncClient 4.1 or 5.0 that can handle out of sequence responses. Oleg --------------------------------------------------------------------- To unsubscribe, e-mail: httpclient-users-unsubscr...@hc.apache.org For additional commands, e-mail: httpclient-users-h...@hc.apache.org