我想创建一个通过代理服务器创建(HTTP)URLConnection的函数。
我已经通过互联网上的一些免费代理对此进行了测试。
所有这些(我已经测试过) ,一切正常。
但是如果我在我的客户网络(客户代理服务器)中运行此代码,则urlCon.getInputStream()
的代码会因IOException而失败。< br />我在同一个网络上使用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
答案 0 :(得分:-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属性”&gt;中检查代理。连接然后LAN设置。 通过代理发送请求后,请将代理主机和端口设置为null。
使用http.proxyHost进行httpRequest,并使用https.proxyHost进行httpsRequest。