与java.net.HttpURLConnection相比,Apache HttpClient处理GET请求的方式不同吗?处理GET请求 - Apache HttpClient vs java.net.HttpURLConnection
我试着向一个URL发出一个GET请求,它使用这两种方法返回一个重定向。虽然从HttpURLConnection的响应代码返回一个302预期,使得使用HttpClient的结果在200
下同电话是我的代码:
// Using Apache HttpClient
HttpClient client = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();
HttpGet request = new HttpGet(authUrl);
HttpResponse response = client.execute(request);
int responseCode = response.getStatusLine().getStatusCode(); //Returns 200
// Using java.net.HttpURLConnection
URL obj = new URL(authUrl);
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
int responseCode = conn.getResponseCode(); //Returns 302
这是使用Apache的HttpClient我的第一次,所以我代码可能是错误的。
谢谢。
看标题为“位置”可以读取头 - 你可以从得到重定向。 (for header header:response.getHeaders(“Location”)){redirectLink = header.getValue(); }' – Ascalonian
我试过了。它没有任何“位置”标题,因为响应是200,而不是302。 – drunkenfist