Java httpurlconnection Timeout and io exception handling _java

Source: Internet
Author: User
Tags exception handling time interval

When I recently synchronized my data, I found a problem, and I was in the background of synchronizing the data to other departments. To put it simply is to invoke the interface provided by the other side and make an HTTP request call. And then the problem was found in the back. HTTP request, it is possible to request timeout, interrupt failed, IO exception is actually possible, if it is usually open a Web page is OK, not open, you will turn off, or his page to show you information. However, synchronization cannot be done, and once the request fails, the data must be properly synchronized to realize the importance of the issue today.

String Httpurl = "https://www.baidu.com/s?ie=UTF-8&tn=90594569_hao_pg&wd=1"; 
URL url = null; 
HttpURLConnection httpconn = null; 
String result = ""; 
  try {String address = Httpurl; 
  url = new URL (address); 
   
  Httpconn = (httpurlconnection) url.openconnection (); A URL Connection can is used for input and/or output. Set the//DOINPUT flag to true if you are intend to the URL connection for input,//false if not.  
   
  The default is true. URL connections can be used for input or output. 
  If you want to enter with a URL connection, set the Doinput label to True. 
  The inputs and outputs are for the computer, and if you think about it from a programmer's point of view, always confuse it. Input inputs, output outputs, so not from output in read,input write it, in fact, the opposite//input input into the computer, the computer can read, so it is from input read, and output is the computer outputs, through output 
  Write 
  Httpconn.setdooutput (FALSE); So if Setdoinput (false), you cannot read//cannot read from URLConnection if doinput=false when you want to read from the URLConnection (call Setdoinput (True 
   
  )) Httpconn.setdoinput (true); 
  Connection establishment timeout time and read data timeout, httpconn.setconnecttimeout (600000); 
  Httpconn.setreadtimeout (600000); Httpconn.setRequestmethod ("get"); 
  
  Httpconn.connect (); 
  Gets the status code int code = Httpconn.getresponsecode (); 
  SYSTEM.OUT.PRINTLN (code); 
  Read HTTP request response BufferedReader reader = new BufferedReader (New InputStreamReader (Httpconn.getinputstream ())); 
  String Line; 
  while (line = Reader.readline ())!= null) {result = result + line+ ' \ n '; 
  } System.out.println (Result); 
  Turn off IO and connect reader.close (); 
Httpconn.disconnect (); 
catch (Exception e) {log.error (e); 
} finally{if (httpconn!=null) Httpconn.disconnect (); 

 }

The code seems to write nothing, and the place to release the resources is released. The log output has also been hit. In fact, the problem is abnormal handling. The previous thought that something was not synchronized was due to a connection timeout problem. So specially catch Sockettimeoutexception exception, after looking at the log, found that the synchronization interface over there server problem, reported 502 errors. In fact, the exception is an IO exception.

In either case, we have to send the request again after this problem occurs, confirming that the other side is synchronized, based on the results returned by the interface. If the server is temporarily out of the question, we can pause for a short time and then request again.

So the idea for the moment is that because of the real-time nature of the synchronization requirements are not high, then the time interval can be a little longer. Then loop and start the thread at intervals of 5 minutes, until the result is normal.

catch (Exception e) {for 
  (int i = 0; i < 6; i++) { 
    thread t = new Thread () {public void run () {Get ()}}; 
    T.start (); 
    if (Result.equals ("OK")) {break 
      ; 
    } 
    try { 
      thread.sleep (300000); 
    } catch (interruptedexception E2) { 
      log.error (E2); 
    } 
  } 
  Log.error (e); 
} 





The above is the Java httpurlconnection timeout and IO exception processing data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!

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.