我试图让HttpClient按照我想要的方式工作,我都快疯了。这一切都让我摸不着头脑。
所有我想做的是提供一个网址,网址可以指向网页,.zip文件,.doc文件和重定向,在上述任何结束。然后,我就可以将最终的状态代码打印到控制台。
有谁能帮帮我吗?到目前为止,我有(在投入一切之后,代码一团糟):
DefaultHttpClient client = new DefaultHttpClient();
client.setRedirectStrategy(new DefaultRedirectStrategy(){
public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) {
boolean isRedirect=false;
try {
isRedirect = super.isRedirected(request, response, context );
} catch (org.apache.http.ProtocolException e) {
System.out.println("what?");
e.printStackTrace();
}
if (!isRedirect) {
int responseCode = response.getStatusLine().getStatusCode();
System.out.println(responseCode);
if (responseCode == 301 || responseCode == 302) {
System.out.println("redirect");
return true;
}
}
return isRedirect;
}
});
HttpHead test = new HttpHead("http://support.xbox.com/en-US/xbox-live/troubleshoot/game-play");
HttpEntity httpEntity = null;
try {
HttpResponse response = client.execute(test);
System.out.println(response.getStatusLine().getStatusCode());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
System.out.println(e);
System.out.println("400");
} catch (IOException e) {
System.out.println("404");
System.out.println(e);
}编辑:
对于这个url:http://support.xbox.com/en-US/xbox-live/troubleshoot/game-play,我得到的是404。我本以为会有200个。
对于http://www.google.com,不出所料,我得到了200分。
对于http://tinyurl.com/2tx,一个tinyurl重定向到google.com,我得到一个200。
我不确定我的结果是什么。我只想能够测试链接,看看从最终用户的角度来看,它们是工作的还是断开的。
发布于 2012-05-19 05:08:22
问题是您发送的是HEAD请求,而不是GET请求,对于GET请求,xbox站点只返回404。
使用:
HttpGet test = new HttpGet("http://support.xbox.com/en-US/xbox-live/troubleshoot/game-play");没有任何重定向的东西,对我来说很好。
https://stackoverflow.com/questions/10655496
复制相似问题