gpt4 book ai didi

java.net.Authenticator : java.net.ProtocolException:服务器重定向次数过多 (20)

转载 作者:搜寻专家 更新时间:2023-11-01 03:37:12 26 4
gpt4 key购买 nike

我们在我们的 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)

此外,请在下面找到我的代码片段: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();
}

几个月以来,我们一直陷入困境,无法在任何地方获得任何有用的信息。请帮助。谢谢

最佳答案

如果您提供错误的凭据(用户名或密码),您将收到此错误。这发生在我身上,基于 Glassfish 的网络应用程序。但是,我期待 401 响应。

我认为,身份 validator 会多次尝试相同的凭据。

关于java.net.Authenticator : java.net.ProtocolException:服务器重定向次数过多 (20),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27541300/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com