hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Preygel, Sofya" <Prey...@pbworld.com>
Subject RE: 'Socket closed' exception using
Date Wed, 12 May 2004 22:09:08 GMT
Hi Mike,

Here is the code:

      PostMethod post = new PostMethod(m_URL);
      post.setHttp11(false);
      post.setRequestHeader("Content-Length",
Integer.toString(payload.toString().length()));
      post.setRequestHeader("Content-type", "text/xml;charset=utf-8");
      post.setRequestHeader("Content-type", "text/xml;
charset=ISO-8859-1");
      post.setRequestHeader("SOAPAction",
"http://tempuri.org/action/Agent." + action);
      post.setRequestContentLength(payload.toString().length());
      post.setRequestBody(payload.toString());

      // Log the request (debug mode only)
      try
      {
         m_logger.debug("*** Request: " +
post.getRequestBodyAsString());
      }
      catch (Exception e)
      {
      }

      // Create an instance of HttpClient
      HttpClient httpclient = new HttpClient();

      // Set the connection and socket timeouts
      int requestTimeout = m_socketTimeoutInMillis;
      httpclient.setConnectionTimeout(m_connTimeoutInMillis);
      httpclient.setTimeout(requestTimeout);
      m_logger.info ("Setting HTTP timeouts: connection to " +
m_connTimeoutInMillis + 
                     " millis., socket to " + requestTimeout + "
millis.");
      
      // Execute the request
      int statusCode = -1;
      for (int attempt = 1; statusCode == -1 && attempt <=
m_executeRetries; attempt++)
      {
         try
         {
            statusCode = httpclient.executeMethod(post);
         }
         catch (HttpRecoverableException e)
         {
             m_logger.debug("Recoverable exception occurred while
executing the " +
                           "get data request: " + e.getMessage() + "
(attempt " +
                           attempt + " out of " + m_executeRetries +
").");

            if
(e.getMessage().startsWith("java.net.SocketTimeoutException: Read timed
out"))
            {
               // Increase the timeout for this request
               requestTimeout = requestTimeout * 2;
               m_logger.debug("Increasing the request timeout to " +
requestTimeout +
                              " millis.");
               httpclient.setTimeout(requestTimeout);
            }
         }
         catch (IOException e)
         {
            m_logger.error("I/O error sending the get data request: " +
action + "'. ", e);                         
            post.releaseConnection();
            PBFException ex = new PBFException("I/O error sending the
get data request.");
            throw ex;
         }
      }

      // Check if the request succeeded or we just ran out of retry
attempts
      if (statusCode == -1)
      {
         // Request failed. Log the exception, release the connection
and
         // throw an exception
         m_logger.error("Get data request failed. Unable to recover from
the HTTP " +
                        "recoverable exception after " +
m_executeRetries + " retries.");
         post.releaseConnection();
         PBFException ex =
            new PBFException("Unable to recover from the HTTP
recoverable exception after " +
                             m_executeRetries + " retries.");
         throw ex;
      }

      // Request succeeded, get the response )
      StringBuffer retval = new StringBuffer();
      try
      {
         InputStream is = post.getResponseBodyAsStream();
         if (is != null)
         {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            byte[] buffer = new byte[8192];
            int len;
            while ((len = is.read(buffer)) > 0)
            {
                os.write(buffer, 0, len);
            }
            os.close();

            String response = os.toString();
            retval = new StringBuffer(response);

            // Log the response (debug mode only)
            if (m_logger.isDebugEnabled() && m_logSOAPCommsFlag)
            {
               m_logger.debug("*** Response: " + decode(response));
            }
         }
         else
         {
             // Shall see if this ever happens... if yes, will throw an
exception
             m_logger.error("No data received (getResponseBodyAsStream()
is null).");
         }
      }
      catch (IOException e)
      {
         m_logger.error("I/O error receiving the get data response: " +
action + "'. ", e);
         PBFException ex = new PBFException("I/O error receiving the get
data response.");
         throw ex;
      }
      finally
      {
         // Release the current connection to the connection pool
         post.releaseConnection();         
      }

Here are the request headers:
18:04:53 DEBUG [Thread-17] - >> "POST /WS/AgentLibrary.asp
HTTP/1.0[\r][\n]"
18:04:53 DEBUG [Thread-17] - >> "Content-Length: 868[\r][\n]"
18:04:53 DEBUG [Thread-17] - >> "Content-type: text/xml;
charset=ISO-8859-1[\r][\n]"
18:04:53 DEBUG [Thread-17] - >> "SOAPAction:
http://tempuri.org/action/Agent.Execute[\r][\n]"
18:04:53 DEBUG [Thread-17] - >> "User-Agent: Jakarta
Commons-HttpClient/2.0final[\r][\n]"
18:04:53 DEBUG [Thread-17] - >> "Host: dcgate1[\r][\n]"
18:04:53 DEBUG [Thread-17] - >> "[\r][\n]"

And this are the response headers:
18:06:14 DEBUG [Thread-17] - << "HTTP/1.1 200 OK[\r][\n]"
18:06:14 DEBUG [Thread-17] - << "Server: Microsoft-IIS/5.0[\r][\n]"
18:06:14 DEBUG [Thread-17] - << "Date: Thu, 13 May 2004 10:04:57
GMT[\r][\n]"
18:06:14 DEBUG [Thread-17] - << "Connection: Keep-Alive[\r][\n]"
18:06:14 DEBUG [Thread-17] - << "Content-Length: 45036[\r][\n]"
18:06:14 DEBUG [Thread-17] - << "Content-Type: text/xml;
charset="UTF-8"[\r][\n]"
18:06:14 DEBUG [Thread-17] - << "Set-Cookie:
ASPSESSIONIDACCCCRAR=EDLBKEJANJBFEPANEPIGIOGF; path=/[\r][\n]"
18:06:14 DEBUG [Thread-17] - << "Cache-control: private[\r][\n]"

I wonder if the HTTP version mismatch may have something to do with the
problem...

Thank you for helping out!
Sofya


-----Original Message-----
From: Michael Becke [mailto:becke@u.washington.edu] 
Sent: Wednesday, May 12, 2004 4:37 PM
To: Commons HttpClient Project
Subject: Re: 'Socket closed' exception using


Hi Sofya,

There are a couple of possibilities, but it sounds like you are 
experiencing some connection management issues.  If you could, please 
send some sample code showing how you are using HttpClient, as well as a

wire log <http://jakarta.apache.org/commons/httpclient/logging.html> 
(just the headers please).

Mike

Preygel, Sofya wrote:

> Hello,
> 
> We are trying to use HTTPClient.execute(PostMethod) for sending SOAP 
> requests to Connotate's Web Mining Server (WMS). The requests are sent

> every 1 minute, and in most cases everything works fine. However, 
> every so often (sometimes way too often!) we are getting the 'Socket 
> closed' exception. The socket timeout was initially set to 10 seconds 
> (10000 millis), but even after I set it to 120 seconds (120000 
> millis), we are still seeing it happening quite often. I tried to use 
> both the
> getResponseBodyAsString() and getResponseBodyAsStream() to retrieve
the
> response, but it does not change anything.
> 
> The application is running on Windows 2000 (SP4). I tried both the 
> HTTPClient v2.0 (final) and the latest night build (as of yesterday), 
> but the results are the same. We are using HTTP 1.0 
> (PostMethod.setHttp11(false)), with the application running on the 
> same computer as the WMS.
> 
> I will appreciate any suggestions about what can be done here.
> 
> Thank you,
> Sofya
> 
> Stack trace:
> 08:54:37 DEBUG [Thread-19] - enter PostMethod.renerateRequestBody() 
> 08:54:37 DEBUG [Thread-19] - enter
> EntityEnclosingMethod.renerateRequestBody()
> 08:54:37 DEBUG [Thread-19] - enter getContentCharSet( Header 
> contentheader ) 08:54:37 DEBUG [Thread-19] - enter 
> HeaderElement.parse(String) 08:54:37 DEBUG [Thread-19] - enter 
> HeaderElement.parsePair(char[], int,
> int)
> 08:54:37 DEBUG [Thread-19] - enter HeaderElement.parsePair(char[], 
> int,
> int)
> 08:54:37 DEBUG [Thread-19] - Using buffered request body
> 08:54:37 DEBUG [Thread-19] - enter
> HttpConnection.getRequestOutputStream()
> 08:54:37 DEBUG [Thread-19] - Request body sent
> 08:54:37 DEBUG [Thread-19] - enter
> HttpConnection.flushRequestOutputStream()
> 08:54:37 DEBUG [Thread-19] - enter HttpConnection.close()
> 08:54:37 DEBUG [Thread-19] - enter
> HttpConnection.closeSockedAndStreams()
> 08:54:37 DEBUG [Thread-19] - enter HttpConnection.close()
> 08:54:37 DEBUG [Thread-19] - enter
> HttpConnection.closeSockedAndStreams()
> 08:54:37 DEBUG [Thread-19] - enter HttpConnection.close()
> 08:54:37 DEBUG [Thread-19] - enter
> HttpConnection.closeSockedAndStreams()
> 08:54:37 DEBUG [Thread-19] - enter HttpConnection.releaseConnection()
> 08:54:37 ERROR [Thread-19] - I/O exception executing the get data
> request 'Execute'. 
> java.net.SocketException: Socket closed
> 	at java.net.SocketOutputStream.socketWrite0(Native Method)
> 	at
> java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
> 	at
> java.net.SocketOutputStream.write(SocketOutputStream.java:136)
> 	at
>
org.apache.commons.httpclient.HttpConnection$WrappedOutputStream.write(H
> ttpConnection.java:1368)
> 	at
> java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:69)
> 	at
> java.io.BufferedOutputStream.flush(BufferedOutputStream.java:127)
> 	at
>
org.apache.commons.httpclient.HttpConnection.flushRequestOutputStream(Ht
> tpConnection.java:799)
> 	at
>
org.apache.commons.httpclient.HttpMethodBase.writeRequest(HttpMethodBase
> .java:2324)
> 	at
>
org.apache.commons.httpclient.HttpMethodBase.processRequest(HttpMethodBa
> se.java:2657)
> 	at
>
org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java
> :1093)
> 	at
>
org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:6
> 75)
> 	at
>
org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:5
> 29)
> 	at
>
pbf.travinfo.dcol.CHPDI.AgentLibrary.getSOAPResponse(AgentLibrary.java:6
> 48)
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: 
> commons-httpclient-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
commons-httpclient-dev-help@jakarta.apache.org
> 

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


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