An Http request timeout method.
The common timeout processing of URLConnection is to call the setConnectTimeout and setReadTimeout methods:
- SetConnectTimeout: sets the host connection timeout (unit: milliseconds)
- 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 ();}}