weidu 2016-04-05
使用同一个HttpClient,请求同一个网站的不同url,session保持不变
反之,使用不同的HttpClient,请求同一个网站的同或者不同url,session要变
publicclassTesty1{
publicstaticvoidmain(String[]args)throwsHttpException,IOException{
//TODOAuto-generatedmethodstub
HttpClientclient=newHttpClient();
GetMethodget=newGetMethod("http://localhost:13080/test/testy.jsp");
client.executeMethod(get);
Stringresult=get.getResponseBodyAsString();
result=result.substring(result.indexOf("<br>")+1);
result=result.substring(0,result.indexOf("3<br>"));
System.out.println("qnsoft1:"+result);
GetMethodget2=newGetMethod("http://localhost:13080/test/testy.jsp");
client.executeMethod(get2);
Stringresult2=get2.getResponseBodyAsString();
result2=result2.substring(result2.indexOf("<br>")+1);
result2=result2.substring(0,result2.indexOf("3<br>"));
System.out.println("q1:"+result);
System.out.println("q2:"+result2);
get2.releaseConnection();
}
}
结果:
<br>IDF29F636379F7C4F8F92B83221A0E86F3.tomcat
<br>IDF29F636379F7C4F8F92B83221A0E86F3.tomcat
publicstaticvoidmain(String[]args)throwsHttpException,IOException{
//TODOAuto-generatedmethodstub
HttpClientclient=newHttpClient();
GetMethodget=newGetMethod("http://localhost:13080/test/testy.jsp");
client.executeMethod(get);
Stringresult=get.getResponseBodyAsString();
result=result.substring(result.indexOf("<br>")+1);
result=result.substring(0,result.indexOf("3<br>"));
System.out.println("qnsoft1:"+result);
HttpClientclient2=newHttpClient();
GetMethodget2=newGetMethod("http://localhost:13080/test/testy.jsp");
client2.executeMethod(get2);
Stringresult2=get2.getResponseBodyAsString();
result2=result2.substring(result2.indexOf("<br>")+1);
result2=result2.substring(0,result2.indexOf("3<br>"));
System.out.println("q1:"+result);
System.out.println("q2:"+result2);
get2.releaseConnection();
}
结果:
<br>IDE772E60E79DB619976B18E52A78AB7D7.tomcat
<br>ID9AB143BD096AFEF43347123C241E7A59.tomcat
创建一个 HttpClient 实例,这个实例需要调用 Dispose 方法释放资源,这里使用了 using 语句。接着调用 GetAsync,给它传递要调用的方法的地址,向服务器发送 Get 请求。