HttpClient保持session

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

相关推荐

nalanrumeng / 0评论 2020-04-09