日期:2014-05-20  浏览次数:20931 次

httpclient如何判断 链接成功与否
请问httpclient如何判断 链接成功与否?
我的代码:
Java code

public static void main(String[] args) throws IOException {
        HttpClient client = new HttpClient();
//        HttpMethod method = new GetMethod("http://www. baidu.com");
        HttpMethod method = new GetMethod("http://www. baidu000.com");
        int i=client.executeMethod(method);
        System.out.println(i);
        // 打印服务器返回的状态
        System.out.println(method.getStatusLine());//HTTP/1.1 200 OK
}


我的问题是:www.baidu000.com明明打不开,为什么服务器返回的状态还是200 OK
如何判断链接成功与否?

------解决方案--------------------
lz先用下面的程序看一下返回的结果
Java code
BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
            String line = null;
            StringBuffer str = new StringBuffer();
            line = reader.readLine();
            if (null != line) {
                str.append(line);
            }
            while ((line = reader.readLine()) != null) {
                str.append(System.getProperty("line.separator")).append(line);
            }
            System.out.println("应答结果:" + str.toString());

------解决方案--------------------
有可能是被什么工具给跳转到其它地址了