hosts文件设置如下:
127.0.0.1 steve091801.qa.com
运行下面的main函数:
public static void main(String[] args) throws Exception{
String url="http://steve091801.qa.com/blank.html";
testConnect(url);
//testConnect函数在控制台输出请求的状态为200,这时候修改hosts文件
127.0.0.1 steve091802.qa.com
在控制输入任意的字符,然后回车,程序继续执行
String str = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
str = br.readLine();
testConnect(url);
//这是testConnect函数为什么仍然在控制台输出请求的状态为200?
}testConnection函数如下:
public static void testConnect(String theURL){
HttpClient fcClient = new HttpClient();
HttpMethod fcMethod = new GetMethod(theURL);
fcClient.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
NameValuePair[] nvpa = new NameValuePair[1];
nvpa[0] = new NameValuePair("siteid", "400009708");
try{
int statusCode = fcClient.executeMethod(fcMethod);
System.out.println("statsCode:"+statusCode);
System.out.println(fcMethod.getResponseBodyAsString());
}catch(Exception e){
System.out.println(e.getMessage());
}
fcMethod.releaseConnection();
}
127.0.0.1 steve091801.qa.com
运行下面的main函数:
public static void main(String[] args) throws Exception{
String url="http://steve091801.qa.com/blank.html";
testConnect(url);
//testConnect函数在控制台输出请求的状态为200,这时候修改hosts文件
127.0.0.1 steve091802.qa.com
在控制输入任意的字符,然后回车,程序继续执行
String str = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
str = br.readLine();
testConnect(url);
//这是testConnect函数为什么仍然在控制台输出请求的状态为200?
}testConnection函数如下:
public static void testConnect(String theURL){
HttpClient fcClient = new HttpClient();
HttpMethod fcMethod = new GetMethod(theURL);
fcClient.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
NameValuePair[] nvpa = new NameValuePair[1];
nvpa[0] = new NameValuePair("siteid", "400009708");
try{
int statusCode = fcClient.executeMethod(fcMethod);
System.out.println("statsCode:"+statusCode);
System.out.println(fcMethod.getResponseBodyAsString());
}catch(Exception e){
System.out.println(e.getMessage());
}
fcMethod.releaseConnection();
}
不是的。
我第一次请求那个URL,返回200是正确的,但是然后我改变了hosts文件,再请求那个URL,由于URL的映射已经改变了,现在不可以访问了,HttpClient返回的应该不是200。但实际的结果却仍然是200.