An Http request timeout method.

Source: Internet
Author: User

An Http request timeout method.

The common timeout processing of URLConnection is to call the setConnectTimeout and setReadTimeout methods:

  1. SetConnectTimeout: sets the host connection timeout (unit: milliseconds)
  2. SetReadTimeout: sets the time-out for data reading from the host (unit: milliseconds)

Another alternative method is to use the wait (), Y (), and notifyAll () Methods of java Object to process urlConnection timeout using the thread wait and notification mechanisms, the following code is directly pasted:

Public class HttpConnProcessThread implements Runnable {public boolean isStop = false; public boolean readOK = false; private HttpURLConnection reqConnection = null; public Thread readingThread; private int readLen; private String msg = null; private String reqMethod; private byte [] data;/*** ReadThread constructor comment. */public HttpConnProcessThread (HttpURLConnection reqConnection, String Msg, String reqMethod) {super (); this. reqConnection = reqConnection; this. msg = msg; this. reqMethod = reqMethod;} public void run () {InputStream input = null; OutputStream output = null; try {// reqConnection. connect (); output = reqConnection. getOutputStream (); if ("post ". equalsIgnoreCase (reqMethod) & msg! = Null & msg. length ()> 0) {output. write (msg. getBytes (); output. close (); output = null;} // int responseCode = reqConnection. getResponseCode (); // response code if (responseCode! = 200) System. out. println ("connect failed! ResponseCode = "+ responseCode +" msg = "+ reqConnection. getResponseMessage (); input = reqConnection. getInputStream (); int len; byte [] buf = new byte [2048]; readLen = 0; ByteArrayOutputStream outStream = new ByteArrayOutputStream (); // read inputStream while (! IsStop) {len = input. read (buf); if (len <= 0) {this. readOK = true; input. close (); data = outStream. toByteArray (); break;} outStream. write (buf, 0, len); readLen + = len ;}} catch (IOException ie) {}catch (Exception e) {} finally {try {reqConnection. disconnect (); if (input! = Null) input. close (); if (output! = Null) output. close (); // wake up the thread and wait for wakeUp ();} catch (Exception e) {}} public String getMessage () {if (! ReadOK) // timeout {return "";} if (readLen <= 0) {return "";} return new String (data, 0, readLen );} public void startUp () {this. readingThread = new Thread (this); readingThread. start () ;}// wake up the thread and no longer wait for private synchronized void wakeUp () {policyall () ;} public synchronized void waitForData (int timeout) {try {// specify the timeout time and wait for the connection to respond to wait (timeout);} catch (Exception e) {} if (! ReadOK) {isStop = true; try {// interrupt thread if (readingThread. isAlive () readingThread. interrupt ();} catch (Exception e) {}} public static main (String [] args) {String msg = ""; URL reqUrl = new URL ("http: // 127.0.0.1: 8080/"); // create a URLConnection connection reqConnection = (HttpURLConnection) reqUrl. openConnection (); HttpConnProcessThread rec = new HttpConnProcessThread (reqConnection, msg, "post"); rec. startUp ();
// If the data is successfully connected and read, wait out; otherwise, rec. waitForData (2000); String retMessage = rec. getMessage ();}}

 

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.