java.net.SocketTimeoutException: Read timed out

來源:互聯網
上載者:User

出現java.net.SocketTimeoutException: Read timed out的問題

最後定位到如下地方:

   httpURL = new URL(url);
   System.out.println("-----------url:"+url+"--------------");
   connection = (HttpURLConnection) httpURL.openConnection();
   System.out.println("-----------httpURL.openConnection()--------------");
   connection.setDoInput(true);
   connection.setDoOutput(false);
   connection.setRequestMethod("GET");
   connection.setConnectTimeout(2000);
   connection.setReadTimeout(2000);
   System.out.println("-----------connection.setReadTimeout(30000)--------------");
   connection.connect();
   System.out.println("-----------connection.connect()--------------");
   cin = connection.getInputStream();
   System.out.println("-----------connection.getInputStream():"+cin.toString()+"--------------");
   BufferedInputStream bci = new BufferedInputStream(cin);
   System.out.println("-----------new BufferedInputStream(cin)--------------");
   SAXReader reader = new SAXReader();
   doc = reader.read(bci);
   System.out.println("-----------doc = reader.read(bci);:"+doc.toString()+"--------------");

一開始並沒有任何提示,這些提示都是後來加上去的

查了很多,大家給的提議要麼就是提高機子的效能,要麼就是將timeout的時間延長

提高機子的效能肯定不行了,於是我加大了timeout的時間,可是開始不行!!!!

鬱悶,又看了看,網上似乎提到Proxy 伺服器好像跟這個有關,正好這時看見一個兄台提示,如是對代碼做了以下修改

URL httpURL = null;
  HttpURLConnection connection = null;
  Document doc = null;
  InputStream cin = null;
  try {
   Properties prop = System.getProperties();
    System.getProperties().put("proxySet","true");
    // 設定http訪問要使用的Proxy 伺服器的地址
    prop.setProperty("http.proxyHost","172.17.0.2");
    // 設定http訪問要使用的Proxy 伺服器的連接埠
    prop.setProperty("http.proxyPort","8080");
   httpURL = new URL(url);
   System.out.println("-----------url:"+url+"--------------");
   connection = (HttpURLConnection) httpURL.openConnection();
   System.out.println("-----------httpURL.openConnection()--------------");
   String authentication = "username:password"; // 使用者名稱密碼
   String encodedLogin = new BASE64Encoder().encode(authentication.getBytes()); // 編碼
   connection.setRequestProperty("Proxy-Authorization", " Basic " + encodedLogin);  // 授權
   System.out.println("-----------connection.setRequestProperty:"+encodedLogin+"--------------");
   connection.setDoInput(true);
   connection.setDoOutput(false);
   connection.setRequestMethod("GET");
   connection.setConnectTimeout(10000);
   connection.setReadTimeout(30000);
   System.out.println("-----------connection.setReadTimeout(30000)--------------");
   connection.connect();
   System.out.println("-----------connection.connect()--------------");
   cin = connection.getInputStream();
   System.out.println("-----------connection.getInputStream():"+cin.toString()+"--------------");
   BufferedInputStream bci = new BufferedInputStream(cin);
   System.out.println("-----------new BufferedInputStream(cin)--------------");
   SAXReader reader = new SAXReader();
   doc = reader.read(bci);
   System.out.println("-----------doc = reader.read(bci);:"+doc.toString()+"--------------");
  } catch (Exception e) {
   e.printStackTrace();
   doc = null;
  } finally {
   if (cin != null) {
    try {
     cin.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
   }
   if(connection!=null){
    connection.disconnect();
   }

  }

標紅的地方為修改的地方,就是為請求添加了個Proxy 伺服器

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.