On Wed, Apr 01, 2009 at 04:58:29PM +0530, Brijesh Deo wrote:
> Hi Oleg,
> Thanks for the confirmation.
>
> I am using HttpClient in my application which is a kind of reverse proxy
> inside my tomcat web application where I use HttpClient to perform all
> HTTP GET and POST with the backend web servers and then send back the
> obtained contents from the HttpResponse in the actual
> HttpServletResponse instance to the client (browser). For this I have to
> read from the HttpResponse entity inputstream and write the contents
> into the OutputStream of the HttpServletResponse.
> Any suggestions here on the ClientAbortException resulting from the
> SocketException ?
>
> public void sendStreamToBrowser(HttpServletResponse response) throws
> IOException {
> InputStream streamFromResponse = null;
> // Get hold of the response entity
> HttpEntity entity = this.httpResponse.getEntity();
> if (entity != null) {
> streamFromResponse = entity.getContent();
> }
> OutputStream responseStream = response.getOutputStream();
>
> if (streamFromResponse!= null) {
> byte[] buffer = new byte[1024];
> int read = streamFromResponse.read(buffer);
> while (read > 0) {
> responseStream.write(buffer, 0, read);
> read = streamFromResponse.read(buffer);
> }
>
> }
> responseStream.flush();
> responseStream.close();
> entity.consumeContent(); //called to properly close and
> release all underlying resources
> }
>
> I am getting a Socket exception on the line: responseStream.flush();
>
> The stack trace:
>
> ClientAbortException: java.net.SocketException: Connection reset by
> peer: socket write error
> at
> org.apache.catalina.connector.OutputBuffer.doFlush(OutputBuffer.java:327
> )
> at
> org.apache.catalina.connector.OutputBuffer.flush(OutputBuffer.java:293)
> at
> org.apache.catalina.connector.CoyoteOutputStream.flush(CoyoteOutputStrea
> m.java:97)
> at
> sample.test.MyResponseHandler.sendStreamToBrowser(MyResponseHandler.java
> :123)
>
> Thanks,
> Brijesh
>
That means that the browser already closed the connection most likely
because it was not expecting any more data. This can well be due to a
bug in your code.
You may want to take a look at this sample code that implements an
elemental reverse proxy using HttpCore, the same toolkit HttpClient 4.0
is based on:
http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/httpcore/src/examples/org/apache/http/examples/ElementalReverseProxy.java
Hope this helps
Oleg
>
> -----Original Message-----
> From: Oleg Kalnichevski [mailto:[email protected]]
> Sent: Wednesday, April 01, 2009 4:32 PM
> To: HttpClient User Discussion
> Subject: Re: How to release connection with HttpClient 4.0 api ?
>
> On Wed, 2009-04-01 at 15:24 +0530, Brijesh Deo wrote:
> > Hi,
> >
> >
> >
> > I am trying to port my earlier code which used HttpClient 3.1 to now
> use
> > HttpClient 4.0. I am trying to find out a corresponding method with
> 4.0
> > to gracefully release a connection as it was being done with the 3.1
> > api.
> >
> >
> >
> > With HC 3.1:
> >
> > GetMethod method = new GetMethod("/someURL");
> >
> > //execute the method using the HttpClient instance
> >
> > //and then finally release the connection as shown below.
> >
> > method.releaseConnection();
> >
> >
> >
> >
> >
> > With HC 4.0:
> >
> > HttpGet httpGet = new HttpGet("/someURL");
> >
> > //execute the method using the HttpClient instance
> >
> > HttpResponse response = httpClient.execute(httpget);
> >
> >
> >
> > How to release the connection gracefully?
> >
> > I don't want to call the abort() method on the httpGet instance.
> >
> >
> >
> > Is it sufficient to call the consumeContect() to release a connection
> > gracefully?
> >
> > HttpEntity entity = response.getEntity();
> >
> > If (entity != null) {
> >
> > entity.consumeContent();
> >
> > }
> >
> >
> >
> >
> >
> > Please let me know if there is a better and more efficient way to do
> it.
>
> This is indeed the recommended way. For details, see the 'Ensuring
> release of low level resources' section of the tutorial
>
> http://wiki.apache.org/HttpComponents/HttpClientTutorial
>
>
> > I have also tried to get the underlying inputstream from the response
> > entity and calling a close() on that but it's resulting in a
> > SocketException from other parts in my code.
> >
>
> This should also work. What kind of SocketException are you getting?
>
> Oleg
>
>
>
>
> >
> >
> > Thanks,
> >
> > Brijesh
> >
> >
> >
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]