hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jefftymc <jefft...@gmail.com>
Subject SocketTimeoutException in HttpBaseMethod.getResponseBodyAsStream().read()
Date Mon, 19 May 2008 09:06:18 GMT
Hi,
    We are using commons-httpclient 2.01 that does send and receive 
simple post-requests && response . Unfortunately we get sometimes the 
following error java.net.SocketTimeoutException: Read timed out

05-19-08 14:40:29:812|Service-0|java.net.SocketTimeoutException: Read 
timed out
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(Unknown Source)
    at java.io.FilterInputStream.read(Unknown Source)
    at java.io.PushbackInputStream.read(Unknown Source)
    at 
org.apache.commons.httpclient.WireLogInputStream.read(WireLogInputStream.java:71)
    at java.io.FilterInputStream.read(Unknown Source)
    at 
org.apache.commons.httpclient.ContentLengthInputStream.read(ContentLengthInputStream.java:135)
    at java.io.FilterInputStream.read(Unknown Source)
    at 
org.apache.commons.httpclient.AutoCloseInputStream.read(AutoCloseInputStream.java:110)
    at com.test.TestHttpClient.execute(TestHttpClient.java:49)
    at com.test.TestThread.run(TestThread.java:34)
    at java.lang.Thread.run(Unknown Source)


   The code is listed below:

    TestHttpClient.java   

    ............
    public void execute() throws IOException
    {     
       String url  =  "http://www.xxx.com/test";
       String request = "args1=abc, args2=abc";
       String result = null;
       PostMethod method = new PostMethod(url);
       InputStream data = new ByteArrayInputStream(request.getBytes());
       DefaultMethodRetryHandler retryhandler = new 
DefaultMethodRetryHandler();
       retryhandler.setRequestSentRetryEnabled(true);
       retryhandler.setRetryCount( 3 ); // retry 3 times

       method.setRequestBody(data);
       method.setFollowRedirects(true);
       method.setMethodRetryHandler(retryhandler);

       MultiThreadedHttpConnectionManager hcm = new 
MultiThreadedHttpConnectionManager();
       hcm.setConnectionStaleCheckingEnabled( true );
       hcm.setMaxConnectionsPerHost(1);
       HttpClient httpClient = new HttpClient( hcm );
       httpClient.setConnectionTimeout( 300000);
       httpClient.setTimeout( 300000);

       try {
            // set the proxy url
            httpClient.getHostConfiguration().setProxy("192.168.0.100", 
8000);

            // execute the method
            int code = httpClient.executeMethod(method);
            if (code == HttpStatus.SC_OK) {
                byte[] responseBody = new byte[10240];             

                InputStream istream = method.getResponseBodyAsStream();
                int pos = 0;
                int read = 0;
                while ((read = istream.read(responseBody, pos,
                        responseBody.length - pos)) >= 0) {
                    pos += read;
                    if (pos >= responseBody.length) {
                        byte[] tmp = new byte[pos + INC_BODY_SIZE];
                        System.arraycopy(responseBody, 0, tmp, 0, pos);
                        responseBody = tmp;
                    }
                }

                result = new String(responseBody, 0, pos);
            }
        } finally {
            method.releaseConnection();
        }
         System.out.println("The result is :" + result);
    }

    We tried to use these in a thread and sometimes the SocketTimeout 
exception was thrown. I added logs and found that the exception was 
thrown in :
    while ((read = istream.read(responseBody, pos, responseBody.length - 
pos)) >= 0) {
                   ........
        }
    }
    Why the InputStream.read() throw a Socket timeout exception?  We may 
not update the HttpClient to the newest version, maybe we can do it 
later. So we try to solve the problem
with 2.01. Any suggestion? Thanks in advance.


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