java测试请求网站返回的状态码

来源:互联网 发布:lol进游戏无法连接网络 编辑:程序博客网 时间:2024/05/29 17:00

源程序如下,需要导入httpclient-x.x.x.jar,commons-logging-x.x.x.jar,httpcore-x.x.x.jar包

          public void testHtml(){
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://www.vooec.com/404.htm");
HttpResponse response;
try {
response = client.execute(httpGet);
int code = response.getStatusLine().getStatusCode();
System.out.println("状态:"+code);
if(code != 200){
System.out.println("链接无效");
}else if(code == 200){
System.out.println("链接有效");
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}

      public static void main(String[] args) {

            new GetHtml().testHtml();

      }


0 0