hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Brijesh Deo" <b...@SonicWALL.com>
Subject RE: How to release connection with HttpClient 4.0 api ?
Date Wed, 01 Apr 2009 17:15:03 GMT
Hi Oleg,
The link to the reverse proxy is awesome. Thanks for the great help. I
am planning to use that example in some way to see if I can get rid of
the URL rewriting requirement in my current implementation and switch
over to a port based reverse proxying mechanism.

You were right about the Socket Exception. There was indeed a bug which
I have found out and have fixed it.
However, I have noticed one strange behavoir.

If I set up a HttpHost which is to a valid host like shown below and
create a HttpGet with a URI that is invalid, then it always goes to
https://jakarta.apache.org:443.

Is it set to this location by default? Instead of this behavior, how do
I get a proper error (404 or something) when an invalid URI is set in
the HttpGet instance?


HttpHost target = new HttpHost("192.168.145.109", 443, "https");
HttpGet httpget = new HttpGet("/server/some_invalid_URI");
HttpResponse response = httpclient.execute(target, httpget);

Stack trace:

org.apache.http.conn.HttpHostConnectException: Connection to
https://jakarta.apache.org:443 refused
	at
org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection
(DefaultClientConnectionOperator.java:133)
	at
org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:
164)
	at
org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledC
onnAdapter.java:119)
	at
org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultReques
tDirector.java:349)
	at
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClien
t.java:555)
	at
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClien
t.java:509)
	at sample.test.MyLoginModule.login(MyLoginModule.java:91)

Thanks,
Brijesh



-----Original Message-----
From: Oleg Kalnichevski [mailto:olegk@apache.org] 
Sent: Wednesday, April 01, 2009 7:39 PM
To: HttpClient User Discussion
Subject: Re: How to release connection with HttpClient 4.0 api ?

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/s
rc/examples/org/apache/http/examples/ElementalReverseProxy.java

Hope this helps

Oleg




> 
> -----Original Message-----
> From: Oleg Kalnichevski [mailto:olegk@apache.org] 
> 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: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Mime
View raw message