我访问一个URL,该URL发送301重定向到同一服务器内的某个位置。请参阅下面的curl输出
curl -I https://www.dropbox.com/<somedir>/test_out4.mp4
HTTP/1.1 301 Moved Permanently
Location: /s/raw/<someotherdir>/test_out4.mp4 <<< different path in same host
当我尝试使用Apache HttpComponents在Java中访问此URL并禁用重定向时,它始终遵循重定向。我没有得到301,而是得到了200。
public static void main(String[] args) throws IOException, URISyntaxException {
HttpHead request = new HttpHead(new URI(SAMPLE_URL));
HttpClient client = HttpClients.custom().disableRedirectHandling().build();
HttpResponse response = client.execute(request);
System.out.println(response.getStatusLine());
DefaultHttpClient instance = new DefaultHttpClient();
HttpParams params = new BasicHttpParams();
params.setParameter(ClientPNames.HANDLE_REDIRECTS, false);
request.setParams(params);
response = instance.execute(request);
System.out.println(response.getStatusLine());
}
4.3和4.3之前的方法都失败。
HTTP / 1.1 200 OK <<< disableRedirectHandling()失败
HTTP / 1.1 200 OK <<< ClientPNames.HANDLE_REDIRECTS,false)失败
如何禁用所有重定向,甚至禁用本地主机内的重定向?