In the HTTP protocol, you can set a time-out (connection timeout, response timeout), and set the scenario as follows:
1, the Java API way:
Prior to JDK 1.5, the network timeout can be controlled only by setting two system properties:
String szurl = "http://www.ee2ee.com/";
URL url = new URL (szurl);
HttpURLConnection Urlcon = (httpurlconnection) url.openconnection ();
HttpURLConnection is based on HTTP protocol, and its bottom layer is realized through socket communication. If you do not set a timeout (timeout), in the case of a network exception, the program may die without continuing. The appropriate timeout can be set through the following two statements:
System.setproperty ("sun.net.client.defaultConnectTimeout", timeout millisecond string);
System.setproperty ("Sun.net.client.defaultReadTimeout", timeout number of milliseconds string);
Sun.net.client.defaultConnectTimeout: Timeout for connection to host (in milliseconds)
//sun.net.client.defaultreadtimeout: timeout (in milliseconds) for reading data from the host
In 1.5, you can also use the following two methods of the HttpURLConnection parent class URLConnection:
Setconnecttimeout: Set timeout for connection host (in milliseconds)
Setreadtimeout: Set timeout for reading data from host (in milliseconds)
HttpURLConnection Urlcon = (httpurlconnection) url.openconnection ();
Urlcon.setconnecttimeout (30000);
Urlcon.setreadtimeout (30000);
It should be noted that in the JDK1.4.2 environment, the author found that in the case of setting defaultreadtimeout, if a network timeout occurs, HttpURLConnection will automatically resubmit the request, a request call, Request a Server two times problem (trouble). I think this is a bug in JDK1.4.2. In JDK1.5.0, this problem has been resolved, there is no automatic restart phenomenon. Out "," 30000″);
2, HttpClient:
HttpClient Tool class, each version is different:
3.X is like this.
HttpClient httpclient=new defaulthttpclient ();
4.3 Is this.
Closeablehttpclient httpclient = Httpclients.createdefault ();
HttpClient has three timeout settings:
1) 3. Timeout setting method for X
HttpClient client = new HttpClient ();
Client.setconnectiontimeout (30000);
Client.settimeout (30000);
HttpClient httpclient= new HttpClient ();
Httpclient.gethttpconnectionmanager (). Getparams (). Setconnectiontimeout (5000);
2) 4. Timeout setting for version x (obsolete after 4.3)
HttpClient httpclient=new defaulthttpclient ();
Httpclient.getparams (). Setparameter (coreconnectionpnames.connection_timeout,2000);//Connection time
Httpclient.getparams (). Setparameter (coreconnectionpnames.so_timeout,2000);//Data transfer time
3) 4.3 version timeout setting
Closeablehttpclient httpclient = Httpclients.createdefault ();
HttpGet httpget=new httpget ("http://www.baidu.com");//http Get request (post similarity)
Requestconfig requestconfig = Requestconfig.custom (). SetSocketTimeout (). Setconnecttimeout (). build ();// Set the request and transfer timeout
Httpget.setconfig (Requestconfig);
Httpclient.execute (HttpGet);//Execute request
3. When using curl, there are two timeout times: one is the connection timeout and the other is the maximum allowable time for data transfer.
The connection timeout is specified with the--connect-timeout parameter, and the maximum allowable time for data transfer is specified with the-m parameter.
For example:
Curl--connect-timeout 10-m "Http://XXXXXXX"
If the connection times out, the error prompts such as:
Curl: () connect () timed out!
When the maximum allowable time for data transfer times out, the error prompts are as follows:
Curl: () Operation timed out after-milliseconds with 0 bytes received
4, Ajax:
1 in the old version of XMLHttpRequest, there is no timeout property, so if you use JavaScript native version of AJAX requests, you can only do this by manually setting up settimeout and Cleartimeout ( These two methods are methods of the Window object
function Createxmlhttprequest () {var request = false; if (window.
XMLHttpRequest) {request = new XMLHttpRequest ();
if (request.overridemimetype) {request.overridemimetype (' text/xml '); ' Else if ' window. ActiveXObject) {var versions = [' Microsoft.XMLHTTP ', ' MSXML. XMLHTTP ', ' microsoft.xmlhttp ', ' msxml2.xmlhttp.7.0 ', ' msxml2.xmlhttp.6.0 ', ' msxml2.xml http.5.0 ', ' msxml2.xmlhttp.4.0 ', ' MSXML2. xmlhttp.3.0 ', ' MSXML2.
XMLHTTP ']; for (var i = 0; i < versions.length i++) {try {request = new ActiveXObject (versions
[i]);
if (request) {return request;
The catch (e) {}} is return request;
function Ajax (XMLHTTP, _method, _url, _param, _callback) {if (typeof xmlhttp = = ' undefined ') return;Xmlhttp.onreadystatechange = function () {if (xmlhttp.readystate = = 4 && xmlhttp.status = 200) {
_callback (XMLHTTP);
}
};
Xmlhttp.open (_method, _url, true); if (_method = = "POST") {Xmlhttp.setrequestheader ("Content-type", "Application/x-www-form-urle
Ncoded ");
Xmlhttp.setrequestheader ("Content-length", _param.length);
Xmlhttp.send (_param);
else {xmlhttp.send (null);
}//Use example var xmlhttp = Createxmlhttprequest (); var T1;
Used for timeout processing function Adduserok (request) {//alert (Request.responsetext);
if (T1) cleartimeout (t1);
function Connecttofail () {if (XMLHTTP) xmlhttp.abort ();
Alert (' Time Out '); } if (XMLHTTP) {Ajax (XMLHTTP, "POST", "http://10.1.2.187/adduser.cgi", "Act=do&user=abc", Addus
Erok);
T1 = settimeout (Connecttofail, 30000); else {alert ("INIT XMLHttpRequest fail "); }
2 The above manual processing Ajax for the initial stage, if using the jquery framework, then in jquery can set the Timeout timeout property:
$.ajax ({
URL: ', //requested URL
timeout:1000,//timeout setting, unit millisecond
type: ' Get ', //Request mode, GET or post
data : {}, //request-passed parameters, JSON format
dataType: ' json ',//returned data format
success:function (/////Request successful callback function
alert ("Success");
},
complete:function (xmlhttprequest,status) {//request completed after the final execution
of the parameter if (status== ' timeout ') {//timeout, status and S The situation of Uccess,error equivalence
ajaxtimeouttest.abort (); Alert ("timeout");
}
}
);
3 XMLHttpRequest Level2 New version, the timeout attribute is added and there is a timeout event:
Xhr.timeout = 3000;
Xhr.ontimeout = function (event) {
alert (' Request timed out. ');
}