Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8110037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:36:37+00:00 2026-06-06T01:36:37+00:00

I’m reading a web page using HttpClient like this: httpclient = new DefaultHttpClient(); httpget

  • 0

I’m reading a web page using HttpClient like this:

        httpclient = new DefaultHttpClient();
        httpget = new HttpGet("http://google.com");
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream PIS = entity.getContent();
        }  

I need a timeout on the entire job (connecting, waiting & reading – All together or separately).
I tried setting timeout parameters just after httpclient = new DefaultHttpClient(); line:

        int timeout=10;
        httpclient.getParams().setParameter("http.socket.timeout", timeout * 1000);
        httpclient.getParams().setParameter("http.connection.timeout", timeout * 1000);
        httpclient.getParams().setParameter("http.connection-manager.timeout", new Long(timeout * 1000));
        httpclient.getParams().setParameter("http.protocol.head-body-timeout", timeout * 1000);

But it didn’t worked (It timeouts after about 10 times more than the timeout I set).
So I tried a thread to cancel request after a time using httpget.abort() & httpclient.getConnectionManager().shutdown() just after httpget = new HttpGet("http://google.com"); line like this:

        (new Timer()).schedule(new java.util.TimerTask() {
            public void run() {
                httpget.abort();
                httpclient.getConnectionManager().shutdown();
            }
        },10000);

but it had no effect(Timer runs; but those two lines of code do nothing!)!!
I also tried to use this:

URL url = new URL("http://google.com");
URLConnection con = url.openConnection();
con.setConnectTimeout(10000);
con.setReadTimeout(10000);
InputStream PIS = con.getInputStream();

but it was same as my first try (setting timeout parameters in HttpClient)!!

what is the problem?
How can I solve my timeout problem?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T01:36:38+00:00Added an answer on June 6, 2026 at 1:36 am

    Not the solution, but more of an explanation of what is going on.

    What you are doing is correct.

    First of all, if you are using Log4J, make sure you see everything that HttpClient wants to show you:

    log4j.logger.org.apache.http=trace
    

    Then take a look at this class:

    http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/conn/DefaultClientConnectionOperator.html

    This connection operator is multihome network aware and will attempt to retry failed connects against all known IP addresses sequentially until the connect is successful or all known addresses fail to respond. Please note the same CoreConnectionPNames.CONNECTION_TIMEOUT value will be used for each connection attempt, so in the worst case the total elapsed time before timeout can be CONNECTION_TIMEOUT * n where n is the number of IP addresses of the given host.

    That’s what most likely is happening in your case.

    Also, it is better to use constants from this interface HttpConnectionParams:

    SO_TIMEOUT = "http.socket.timeout"
    TCP_NODELAY = "http.tcp.nodelay"
    SOCKET_BUFFER_SIZE = "http.socket.buffer-size"
    SO_LINGER = "http.socket.linger"
    SO_REUSEADDR = "http.socket.reuseaddr"
    CONNECTION_TIMEOUT = "http.connection.timeout"
    STALE_CONNECTION_CHECK = "http.connection.stalecheck"
    MAX_LINE_LENGTH = "http.connection.max-line-length"
    MAX_HEADER_COUNT = "http.connection.max-header-count"
    MIN_CHUNK_LIMIT = "http.connection.min-chunk-limit"
    

    You need only two of them:

    HttpConnectionParams.CONNECTION_TIMEOUT
    HttpConnectionParams.SO_TIMEOUT
    

    So the best way to solve this is to implement a custom ClientConnectionOperator.resolveHostname method that returns only one IP address.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm making a simple page using Google Maps API 3. My first. One marker
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.