Home > Back-end >  HttpUrlConnection how connection timeout repeat request 3 times and then determine failure
HttpUrlConnection how connection timeout repeat request 3 times and then determine failure

Time:11-27

HttpUrlConnection how connection timeout repeat request 3 times and then determine failure, capture the timeout exception, how to deal with in the catch to repeat send stipulate number

CodePudding user response:

For or a while loop in the try catch, without exception, direct break cycle; If abnormal, in judging whether the catch timeout exception, so accumulate a try, if the try number greater than 3 times, then break cycle, or recycle the try catch

CodePudding user response:

Thank you for your reply, actually I wanted to put our heads together and see what other way

CodePudding user response:

Recursive method to add a retry count parameter

CodePudding user response:

Where is this retry parameters defined, will there be thread safety problem?

CodePudding user response:

Reading the double chromosphere API code, the interface is not available now, try the code, but the level of available

 
Public static String getDCBallHistory () {

InputStream inputStream=null;
InputStreamReader InputStreamReader=null;
BufferedReader reader=null;
StringBuffer resultBuffer=new StringBuffer ();
String tempLine=null;

boolean flag=false;
int count=0;

//System. SetProperty (" sun.net.client.defaultConnectTimeout ", "2500");
//System. SetProperty (" sun.net.client.defaultReadTimeout ", "3500");

While (the count & lt; 3) {

Try {

URL apiUrl=new URL (" http://f.apiplus.net/ssq-50.json ");

HttpURLConnection HttpURLConnection.=(HttpURLConnection) apiUrl openConnection ();

HttpURLConnection. SetConnectTimeout (5000);
HttpURLConnection. SetReadTimeout (5000);
HttpURLConnection. SetRequestProperty (" Accept - Charset ", "utf-8");
InputStream=httpURLConnection. GetInputStream ();
InputStreamReader=new inputStreamReader (inputStream);
Reader=new BufferedReader (inputStreamReader);
If (httpURLConnection. GetResponseCode () & gt; {
=300)Throw new Exception (
"HTTP Request is not success, the Response code is" + httpURLConnection. GetResponseCode ());
}

While ((tempLine=reader. ReadLine ())!=null) {
ResultBuffer. Append (tempLine);
}

} the catch (Exception e) {
//e.p rintStackTrace ();
flag=true;
Direct System. Out. Println (" get "+" times "+ count);
} the finally {

If (reader!=null) {
Try {
Reader. The close ();
} the catch (IOException e) {
e.printStackTrace();
}
}

If (inputStreamReader!=null) {
Try {
InputStreamReader. Close ();
} the catch (IOException e) {
e.printStackTrace();
}
}

If (inputStream!=null) {
Try {
InputStream. Close ();
} the catch (IOException e) {
e.printStackTrace();
}
}

}
If (flag) {
count++;
} else {
break;
}

}

Return resultBuffer. ToString ();
}


CodePudding user response:

How can I get a point

CodePudding user response:

refer to 6th floor tianfang response:
reading the double chromosphere API code, the interface is not available now, try the code, but the level of available

 
Public static String getDCBallHistory () {

InputStream inputStream=null;
InputStreamReader InputStreamReader=null;
BufferedReader reader=null;
StringBuffer resultBuffer=new StringBuffer ();
String tempLine=null;

boolean flag=false;
int count=0;

//System. SetProperty (" sun.net.client.defaultConnectTimeout ", "2500");
//System. SetProperty (" sun.net.client.defaultReadTimeout ", "3500");

While (the count & lt; 3) {

Try {

URL apiUrl=new URL (" http://f.apiplus.net/ssq-50.json ");

HttpURLConnection HttpURLConnection.=(HttpURLConnection) apiUrl openConnection ();

HttpURLConnection. SetConnectTimeout (5000);
HttpURLConnection. SetReadTimeout (5000);
HttpURLConnection. SetRequestProperty (" Accept - Charset ", "utf-8");
InputStream=httpURLConnection. GetInputStream ();
InputStreamReader=new inputStreamReader (inputStream);
Reader=new BufferedReader (inputStreamReader);
If (httpURLConnection. GetResponseCode () & gt; {
=300)Throw new Exception (
"HTTP Request is not success, the Response code is" + httpURLConnection. GetResponseCode ());
}

While ((tempLine=reader. ReadLine ())!=null) {
ResultBuffer. Append (tempLine);
}

} the catch (Exception e) {
//e.p rintStackTrace ();
flag=true;
Direct System. Out. Println (" get "+" times "+ count);
} the finally {

If (reader!=null) {
Try {
Reader. The close ();
} the catch (IOException e) {
e.printStackTrace();
}
}

If (inputStreamReader!=null) {
Try {
InputStreamReader. Close ();
} the catch (IOException e) {
e.printStackTrace();
}
}

If (inputStream!=null) {
Try {
InputStream. Close ();
} the catch (IOException e) {
e.printStackTrace();
}
}

}
If (flag) {
count++;
} else {
break;
}

}

Return resultBuffer. ToString ();
}


nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related