2015-04-30 30 views
2

我想創建一個函數,通過代理服務器創建(HTTP)URLConnection。
我已經用互聯網上的一些免費代理測試過了。
所有人(我測試過),一切工作正常

但是,如果我在我的客戶網絡(與客戶代理服務器)中運行此代碼失敗urlCon.getInputStream() IOException。

我在同一個網絡上(使用相同的代理服務器)對Apache HttpClient做了一些測試。
Apache HttpClient工作正常。
但是爲什麼我的代碼(使用HttpURLConnection)不起作用?HttpURLConnection代理奇怪的IOException(「錯誤寫入服務器」)

編輯:換句話說:
爲什麼我的代碼中使用客戶代理服務器時拋出例外,?
爲什麼我在使用ApacheHTTPClient和客戶代理時得到也不例外

這是我工作的代碼:

final String proxyURL = txtProxyURL.getText(); 
    final String proxyPort = txtProxyPort.getText(); 
    final String proxyUserName = txtProxyUserName.getText(); 
    final String proxyPassword = new String(txtProxyPassword.getPassword()); 

    String sURL = m_mainFrame.getCurrentAdress(); 
    BufferedReader reader = null; 
    try 
    {    
      HttpURLConnection urlCon;     
      URL testURL = new URL(sURL);     

      Authenticator authenticator = new Authenticator() { 
       @Override 
       public PasswordAuthentication getPasswordAuthentication() { 
        return (new PasswordAuthentication(proxyUserName, proxyPassword.toCharArray())); 
       } 
      }; 
      Authenticator.setDefault(authenticator);     

      //Proxy instance, proxy ip = 10.0.0.1 with port 8080 
      Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyURL, Integer.parseInt(proxyPort))); 
      urlCon = (HttpURLConnection)testURL.openConnection(proxy); 

      urlCon.setRequestMethod("GET"); 
      urlCon.setDoOutput(true); 
      urlCon.setReadTimeout(1000 * 300); 
      urlCon.setConnectTimeout(1000 * 300); 
      urlCon.connect(); 

      //!!! HERE THE EXCEPTION OCCURS (when calling getInputStream): 
      reader = new BufferedReader(new InputStreamReader(urlCon.getInputStream(),"UTF-8")); 
      StringBuilder sb = new StringBuilder(); 
      char[] cbuf = new char[512]; 
      while (reader.read(cbuf) > -1) 
      { 
       String s = new String(cbuf); 
       sb.append(s); 
      } 

      System.out.println(sb.toString()); 

      int responseCode = urlCon.getResponseCode();     
      if (responseCode == 200) 
       JOptionPane.showMessageDialog(null, "Proxy OK for URL: " + sURL); 
      else 
       JOptionPane.showMessageDialog(null, "Proxy not OK for URL:"+ sURL + "\r\nIt returns Code: " + responseCode); 
    } 
    catch (Exception ex) 
    { 
     ex.printStackTrace();    
    } 

的代碼失敗與此異常:

java.io.IOException: Error writing to server 
at sun.reflect.GeneratedConstructorAccessor2.newInstance(Unknown Source) 
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) 
at java.lang.reflect.Constructor.newInstance(Unknown Source) 
at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source) 
at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(Unknown Source) 
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source).... 

Caused by: java.io.IOException: Error writing to server 
at sun.net.www.protocol.http.HttpURLConnection.writeRequests(Unknown Source) 
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) 
at sun.net.www.protocol.http.HttpURLConnection.getHeaderFields(Unknown Source) 
at *.*.webstarter.ui.SettingsDlg.testProxy(SettingsDlg.java:673) 
... 84 more 
+0

通過代理傳遞請求,試試這種方式。 – blackOcean

+0

@ blackOcean:正如你在我的代碼中看到的那樣,請求**是**通過代理'testURL.openConnection(proxy);' – Lars

+0

通過這種方式傳遞的,因爲幾天後我碰到了。 getINputStream和getOutputStream將不起作用,因爲仍然沒有連接。您必須通過這種方式創建連接,然後使用URL連接。它會工作。 – blackOcean

回答

-3

使用的TrustManager和認證是有點麻煩。一個人認爲你可以做的就是通過代理傳遞你的請求。

System.getProperties().put("https.proxyHost", "proxy.url"); // Enter proxy of the url like proxy.domain.com 
    System.getProperties().put("https.proxyPort", "port no"); // Enter port no most common is 80 for http. 

您可以從Internet屬性>連接,然後LAN設置檢查代理。 通過代理髮送請求後,請設置代理主機和端口爲空。

對於httpRequest使用http.proxyHost並對httpsRequest使用https.proxyHost。

+0

我的問題與SSL/HTTPS沒有任何關係。我簡化了我的代碼,但同樣的問題發生。 – Lars

+0

並且不要使用代碼來處理不是代碼的文本。 – EJP