Timeout is not working for me as well. Code below

***************************************************************
package com.test.hc;

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpConnectionManager;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpVersion;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.SimpleHttpConnectionManager;
import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;

public class HttpGetter { 

        private static final int DEFAULT_SOCKET_TIMEOUT = 100; 
        private HttpClient httpClient; 
        private GetMethod method; 

        public HttpGetter() { 
                super();

                int timeout = DEFAULT_SOCKET_TIMEOUT;
                HttpConnectionManager cm = new SimpleHttpConnectionManager();   
        
                
                HttpConnectionManagerParams connManagerParams = new
HttpConnectionManagerParams();
                
          connManagerParams.setConnectionTimeout(timeout); //D in
HttpConnectionManagerParams     
          cm.setParams(connManagerParams);
          HttpClientParams cliParams = new HttpClientParams(); //D
            cliParams.setConnectionManagerTimeout(timeout); //D
            
            cliParams.setParameter("http.protocol.version", 
HttpVersion.HTTP_1_1);
//SAMEAS 1
            cliParams.setParameter("http.socket.timeout", new Integer(timeout));
//SAMEAS 1
            cliParams.setParameter("http.protocol.content-charset", "UTF-8");
//SAMEAS 1
            
            this.httpClient = new HttpClient(cliParams, cm); //D                
                
        }

        public String getFromURLAsString(String url) throws HttpException, 
        IOException { 
          URI uri = new URI(url, false); 
          method = new GetMethod(uri.getEscapedURI()); 
          method.getParams().setSoTimeout(DEFAULT_SOCKET_TIMEOUT); 
          int result = httpClient.executeMethod(method); 
          return method.getResponseBodyAsString();
        } 
        } 



***************************************************************

Calling code is 

        HttpGetter httpGetter = new HttpGetter(); 
System.out.println("Came here");        
       
System.out.println(httpGetter.getFromURLAsString("http://google.com";)); 
System.out.println("Came out"); 


***************************************************************

OUTPUT WAS : after almost 2 minutes

Error 500--Internal Server Error 
java.net.ConnectException: Connection timed out: connect
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:305)
        at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:171)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:158)
        at java.net.Socket.connect(Socket.java:452)
        at java.net.Socket.connect(Socket.java:402)
        at java.net.Socket.(Socket.java:309)
        at java.net.Socket.(Socket.java:184)
        at
org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:80)
        at
org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:122)
        at
org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:710)
        at
org.apache.commons.httpclient.MultiThreadedHttpConnectionManager$HttpConnectionAdapter.open(MultiThreadedHttpConnectionManager.java:1361)
        at
org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:387)
        at
org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
        at
org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
        at
org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)
        at com.test.hc.HttpGetter.getFromURLAsString(HttpGetter.java:31)
        at jsp_servlet.__i._jspService(__i.java:125)
        at weblogic.servlet.jsp.JspBase.service(JspBase.java:33)
        at
weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:1072)
        at
weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:465)
        at
weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:526)
        at
weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:348)
        at
weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:6981)
        at
weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
        at
weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)
        at
weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:3892)
        at
weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2766)
        at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:224)
        at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:183)
 


***************************************************************



Vitaly Baranovsky 2 wrote:
> 
> Good day!
> 
> Connection timeouts of httpClient in my program doesn't works. I set
> timeout to 10 000 milliseconds, but my program waits for about 65
> seconds before answering.
> 
> Here is stacktrace of my unit test:
> java.net.SocketException: Network is unreachable: connect
>  at java.net.PlainSocketImpl.socketConnect(Native Method)
>  at java.net.PlainSocketImpl.doConnect(Unknown Source)
>  at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
>  at java.net.PlainSocketImpl.connect(Unknown Source)
>  at java.net.SocksSocketImpl.connect(Unknown Source)
>  at java.net.Socket.connect(Unknown Source)
>  at java.net.Socket.connect(Unknown Source)
>  at java.net.Socket.<init>(Unknown Source)
>  at java.net.Socket.<init>(Unknown Source)
>  at
> org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(Unknown
> Source)
>  at
> org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(Unknown
> Source)
>  at org.apache.commons.httpclient.HttpConnection.open(Unknown Source)
>  at
> org.apache.commons.httpclient.MultiThreadedHttpConnectionManager$HttpConnectionAdapter.open(Unknown
> Source)
>  at
> org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(Unknown
> Source)
>  at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(Unknown
> Source)
>  at org.apache.commons.httpclient.HttpClient.executeMethod(Unknown Source)
>  at org.apache.commons.httpclient.HttpClient.executeMethod(Unknown Source)
>  at net.liga.commons.HttpGetter.runGetCore(HttpGetter.java:58)
>  at net.liga.commons.HttpGetter.getFromURLAsString(HttpGetter.java:88)
>  at
> net.liga.commons.test.HttpGetterTest.testGetFromUrlAsString(HttpGetterTest.java:38)
>  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>  at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
>  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
>  at java.lang.reflect.Method.invoke(Unknown Source)
>  at junit.framework.TestCase.runTest(TestCase.java:164)
>  at junit.framework.TestCase.runBare(TestCase.java:130)
>  at junit.framework.TestResult$1.protect(TestResult.java:106)
>  at junit.framework.TestResult.runProtected(TestResult.java:124)
>  at junit.framework.TestResult.run(TestResult.java:109)
>  at junit.framework.TestCase.run(TestCase.java:120)
>  at
> org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
>  at
> org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
>  at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
>  at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
>  at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
>  at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
> 
> 
> 
> Here is the code of my class:
> 
> public class HttpGetter {
> 
> private static final int DEFAULT_SOCKET_TIMEOUT = 10000;
> private HttpClient httpClient;
> private GetMethod method;
> 
> public HttpGetter() {
> super();
> MultiThreadedHttpConnectionManager cm = new
> MultiThreadedHttpConnectionManager();
> cm.getParams().setSoTimeout(DEFAULT_SOCKET_TIMEOUT);
> 
> httpClient = new HttpClient(cm);
> }
> 
> private String getFromURLAsString(String url) throws HttpException,
> IOException {
>   URI uri = new URI(url, false);
>   method = new GetMethod(uri.getEscapedURI());
>   method.getParams().setSoTimeout(DEFAULT_SOCKET_TIMEOUT);
>   int result = httpClient.executeMethod(method);
>   return method.getResponseBodyAsString();;
> }
> }
> 
> 
> 
> Here is the code of my TestCase:
> 
> public class HttpGetterTest extends TestCase {
>       public void testGetFromUrlAsString() throws HttpException, IOException{
>               HttpGetter httpGetter = new HttpGetter();
>               
> System.out.println(httpGetter.getFromURLAsString("http://google.com";));
>       }
> }
> 
> 
> Why timeouts doesn't works?
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
> 
> 
> 
Timeout is not working for me as well. Code belowTimeout is not working for
me as well. Code below
-- 
View this message in context: 
http://www.nabble.com/Why-connection-timeouts-doesn%27t-works--tp12163727p21261092.html
Sent from the HttpClient-User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to