Java HttpURLConnection代理异常(“写入服务器时出错”)

Java HttpURLConnection代理异常(“写入服务器时出错”),java,proxy,httpurlconnection,Java,Proxy,Httpurlconnection,我想创建一个通过代理服务器创建HttpUrl连接的函数。我已经用一些来自internet的免费代理进行了测试。我已经测试了所有这些,一切正常。但是如果我在客户网络中使用客户代理服务器运行此程序,代码在urlCon.getInputStream失败,并出现IOException。我使用Apache HttpClient在同一网络上使用同一代理服务器进行了一些测试。ApacheHttpClient工作得很好。但是为什么我使用HttpURLConnection的代码不能工作呢 编辑:换句话说: 为什么

我想创建一个通过代理服务器创建HttpUrl连接的函数。我已经用一些来自internet的免费代理进行了测试。我已经测试了所有这些,一切正常。但是如果我在客户网络中使用客户代理服务器运行此程序,代码在urlCon.getInputStream失败,并出现IOException。我使用Apache HttpClient在同一网络上使用同一代理服务器进行了一些测试。ApacheHttpClient工作得很好。但是为什么我使用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

使用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,对HttpSreRequest使用https.proxyHost。

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

通过代理传递请求,请尝试这种方式。@blackOcean:正如您在我的代码中看到的,请求通过代理testURL.openConnectionproxy传递;试着这样做,因为几天前我被击中了。getINputStream和getOutputStream将不工作,因为仍然没有连接。您必须通过这种方式创建连接,然后只需使用URL连接。黑海:也许你没有注意到,但我的代码已经完全正常工作了。我只有一个例外,当使用客户端代理服务器时,我在使用ApacheHTTPClient时没有问题。通过代理传递请求,请尝试这种方式。@blackOcean:正如您在我的代码中看到的,请求通过代理testURL.openConnectionproxy传递;试着这样做,因为几天前我被击中了。getINputStream和getOutputStream将不工作,因为仍然没有连接。您必须通过这种方式创建连接,然后只需使用URL连接。黑海:也许你没有注意到,但我的代码已经完全正常工作了。我只有一个例外,当使用客户端代理服务器时,我使用ApacheHTTPClient没有问题。我的问题与SSL/HTTPS无关。我简化了我的代码,但也出现了同样的问题。不要使用代码来填充非代码的文本。我的问题与SSL/HTTPS无关。我简化了我的代码,但是同样的问题也发生了。不要用代码来填充不是代码的文本。