展开全部
httpclient默认自动处理redirect,返回200是因为它已经自动跳转了,如62616964757a686964616fe59b9ee7ad9431333337613239果想抓到301状态,可以手动修改跳转规则(以下代码基于httpclient 4.5):public static void main(String[] args) {
CloseableHttpClient
httpclient = HttpClients.custom().setRedirectStrategy(new
DefaultRedirectStrategy() {
public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) {
return false;
}}).build();
HttpClientContext context = HttpClientContext.create();
HttpGet httpget = new HttpGet("
);
CloseableHttpResponse response = null;
try {
response = httpclient.execute(httpget, context);
HttpHost target = context.getTargetHost();
List redirectLocations = context.getRedirectLocations();
URI location = URIUtils.resolve(httpget.getURI(), target, redirectLocations);
System.out.println("Final HTTP location: " + location.toASCIIString());
System.out.println(response.getStatusLine());
// Expected to be an absolute URI
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}