【问题标题】:HttpURLConnection Proxy strange IOException ("Error writing to server")HttpURLConnection 代理奇怪的 IOException(“写入服务器时出错”)
【发布时间】:2015-04-30 11:08:15
【问题描述】:

我想创建一个通过代理服务器创建 (HTTP)URLConnection 的函数。
我已经使用互联网上的一些免费代理对此进行了测试。
对于所有这些(我已经测试),一切正常。

但如果我在我的客户网络(使用客户代理服务器)中运行此代码,代码将在 urlCon.getInputStream() 处失败,并出现 IOException。

我在同一网络(使用相同的代理服务器)上使用 Apache HttpClient 进行了一些测试。
Apache HttpClient 工作正常。
但是为什么我的代码(使用 HttpURLConnection)不起作用?

编辑: 换句话说:
为什么我的代码在使用客户代理服务器时抛出异常?
为什么在使用 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

【问题讨论】:

    标签: java proxy httpurlconnection


    【解决方案1】:

    使用 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。

    【讨论】:

      猜你喜欢
      • 2014-04-18
      • 1970-01-01
      • 2017-11-07
      • 2010-10-12
      • 2011-03-04
      • 1970-01-01
      • 2015-11-02
      • 2013-02-08
      • 1970-01-01
      相关资源
      最近更新 更多