1 packagecom.course.httpclient.cookies;2
3 importorg.apache.http.HttpResponse;4 importorg.apache.http.client.CookieStore;5 importorg.apache.http.client.HttpClient;6 importorg.apache.http.client.methods.HttpGet;7 importorg.apache.http.client.methods.HttpPost;8 importorg.apache.http.cookie.Cookie;9 importorg.apache.http.entity.StringEntity;10 importorg.apache.http.impl.client.DefaultHttpClient;11 importorg.apache.http.util.EntityUtils;12 importorg.json.JSONObject;13 importorg.testng.Assert;14 importorg.testng.annotations.BeforeTest;15 importorg.testng.annotations.Test;16 importjava.io.IOException;17 importjava.util.List;18 importjava.util.Locale;19 importjava.util.ResourceBundle;20
21 public classMyCookieForPost {22 privateString url;23 private ResourceBundle bundle;//用于读取配置文件
24 private CookieStore store;//用于存储cookies信息
25
26 @BeforeTest27 public voidbeforeTest() {28
29 bundle = ResourceBundle.getBundle("application", Locale.CHINA);30 //上行代码用于读取配置文件,baseName和类在同一目录的resource文件中
31 url = bundle.getString("test.url");32 //上行代码是获取配置文件中的域名
33 }34
35 @Test36 public void getTestCookie() throwsIOException {37
38 String result;39 String uri = bundle.getString("getCookies.uri");40 //以上代码是获取配置文件中的getCookies.uri对应的路径
41 String testurl = this.url +uri;42 HttpGet get = newHttpGet(testurl);43 System.out.println("这是testurl的地址" +testurl);44 //HttpClient client = new DefaultHttpClient(); HttpClient无法获取cookie信息
45 DefaultHttpClient client = newDefaultHttpClient();46 //创建HttpClient对象,用于执行get请求
47 HttpResponse response =client.execute(get);48 System.out.println("这是response的值" +response);49 result = EntityUtils.toString(response.getEntity(), "utf-8");50 System.out.println(result);51 //以下代码是获取cookie信息
52 this.store =client.getCookieStore();53 List cookkielist =store.getCookies();54 for(Cookie cookie : cookkielist) {55 String name =cookie.getName();56 String value =cookie.getValue();57 System.out.println("cookie name=" + name + " cookie value=" +value);58 }59
60
61 }62
63 @Test(dependsOnMethods = {"getTestCookie"})64 public void postTestMethods() throwsIOException {65
66 String uri = bundle.getString("testPostWithCookies.uri");67 //以上代码是获取配置文件中的getCookies.uri对应的路径
68 String testurl = this.url +uri;69 DefaultHttpClient client = new DefaultHttpClient();//声明一个client对象用来进行方法的执行
70 HttpPost post = new HttpPost(testurl);//什么一个post方法
71 JSONObject param = new JSONObject();//添加参数 接口要求post参数的格式是json格式
72 param.put("name", "zhangshan");73 param.put("age", "18");74 post.setHeader("Accept-Encoding", "gzip, deflate"); //设置请求信息,设置header
75 post.setHeader("Content-Type", "application/json"); //设置请求信息,设置header
76 StringEntity entity = new StringEntity(param.toString(), "utf-8");//将参数信息添加到方法中
77 post.setEntity(entity);//将post方法和参数绑定在一起
78 String result; //声明一个对象,进行响应结果的存储
79 client.setCookieStore(this.store);//设置cookies信息
80 HttpResponse response = client.execute(post);//执行post方法
81 result = EntityUtils.toString(response.getEntity());//获取响应结果
82 JSONObject resultJson = new JSONObject(result); //将返回的响应结果字符串转换成json对象
83
84 String success = resultJson.getString("zhangshan");//获取响应的参数值
85 int status = resultJson.getInt("status");86 //String status = (String) resultJson.get("status");//获取响应的参数值
87 Assert.assertEquals("成功", success); //处理结果,判断期望结果和实际结果
88
89 Assert.assertEquals(1, status);90
91 }92
93 }