hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oleg Kalnichevski <ol...@apache.org>
Subject Re: Is it possible to get a HttpConnection from HttpClient?
Date Fri, 27 Jun 2008 20:27:25 GMT
On Fri, 2008-06-27 at 10:43 -0700, Yan aka. Yansheng wrote:
> Hi There,
> 
> This is my first post here so I hope this is the right audience.
> 
> My story: I want to replace a chunk of my code for doing I/O streaming to a
> webservice.  I use java.net.HttpURLConnection to get the InputStream and
> OutputStream, it works fine most of the time but it can get a bit flaky and
> I had to write my own retry logic.  So I decided to switch to
> jakartacommons-httpclient to see if things get better.  The problem is I
> don't seem to be able to come up with a good replacement from the httpclient
> package.
>
> Here is the method for obtaining a HttpConnection I wrote, the problem is
> the HostConfiguration returned is not populated.
> 

´╗┐Yansheng

HttpClient 3.x API was never meant to be used this way. You should
either let HttpClient manage connections, or use connection management
components from HttpClient 4.0 

Hope this helps

Oleg

> --------
>     private HttpConnection openConnection(URL url) throws IOException {
>         HttpMethod method = new PostMethod(url.toString());
>         HttpConnection conn = null;
> 
>         // Execute the method.
>         int statusCode = getHttpClient().executeMethod(method);
> 
>         if (statusCode != HttpStatus.SC_OK) {
>             method.releaseConnection();
>             throw new IOException("Unable to connect to Service: " + url);
>         }
>         else {
>             HttpConnectionManager connManager =
> client.getHttpConnectionManager();
>             conn = connManager.getConnection(client.getHostConfiguration());
>         }
> 
>         return conn;
>     }
> -------
> 
> The exception I'm getting:
> 
> Exception in thread "main" java.lang.IllegalArgumentException: host
> parameter is null
>     at
> org.apache.commons.httpclient.HttpConnection.setHost(HttpConnection.java:248)
>     at
> org.apache.commons.httpclient.SimpleHttpConnectionManager.getConnectionWithTimeout(SimpleHttpConnectionManager.java:163)
>     at
> org.apache.commons.httpclient.SimpleHttpConnectionManager.getConnection(SimpleHttpConnectionManager.java:191)
>     at
> org.apache.commons.httpclient.SimpleHttpConnectionManager.getConnection(SimpleHttpConnectionManager.java:108)
>     at
> amazon.wma.storage.timber.TimberServiceImpl.openConnection(MyServiceImpl.java:180)
>     ...
> 
> Thanks for any help!
> Yansheng


---------------------------------------------------------------------
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