java.net.SocketTimeoutException: Read timed out

今天在一台虚拟机上部署公司项目的时候,出现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的时间,可是还是不行!!!!

郁闷,又看了看,网上似乎提到代理服务器好像跟这个有关,正好这时看见一个兄台提示,如是对代码做了以下修改

URL httpURL = null;
  HttpURLConnection connection = null;
  Document doc = null;
  InputStream cin = null;
  try {
   Properties prop = System.getProperties(); 
    System.getProperties().put("proxySet","true"); 
    // 设置http访问要使用的代理服务器的地址 
    prop.setProperty("http.proxyHost","172.17.0.2"); 
    // 设置http访问要使用的代理服务器的端口 
    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();
   }
  }


标红的地方为修改的地方,就是为请求添加了个代理服务器

OK

你可能感兴趣的:(JAVA)