国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Http請求超時的一種處理方法

2019-11-14 15:41:59
字體:
來源:轉載
供稿:網友

URLConnection類常見的超時處理就是調用其setConnectTimeout和setReadTimeout方法:

  1. setConnectTimeout:設置連接主機超時(單位:毫秒)  
  2. setReadTimeout:設置從主機讀取數據超時(單位:毫秒)

還有一種比較另類的就是利用java Object對象的wait()和notify()、notifyAll()方法,利用線程的等待和通知機制處理urlConnection的超時,下面直接貼代碼:

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;            }            // 處理HTTP響應的返回狀態信息            int responseCode = reqConnection.getResponseCode();// 響應的代碼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();         // 讀取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();                                //喚醒線程,跳出等待                wakeUp();            }catch(Exception e)            {                            }        }    }    public String getMessage(){        if (!readOK) //超時        {            return "";        }                if (readLen <= 0) {            return "";        }        return new String(data, 0, readLen);    }    public void startUp() {        this.readingThread = new Thread(this);        readingThread.start();    }    //喚醒線程,不再等待    private synchronized void wakeUp() {        notifyAll();    }    public synchronized void waitForData(int timeout)     {        try {            //指定超時時間,等待connection響應            wait(timeout);        }         catch (Exception e)         {        }                    if (!readOK)        {            isStop = true;            try{                //中斷線程                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/");        // 建立URLConnection連接        reqConnection = (HttpURLConnection) reqUrl.openConnection();        HttpConnProcessThread rec = new HttpConnProcessThread(reqConnection, msg, "post" );        rec.startUp();
   // 如果順利連接到并讀完數據,則跳出等待,否則等待超時 rec.waitForData(
2000); String retMessage = rec.getMessage(); }}

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 盐边县| 宝应县| 枝江市| 上思县| 靖江市| 瓦房店市| 叙永县| 广德县| 南昌市| 庆安县| 公主岭市| 仲巴县| 蓬莱市| 车险| 扬州市| 伊金霍洛旗| 巴南区| 丰都县| 祁连县| 梁河县| 合川市| 奉新县| 海晏县| 富阳市| 马公市| 隆回县| 潞城市| 临桂县| 新绛县| 胶州市| 石林| 宕昌县| 随州市| 邹城市| 防城港市| 镇安县| 齐齐哈尔市| 延长县| 公主岭市| 桃江县| 梁平县|