hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ortwin Glück <ortwin.glu...@nose.ch>
Subject Re: HttpClient problems
Date Fri, 28 May 2004 07:11:37 GMT
Some comments on the posted code:

1. You should execute releaseConnection in a finally block, to ensure 
the connection is returned to the pool in any case:

PostMethod post = null;
try {
   post = new PostMethod(urlAddress);
   /* ... */
} finally {
   if (post != null) post.releaseConnection();
}

Otherwise you the connection pool might run out of connections if 
exceptions occur.

2. You are setting the Authorization header manually. You could use 
HttpClient's built-in authentication logic instead.

3. You are setting the encoding in the Content-Type header to 
ISO-8859-1, but you do not specify any encoding in xmlStr.getBytes(). 
This assumes that the default platform encoding be ISO-8859-1, which is 
true for Windows in certain locales but not for most other platforms. 
Furthermore this assumes your <?xml ?> Header uses 
encoding="ISO-8859-1", which I can not verify from the code. I suggest 
you get the XML Stream (not a String) from a DOM directly and specify 
the same encoding as in the Content-Type header.


paul wrote:
> Ortwin,
> 
>    It seems from the wire logs I gathered, that's the normal behaviour.
> 
>    I am creating a new HttpMethod object for every send.
> 
>    Only when I receive the response, then releaseConnection was called.
> 
>    Here's the code :
> =================================
> //convert trade to xml
>         String xmlStr = convertTradeToXMLString(trade, some1id, someid);
> 
>         PostMethod post = new PostMethod(urlAddress);
>         post.setRequestHeader("Content-type", "text/xml; 
> charset=ISO-8859-1");
>         post.setRequestHeader ("Authorization", "Basic " + encoding);
>         post.setRequestHeader("HTTP-Version", "HTTP/1.1");
>         post.setRequestHeader ("Connection", "Keep-Alive");
>                xmlInBytes =new ByteArrayInputStream(xmlStr.getBytes());
>         post.setRequestBody(xmlInBytes);
>                if (xmlStr.length() < Integer.MAX_VALUE) {
>            post.setRequestContentLength(xmlStr.length());
>         }
>         else {
>            
> post.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED);
>         }
>                // Execute request
> 
>         toConsole("B4 sending xml, 
> connMgr.getConnectionsInUse()="+connMgr.getConnectionsInUse());
>         toConsole(">>>>>>>> Send xml to somewhere @ "+ new 
> java.sql.Timestamp(System.currentTimeMillis()));
>         int result = httpClient.executeMethod(post);
>                //System.out.println("Response body: ");
>         MQClientConstants.toConsole(">>>>>>>> Reply from fxlink
received 
> @ "+ new java.sql.Timestamp(System.currentTimeMillis()));
>         String xml = post.getResponseBodyAsString();
>         post.releaseConnection();
> 
> =================================
> 
> Httpclient was created like this :
> ==================================
> connMgr = new MultiThreadedHttpConnectionManager();
>      connMgr.setMaxConnectionsPerHost( MAXHOSTCONNECTIONS );//20 
> connections
>      MQClientConstants.toConsole("MultiThreadedHttpConnectionManager 
> setMaxConnectionsPerHost = "+MAXHOSTCONNECTIONS);
> 
>      connMgr.setConnectionStaleCheckingEnabled( true );
>      MQClientConstants.toConsole("MultiThreadedHttpConnectionManager 
> setConnectionStaleCheckingEnabled = "+true);
>          httpClient = new HttpClient(connMgr);
>      httpClient.setTimeout(TIMEOUT);//5 secs
>      httpClient.setStrictMode(true);
> ==================================
> 
> Paul


-- 
  _________________________________________________________________
  NOSE applied intelligence ag

  ortwin glück                      [www]      http://www.nose.ch
  software engineer
  hardturmstrasse 171               [pgp id]           0x81CF3416
  8005 zürich                       [office]      +41-1-277 57 35
  switzerland                       [fax]         +41-1-277 57 12

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-httpclient-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-httpclient-dev-help@jakarta.apache.org


Mime
View raw message