java.net.ProtocolException:设置CookieHandler无效时

时间:2018-07-04 09:02:20

标签: java jsp

这是我在jsp中的代码:

<c:catch var="feederror">
    <%
        String feedUrl = (String) pageContext.getAttribute("url");
        String feedXml = "";  
      HttpURLConnection conn = (HttpURLConnection) new URL(feedUrl).openConnection();
      BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
      String line;
      while ((line = reader.readLine()) != null) {
          feedXml += line;
      }
      reader.close();
      pageContext.setAttribute("feedXml", feedXml.trim().replaceAll("",""));    
    %>
</c:catch>

变量 feederror 返回java.net.ProtocolException:服务器重定次数过多(20)。

我尝试过:

  • 使用禁用的Cookie打开浏览器-页面根本无法加载(404);
  • 在打开连接之前使用CookieHandler.setDefault(new CookieManager(null,CookiePolicy.ACCEPT_ALL))-没做任何更改;
  • 尝试过CookieHandler.setDefault(new CookieManager())-没做任何更改;

如何解决该问题?

1 个答案:

答案 0 :(得分:1)

java.net.ProtocolException:服务器重定了太多次(20)。

此异常来自HttpURLConnection conn =(HttpURLConnection)新的URL(feedUrl).openConnection();

对于99%的用户,您正在调用一个url,该URL向自身发送重定向,或向站点B发送重定向,该站点B重定向至站点A,该站点重定向至站点B等,因此存在循环。

检查您要调用的URL并通过一些外部工具进行验证,以便您可以检查外部服务器的标头和响应。

我认为与Cookie无关