【发布时间】:2015-02-16 22:17:18
【问题描述】:
我们通过 weblogic 服务器 (node1/node2) 上的 java 独立示例代码使用代理设置调用 URL。 此代码在节点 1 上运行良好,但相同的代码在节点 2 服务器上不起作用。 我们已经检查了代理设置和凭据都很好,但我们仍然收到以下错误:
java.net.ProtocolException: Server redirected too many times (20)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1323)
at ProxyCode.start2(ProxyCode.java:54)
at ProxyCode.main(ProxyCode.java:23)
Exception in thread "Main Thread" java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:61)
at java.io.InputStreamReader.<init>(InputStreamReader.java:55)
at ProxyCode.readFromInputStream(ProxyCode.java:65)
at ProxyCode.start2(ProxyCode.java:59)
at ProxyCode.main(ProxyCode.java:22)
另外,请在下面找到我的代码 sn-p: SimpleAuthenticator.java
导入 java.net.Authenticator; 导入 java.net.PasswordAuthentication;
public class SimpleAuthenticator extends Authenticator
{
private String username;
private String password;
public SimpleAuthenticator(String username,String password)
{
this.username = username;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(
username,password.toCharArray());
}
}
主类:
String url = "http://www.oracle.com/technetwork/java/readme-2-149793.txt";
String proxy = "proxyserver";
String port = "8080";
String username = "username";
String password = "password";
Authenticator.setDefault(new SimpleAuthenticator(username,password));
URL server = null;
try {
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
server = new URL(url);
}
catch (MalformedURLException e) {
e.printStackTrace();
}
Properties systemProperties = System.getProperties();
systemProperties.setProperty("http.proxyHost", proxy);
systemProperties.setProperty("http.proxyPort", port);
InputStream in = null;
URLConnection connection = null;
try {
connection = (URLConnection) server.openConnection();
connection.connect();
in = connection.getInputStream();
}
catch (IOException e) {
e.printStackTrace();
}
System.out.println(readFromInputStream(in));
}
public static String readFromInputStream(InputStream in) {
StringBuffer strBuf = new StringBuffer();
char ac[];
BufferedReader buf = new BufferedReader(new InputStreamReader(in));
try
{
while (buf.ready()) {
ac = new char[10000];
buf.read(ac);
strBuf.append(ac);
}
buf.close();
}
catch (IOException e) {
e.printStackTrace();
}
几个月以来,我们一直陷入困境,无法在任何地方获得任何有用的信息。 请帮忙。谢谢
【问题讨论】:
-
您是否使用过 Firebug 或 chrome/IE 开发人员工具来查看进行了哪些重定向?有时这可以为正在发生的事情提供线索。
-
我在 unix box 上运行它。使用 CURL 测试结果是肯定的。它可以工作..但是从 JAVA 代码调用 URL 不起作用。
-
我仍然认为查看重定向的内容会有所帮助。 getfirebug.com/network
-
请问还有其他选择吗?
标签: java http-proxy