请问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
如何判断链接成功与否?
我的代码: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
如何判断链接成功与否?
System.err.println("Method failed: "
+ getMethod.getStatusLine());
} context meun--> find src -->maybe [http://www. baidu000.com] has something
System.err.println("Method failed: "
+ getMethod.getStatusLine());
}
www.baidu.com自动变成www. baidu.com
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请指出我所犯错误之处
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());
还可以用这个方法System.out.println(method.getResponseBodyAsString());能打开的网站输出一大堆,初步观察是页面的源代码都输出了
打不开的网站用System.out.println(method.getResponseBodyAsString().length());
输出为0,但是method.getResponseBodyAsString()!=""
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自身的问题?