1.获取cookies
public void testGetCookies() throws IOException {
HttpGet get= new HttpGet(uri);
//HttpClient没有获取cookies的方法
DefaultHttpClient client=new DefaultHttpClient();
HttpResponse response=client.execute(get);
result= EntityUtils.toString(response.getEntity(),"utf-8");
//获取cookies信息
this.store=client.getCookieStore();
List<Cookie>cookies=store.getCookies();
for(Cookie cookie:cookies){
System.out.println(cookie.getName()+":"+cookie.getValue());
}
2.发送带cookies的get请求
public void getwithCookies() throws IOException {
HttpGet get=new HttpGet(uri);
DefaultHttpClient client=new DefaultHttpClient();
//设置cookies信息
client.setCookieStore(this.store);
HttpResponse response=client.execute(get);
int statusCode=response.getStatusLine().getStatusCode();
System.out.println(statusCode);
if(statusCode==200){
String result=EntityUtils.toString(response.getEntity());
System.out.println(result);
}