这是我在这里发表的第一篇文章,但我已经两天没有任何结果而苦苦挣扎。
我正在具有Internet代理服务器的托管计算机上安装Java应用程序。
当我在这台特定的机器上使用网络浏览器时,他们不需要任何身份验证 - 只需要代理主机和端口详细信息。
当我运行以下使用UrlConnection的简单程序时,代理主机和端口也需要它才能正常运行。
public class TestProxyConnectionUrlConnection {
public void runUrlConnection() {
try {
String host = "proxy-local.net";
String port = "8080";
System.setProperty( "http.proxyHost", host);
System.setProperty( "http.proxyPort", port);
System.setProperty( "http.useProxy", "true");
URL url = new URL("http://mydomainb.com/myservice");
URLConnection yc = url.openConnection();
yc.setRequestProperty("Connection", "keep-alive");
yc.setDoOutput(true);
yc.setDoInput(true);
DataOutputStream wr = new DataOutputStream(yc.getOutputStream());
wr.writeBytes("<?xml version=\"1.0\" encoding=\"UTF-8\"?><xmlcontent>content</xmlcontent>");
wr.flush();
wr.close();
BufferedReader in = new BufferedReader( new InputStreamReader( yc.getInputStream()));
try {
String inputLine;
String response = "";
while ((inputLine = in.readLine()) != null) {
response = inputLine;
}
}
finally {
in.close();
}
System.out.println(response);
}
catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
但是,当我运行使用Apache Commons HttpClient与服务器通信的代码时,根据下一段代码,代理服务器响应407错误,并请求NTLM或Kerberos身份验证。
*请注意,由于使用HttpCommons v3.1的网络服务产品,我们暂时停止使用它。
public void runUrlConnection() {
try {
String host = "proxy-local.net";
String port = "8080";
System.setProperty( "http.proxyHost", host);
System.setProperty( "http.proxyPort", port);
System.setProperty( "http.useProxy", "true");
URL url = new URL("http://mydomainb.com/myservice");
URLConnection yc = url.openConnection();
yc.setRequestProperty("Connection", "keep-alive");
yc.setDoOutput(true);
yc.setDoInput(true);
DataOutputStream wr = new DataOutputStream(yc.getOutputStream());
wr.writeBytes("<?xml version=\"1.0\" encoding=\"UTF-8\"?><xmlcontent>content</xmlcontent>");
wr.flush();
wr.close();
BufferedReader in = new BufferedReader( new InputStreamReader( yc.getInputStream()));
try {
String inputLine;
String response = "";
while ((inputLine = in.readLine()) != null) {
response = inputLine;
}
}
finally {
in.close();
}
System.out.println(response);
}
catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
有谁知道为什么HttpClient会导致身份验证质疑,但使用标准的UrlConnection会很好地通过代理服务器吗?我有什么基本的错误吗?