当我尝试从URL: java.net.ProtocolException: Server重定向太多次(20次)读取XML时,我得到这个错误。
我找到了一个解决方案,用下面的代码行设置cookie: CookieHandler.setDefault(new ListCookieHandler());其中"ListCookieHandler“是我用请求中的配置定义的类。
但这不管用。在方法"CookieHandler.setDefault“的javadoc中,我发现:”注意:非标准的http协议处理程序可能会忽略此设置“。我认为这可能是问题所在。对我来说还有别的解决方案吗?
我的代码是:
int timeout = 120000;
CookieHandler.setDefault(new ListCookieHandler());
HttpURLConnection conn = (HttpURLConnection) new URL("http://...").openConnection();
conn.setReadTimeout(timeout);
conn.setConnectTimeout(timeout);
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder builder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
builder.append(line).append(System.getProperty("line.separator"));
}
System.out.println(StringEscapeUtils.unescapeHtml(builder.toString()));链接中提供了ListCookieHandler的完整解决方案:http://www.java2s.com/Code/Java/JDK-6/UsingCookieHandlerinJava5.htm
发布于 2015-01-25 17:52:12
当我遇到同样的错误时,我使用了:
import java.net.CookieHandler;
import java.net.CookieManager;
CookieHandler.setDefault(new CookieManager());发布于 2019-04-12 18:42:15
在调用openConnection();之前使用此属性
HttpURLConnection.setFollowRedirects(false);https://stackoverflow.com/questions/12046202
复制相似问题