與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