Java httpurlconnection timeout

Source: Internet
Author: User

In Java, httpurlconnection can be used to request web resources.
The httpurlconnection object cannot be constructed directly. You need to use URL. openconnection () to obtain the httpurlconnection object.CodeAs follows:
String szurl = "http://www.ee2ee.com /";
URL url = new URL (szurl );
Httpurlconnection urlcon = (httpurlconnection) URL. openconnection ();

Httpurlconnection is based on the HTTP protocol and is implemented through socket communication at the underlying layer. If the timeout value is not set, a network exception may causeProgramAnd does not proceed. You can use the following two statements to set the corresponding Timeout:
System. setproperty ("sun.net. Client. defaulttimetimeout", timeout string in milliseconds );
System. setproperty ("sun.net. Client. defaultreadtimeout", timeout string in milliseconds );

Among them: sun.net. Client. defaultconnecttimeout: the timeout time for connecting to the host (unit: milliseconds)
Sun.net. Client. defaultreadtimeout: the timeout time for reading data from the host (unit: milliseconds)

For example:
System. setproperty ("sun.net. Client. defaulttimetimeout", "30000 ″);
System. setproperty ("sun.net. Client. defaultreadtime

In Java, httpurlconnection can be used to request web resources.
The httpurlconnection object cannot be constructed directly. You need to use URL. openconnection () to obtain the httpurlconnection object. The sample code is as follows:
String szurl = "http://www.ee2ee.com /";
URL url = new URL (szurl );
Httpurlconnection urlcon = (httpurlconnection) URL. openconnection ();

Httpurlconnection is based on the HTTP protocol and is implemented through socket communication at the underlying layer. If the timeout value is not set, the program may be frozen and will not continue to be executed due to network exceptions. You can use the following two statements to set the corresponding Timeout:
System. setproperty ("sun.net. Client. defaulttimetimeout", timeout string in milliseconds );
System. setproperty ("sun.net. Client. defaultreadtimeout", timeout string in milliseconds );

Among them: sun.net. Client. defaultconnecttimeout: the timeout time for connecting to the host (unit: milliseconds)
Sun.net. Client. defaultreadtimeout: the timeout time for reading data from the host (unit: milliseconds)

For example:

System. setproperty ("sun.net. Client. defaulttimetimeout", "30000 ″);
System. setproperty ("sun.net. Client. defaultreadtimeout", "30000 ″);

In versions earlier than JDK 1.5, only the two system attributes can be set to control network timeout. In 1.5, you can also use the following two methods of the httpurlconnection parent class urlconnection:
Setconnecttimeout: sets the host connection timeout (unit: milliseconds)
Setreadtimeout: sets the time-out for data reading from the host (unit: milliseconds)

For example:
Httpurlconnection urlcon = (httpurlconnection) URL. openconnection ();
Urlcon. setconnecttimeout (30000 );
Urlcon. setreadtimeout (30000 );

Note that, in the jdk1.4.2 environment, I found that when defaultreadtimeout is set, if the network times out, httpurlconnection will automatically submit a request again, and a request call will appear, trouble ). I think this is a bug in jdk1.4.2. In jdk1.5.0, this problem has been solved and automatic resend does not exist. Out, 30000 ″);

In versions earlier than JDK 1.5, only the two system attributes can be set to control network timeout. In 1.5, you can also use the following two methods of the httpurlconnection parent class urlconnection:
Setconnecttimeout: sets the host connection timeout (unit: milliseconds)
Setreadtimeout: sets the time-out for data reading from the host (unit: milliseconds)

For example:
Httpurlconnection urlcon = (httpurlconnection) URL. openconnection ();
Urlcon. setconnecttimeout (30000 );
Urlcon. setreadtimeout (30000 );

note that in the jdk1.4.2 environment, the author finds that if defaultreadtimeout is set, httpurlconnection will automatically submit a request again if the network times out, A single request call occurs and the server is requested twice (trouble ). I think this is a bug in jdk1.4.2. In jdk1.5.0, this problem has been solved and automatic resend does not exist.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.