http connection timeout issues

前端 未结 10 1508
感情败类
感情败类 2020-12-13 20:54

I\'m running into an issue when i try to use the HttpClient connecting to a url. The http connection is taking a longer time to timeout, even after i set a connection timeoo

相关标签:
10条回答
  • 2020-12-13 21:10

    I've met the same problem, I guess maybe the Android doesn't support this parameter. In my case i tested all three parameters for the ThreadSafeClientConnManager

    params.setParameter( ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(20) );
    params.setIntParameter( ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 200 );
    params.setLongParameter( ConnManagerPNames.TIMEOUT, 10 );
    ThreadSafeClientConnManager connmgr = new ThreadSafeClientConnManager( params );
    

    The first and second worked fine, but the third didn't work as documented. No exception was thrown and the executing thread was blocked indefinitely when the DefaultHttpClient#execute() was executing.

    see http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e650
    "...One can ensure the connection manager does not block indefinitely in the connection request operation by setting 'http.conn-manager.timeout' to a positive value. If the connection request cannot be serviced within the given time period ConnectionPoolTimeoutException will be thrown."

    0 讨论(0)
  • 2020-12-13 21:10

    From you snippet it's not ultimately clear if you set the timeouts before calling HttpClient.executeMethod(..). So this is my guess.

    0 讨论(0)
  • 2020-12-13 21:12

    Not sure if this helps you, however I think it's worth sharing here. While playing with the timeout stuff I found there is a third timeout type you can assign:

    // the timeout until a connection is established
    private static final int CONNECTION_TIMEOUT = 5000; /* 5 seconds */
    
    // the timeout for waiting for data
    private static final int SOCKET_TIMEOUT = 5000; /* 5 seconds */
    
    // ----------- this is the one I am talking about:
    // the timeout until a ManagedClientConnection is got 
    // from ClientConnectionRequest
    private static final long MCC_TIMEOUT = 5000; /* 5 seconds */
    
    ...
    
    HttpGet httpGet = new HttpGet(url);
    setTimeouts(httpGet.getParams());
    
    ...
    
    private static void setTimeouts(HttpParams params) {
        params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 
            CONNECTION_TIMEOUT);
        params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, SOCKET_TIMEOUT);
        params.setLongParameter(ConnManagerPNames.TIMEOUT, MCC_TIMEOUT);
    }
    
    0 讨论(0)
  • 2020-12-13 21:15

    I've had similar issues with timeouts on android. To resolve it what I did was used the commands to not let the phone idle while I was attempting to establish a connection and during any reads or writes to the connection. Its probably worth a shot in this case as well.

    0 讨论(0)
  • 2020-12-13 21:16

    How are you making the HTTP Connection? This looks like a threading issue. If you are using a background thread, then the thread may be killed along with any timeout registered. The fact that it works the next time tells me that your code will work, if you make the call in a android component and manage the WAKE_LOCK on it yourself. Anyways please post more information about the calling mechanism?

    0 讨论(0)
  • 2020-12-13 21:17
    Thread t=new Thread()
    {
      public void run()
      {
        try 
        {
          Thread.sleep(absolutetimeout);
          httpclient.getConnectionManager().closeExpiredConnections();
          httpclient.getConnectionManager().closeIdleConnections(absolutetimeout,TimeUnit.MILLISECONDS);
          httpclient.getConnectionManager().shutdown();
          log.debug("We shutdown the connection manager!");
        }
        catch(InterruptedException e)
        {}
      }
    };
    
    t.start();
    HttpResponse res= httpclient.execute(httpget);
    t.interrupt();
    

    Is that along the lines of what you all are suggesting?

    I'm not exactly sure how to cancel the execute once it has started, but this seemed to work for me. I'm not sure which of the three lines in the thread did the magic, or if it was some combination of all of them.

    0 讨论(0)
提交回复
热议问题