简体   繁体   English

Java HttpURLConnection问题:服务器重定向次数过多

[英]Java HttpURLConnection issue: Server redirected too many times

I'm working with Java 1.7. 我正在使用Java 1.7。

When I test the request with Postman in Firefox, I get a response status : 200, and the Json response is good. 当我在Firefox中使用Postman测试请求时,我得到一个响应状态:200,并且Json响应很好。

When I test it with my Java application, I get this Exception: 当我用我的Java应用程序测试它时,我得到了这个异常:

java.net.ProtocolException: Server redirected too many times (20) java.net.ProtocolException:服务器重定向次数过多(20)

Here is my java code: 这是我的java代码:

try{
    String charset = "UTF-8";
    URL url = new URL("http://example.com/ws");
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setRequestMethod("GET"); 
    con.setRequestProperty("Accept-Charset", charset);
    con.setRequestProperty("token", "mytokenvalue");
    int responseCode = con.getResponseCode();
    BufferedReader in = new BufferedReader(
                        new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();
    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();
    System.out.println(response.toString());
}catch(Exception ex){
    ex.printStackTrace();
}

The exception is thrown in this line: 此行中抛出异常:

int responseCode = con.getResponseCode();

您必须在打开连接之前设置此属性:

HttpURLConnection.setFollowRedirects(false);

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

相关问题 服务器重定向太多次 - Server redirected too many times java.net.ProtocolException: 服务器重定向次数过多 (20) - java.net.ProtocolException: Server redirected too many times (20) java.net.ProtocolException: 服务器重定向次数过多 - java.net.ProtocolException: Server redirected too many times @PreAuthorize(“ isAuthenticated()”)重定向服务器太多次 - @PreAuthorize(“isAuthenticated()”) redirected server too many times java.net.Authenticator:java.net.ProtocolException:服务器重定向太多次(20) - java.net.Authenticator : java.net.ProtocolException: Server redirected too many times (20) 获取“java.net.ProtocolException:服务器重定向次数太多”错误 - Getting “java.net.ProtocolException: Server redirected too many times” Error SOAP客户端 - ProtocolException:服务器重定向次数过多 - SOAP client - ProtocolException: Server redirected too many times 尝试检索 Sharepoint WSDL 失败,并显示“服务器重定向太多次” - Attempt to retrieve Sharepoint WSDL fails with “Server redirected too many times” 尝试使用 HTTPS 进行连接:服务器重定向次数过多 - Tring to connect using HTTPS: Server redirected too many times 使用Java / HttpURLConnection检索重定向的URL - Retrieve redirected URL with Java / HttpURLConnection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM