请问httpclient如何判断 链接成功与否?
我的代码: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
如何判断链接成功与否?

解决方案 »

  1.   

    200 ok ---HttpStatus.SC_OK if (statusCode != HttpStatus.SC_OK) {  
         System.err.println("Method failed: "  
                            + getMethod.getStatusLine());  
     } context meun--> find src -->maybe [http://www. baidu000.com] has something
      

  2.   

    if (statusCode != HttpStatus.SC_OK) {  
        System.err.println("Method failed: "  
                            + getMethod.getStatusLine());  
      

  3.   

    http://www.360doc.com/content/090916/17/61497_6053983.html
      

  4.   

    注:csdn不知道为什么老是 自动多加空格
    www.baidu.com自动变成www. baidu.com
      

  5.   

    我按2,3楼的写法,import java.io.IOException; 
    import org.apache.commons.httpclient.*; 
    import org.apache.commons.httpclient.methods.*; 
    public class SimpleClient {
    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.baidu000011111111.com");
            int statusCode=client.executeMethod(method);
            System.out.println(statusCode);
            // 打印服务器返回的状态
            System.out.println(method.getStatusLine());//HTTP/1.1 200 OK
            if (statusCode != HttpStatus.SC_OK) {  
                System.err.println("Method failed: "  
                                    + method.getStatusLine());  
            } 结果还是:
    200
    HTTP/1.1 200 OK
    请指出我所犯错误之处
      

  6.   

    lz先用下面的程序看一下返回的结果
    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());
      

  7.   

    LZ的方法不错,
    还可以用这个方法System.out.println(method.getResponseBodyAsString());能打开的网站输出一大堆,初步观察是页面的源代码都输出了
    打不开的网站用System.out.println(method.getResponseBodyAsString().length());
    输出为0,但是method.getResponseBodyAsString()!=""
      

  8.   

    刚才 用public static void main(String[] args) {
    URL url;
    HttpURLConnection   connection;
    try {
    url = new URL("http://www.baidu000000.com");
    connection = (HttpURLConnection) url.openConnection();
    System.out.println(connection.getResponseCode());
    System.out.println(connection.getContentLength());
    System.out.println(connection.getResponseMessage());
    } catch (IOException e) {
    e.printStackTrace();
    }
    }结果也是
    200
    0
    OK莫非jdk自身的问题?