Return-Path: X-Original-To: apmail-hc-httpclient-users-archive@www.apache.org Delivered-To: apmail-hc-httpclient-users-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 540E3D8BF for ; Thu, 1 Nov 2012 10:01:53 +0000 (UTC) Received: (qmail 94625 invoked by uid 500); 1 Nov 2012 10:01:52 -0000 Delivered-To: apmail-hc-httpclient-users-archive@hc.apache.org Received: (qmail 94405 invoked by uid 500); 1 Nov 2012 10:01:50 -0000 Mailing-List: contact httpclient-users-help@hc.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "HttpClient User Discussion" Delivered-To: mailing list httpclient-users@hc.apache.org Received: (qmail 94291 invoked by uid 99); 1 Nov 2012 10:01:45 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 01 Nov 2012 10:01:45 +0000 X-ASF-Spam-Status: No, hits=0.7 required=5.0 tests=SPF_NEUTRAL X-Spam-Check-By: apache.org Received-SPF: neutral (athena.apache.org: local policy) Received: from [217.150.250.48] (HELO kalnich.nine.ch) (217.150.250.48) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 01 Nov 2012 10:01:39 +0000 Received: by kalnich.nine.ch (Postfix, from userid 1000) id CF185B801FB; Thu, 1 Nov 2012 11:01:18 +0100 (CET) Date: Thu, 1 Nov 2012 11:01:18 +0100 From: Oleg Kalnichevski To: HttpClient User Discussion Subject: Re: Unable to get HttpStatus.SC_REQUEST_TOO_LONG status successfully Message-ID: <20121101100118.GA26761@kalnich.nine.ch> Mail-Followup-To: HttpClient User Discussion References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.20 (2009-06-14) X-Virus-Checked: Checked by ClamAV on apache.org On Wed, Oct 31, 2012 at 09:19:19AM -0400, Sachin Nikumbh wrote: > Hi, > > I am using HttpClient (version 4.2.2) to communicate with a server using > POST requests. The server can be configured to set request size limit. Once > the request content exceeds this limit, it sends HTTP 413 response status > and closes the connection. > > In my simple application written using Apache HttpClient, I send a POST > request with request content too large for the server using something like > following: > > *************************************************** > DefaultHttpClient httpClient = new DefaultHttpClient(); > ... > ... > HttpPost postReq = new HttpPost(url); > .... > HttpResponse response = httpClient.execute(postReq); > *************************************************** > > I am expecting the response.getStatusLine().getStatusCode() to > return HttpStatus.SC_REQUEST_TOO_LONG. But instead, I am getting a > SocketException with message : > > *************************************************** > Connection reset by peer: socket write error > *************************************************** > > I have used WireShark to see what's being sent and received. Wireshark > shows the response with 413 status from server the moment client exceeds > the request size limit. But it looks like HttpClient is ignoring it and > still continues to send the remaining request. > > Is there something that I am missing or is this not supported? > > Any help will be greatly appreciated, > > Thanks > Sachin This problem is caused by the limitation of Java blocking I/O. There is no efficient way of reading and writing to the same network socket using a single execution thread. Therefore, HttpClient cannot read incoming data until the entire request is fully written out. Given that the server sends a 413 status out of sequence and immediately closes the connection while HttpClient is still busy writing out request data, request execution fails with a connection reset i/o error rather than HTTP 413 status. Your only option would be switching to a NIO based HTTP client such Apache HttpAsyncClient [1] which are better equipped to deal wi8th out of sequence I/O events. Oleg [1] http://hc.apache.org/httpcomponents-asyncclient-dev/index.html --------------------------------------------------------------------- To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org For additional commands, e-mail: httpclient-users-help@hc.apache.org