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.