java測試請求網站返回的狀態碼

源程序如下,需要導入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();

      }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章