简体   繁体   English

获取“java.net.ProtocolException:服务器重定向次数太多”错误

[英]Getting “java.net.ProtocolException: Server redirected too many times” Error

I'm making a simple URL request with code like this: 我正在使用以下代码创建一个简单的URL请求:

URL url = new URL(webpage);
URLConnection urlConnection = url.openConnection();
InputStream is = urlConnection.getInputStream();

But on that last line, I'm getting the "redirected too many times error". 但是在最后一行,我得到了“重定向太多次错误”。 If my "webpage" var is, say, google.com then it works fine, but when I try to use my servlet's URL then it fails. 如果我的“网页”var是google.com,那么它可以正常工作,但是当我尝试使用我的servlet的URL时,它就失败了。 It seems I can adjust the number of times it follows the redirects (default is 20) with this: 我似乎可以通过以下方式调整重定向后的次数(默认为20):

System.setProperty("http.maxRedirects", "100");

But when I crank it up to, say, 100 it definitely takes longer to throw the error so I know it is trying. 但是当我把它调到100时,它肯定需要更长的时间来抛出错误所以我知道它正在尝试。 However, the URL to my servlet works fine in (any) browser and using the "persist" option in firebug it seems to only be redirecting once. 但是,我的servlet的URL在(任何)浏览器中工作正常,并使用firebug中的“persist”选项,它似乎只重定向一次。

A bit more info on my servlet ... it is running in tomcat and fronted by apache using 'mod-proxy-ajp'. 关于我的servlet的更多信息...它在tomcat中运行,并使用'mod-proxy-ajp'在apache前面运行。 Also of note, it is using form authentication so any URL you enter should redirect you to the login page. 另外值得注意的是,它使用的是表单身份验证,因此您输入的任何URL都应该将您重定向到登录页面。 As I said, this works correctly in all browsers, but for some reason the redirect isn't working with the URLConnection in Java 6. 正如我所说,这在所有浏览器中都能正常工作,但由于某些原因,重定向不适用于Java 6中的URLConnection。

Thanks for reading ... ideas? 感谢阅读...想法?

It's apparently redirecting in an infinite loop because you don't maintain the user session. 它显然是在无限循环中重定向,因为您不维护用户会话。 The session is usually backed by a cookie. 会话通常由cookie支持。 You need to create a CookieManager before you use URLConnection . 在使用URLConnection之前,需要创建一个CookieManager

// First set the default cookie manager.
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));

// All the following subsequent URLConnections will use the same cookie manager.
URLConnection connection = new URL(url).openConnection();
// ...

connection = new URL(url).openConnection();
// ...

connection = new URL(url).openConnection();
// ...

See also: 也可以看看:

Duse, I have add this lines: Duse,我添加了这句话:

java.net.CookieManager cm = new java.net.CookieManager();
java.net.CookieHandler.setDefault(cm);

See this example: 看这个例子:

java.net.CookieManager cm = new java.net.CookieManager();
java.net.CookieHandler.setDefault(cm);
String buf="";
dk = new DAKABrowser(input.getText());
try {
    URL url = new URL(dk.toURL(input.getText()));
    DataInputStream dis = new DataInputStream(url.openStream());
    String inputLine;
    while ((inputLine = dis.readLine()) != null) {
        buf+=inputLine;
        output.append(inputLine+"\n");
    }
    dis.close();
} 
catch (MalformedURLException me) {
    System.out.println("MalformedURLException: " + me);
}
catch (IOException ioe) {
    System.out.println("IOException: " + ioe);
}
titulo.setText(dk.getTitle(buf));

I was using Jenkins on Tomcat6 on a unix environment and got this bug. 我在一个unix环境中在Tomcat6上使用Jenkins并得到了这个bug。 For some reason, upgrading to Java7 solved it. 出于某种原因,升级到Java7解决了它。 I'd be interested to know exactly why that fixed it. 我有兴趣知道为什么修复它。

I had faced the same problem and it took considerable amount of time to understand the problem. 我遇到了同样的问题,花了相当多的时间来理解这个问题。 So to summarize the problem was in mismatch of headers. 总而言之,问题在于标题不匹配。

Consider below being my Resource 考虑下面是我的资源

  @GET
  @Path("booksMasterData")
  @Produces(Array(core.MediaType.APPLICATION_JSON))
  def booksMasterData(@QueryParam("stockStatus") stockStatus : String): Response = {
 // some logic here to get the books and send it back
    }

And here is client code, which was trying to connect to my above resource 这是客户端代码,它试图连接到我的上述资源

ClientResponse clientResponse = restClient.resource("http://localhost:8080/booksService").path("rest").path("catalogue").path("booksMasterData").accept("application/boks-master-data+json").get(ClientResponse.class);

And the error was coming on exactly above line. 错误正好在线上。

What was the problem? 出了什么问题?

My Resource was using 我的资源正在使用

"application/json" “应用程序/ JSON”

in

@Produces annotation @Produces注释

and my client was using 我的客户正在使用

accept("application/boks-master-data+json") and this was the problem. 接受(“application / boks-master-data + json”),这就是问题所在。

It took me long to find out this as the error was no where related. 我花了很长时间才发现这一点,因为错误与此无关。 Break through was when I tried to access my resource in postman with 突破是当我试图在邮递员中访问我的资源时

Accept-> "application/json" header Accept->“application / json”标题

it worked fine, however with 它运作良好,但随着

Accept-> "application/boks-master-data+json" header it doesnt. 接受 - >“application / boks-master-data + json”标题它没有。

And again, even Postman was not giving me proper error. 而且,即使是邮递员也没有给我正确的错误。 The error was too generic. 错误过于笼统。 Please see the below image for reference. 请参阅下图以供参考。 通用错误消息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM