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