【发布时间】: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