HttpClient class itself can not get cookie information, and therefore need to use DefaultHttpClient.
Since store.getCookies (); return acquires cookie information List <Cookie>, and therefore need to traverse the list in order to obtain a set of cookie information. Herein used for the collection loop through the list.
java code as follows
package com.course.httpclient.cookies; import org.apache.http.HttpResponse; import org.apache.http.client.CookieStore; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.cookie.Cookie; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; import java.io.IOException; import java.util.List; import java.util.Locale; importthe java.util.ResourceBundle; public class MyCookieForGet { Private String URL; Private ResourceBundle the bundle; // for reading the configuration file @BeforeTest public void beforeTest () { the bundle = the ResourceBundle.getBundle ( "file application" , Locale.CHINA); / / uplink code is used to read the configuration file, baseName class and resource files in the same directory URL = bundle.getString ( "test.url" ); // uplink code acquisition profile name } @Test public void test1 () throws IOException { String Result; URI String = bundle.getString ( "getCookies.uri" ); // This code is to obtain the path to the configuration file corresponding getCookies.uri String = testurl the this .url + URI; HttpGet GET = new new HttpGet (testurl); the System. out.println ( "this is testurl address" + testurl); // HttpClient Client = new new DefaultHttpClient (); HttpClient can not get cookie information DefaultHttpClient Client = new new DefaultHttpClient (); // create the HttpClient object request for executing get HttpResponse = response client.execute (GET); System.out.println ( "this is the value of the response" + response); result = EntityUtils.toString(response.getEntity(), "utf-8"); System.out.println(result); //以下代码是获取cookie信息 CookieStore store = client.getCookieStore(); List<Cookie> cookkielist = store.getCookies(); for (Cookie cookie : cookkielist) { String name = cookie.getName(); String value = cookie.getValue(); System.out.println("cookie name="+name+" cookie value="+value); } } }
Interface information configuration file as follows application.properties
test.url=http://127.0.0.1:8888 getCookies.uri=/getCookies login=/login
Analog interface information follows moco
[ { "Description": "This is a return cookies information get request" , "Request" : { "URI": "/ the getCookies" , "Method": "get" }, "Response" : { "headers" : { "the Content-the Type": "text / HTML; charset = GBK" }, "cookies" : { "Login": "111111" , "Status": "1000" }, "text": "Congratulations on your cookies information success " } } ]