[英]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.