学习本章最后一节 带有cookie的post请求
第6节 post方法的访问实战
1.post方法mock
[
{
"description": "带有cookies的post请求",
"request": {
"uri": "/post/withCookie",
"method": "post",
"cookies": {
"start": "end"
},
"json": {
"name": "ella",
"age": "18"
}
},
"response": {
"json": {
"name": "ella",
"age": "18",
"code": "success"
}
}
},
{
"description": "这是一个返回cookie信息的get方法",
"request": {
"uri": "/get/returnCookie",
"method": "get"
},
"response": {
"text": "return cookie success",
"cookies": {
"start": "end"
}
}
}
]
2.访问post方法步骤详解(在不知道怎么着手写代码的时候可以通过这种方式把每一步要做什么都写下来)
- 获取访问地址:从配置文件中获取并拼接完整地址
- 声明一个client对象,用来进行方法的执行
- 声明一个方法(post方法)
- 添加入参
- 设置请求头信息(json格式--contentType)
- 将入参添加到方法中
- 声明一个对象用于存储相应结果
- 设置cookies信息
- 执行post方法
- 获取响应结果
- 处理结果,断言,验证结果是否符合预期
3.代码
package com.course.cookies;
import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
public class MyCookieForPost {
//优化http请求--调用请求方法的配置化--详见application.properties文件
//读取配置文件
private String url;
private ResourceBundle bundle;
//用来存储cookies信息的变量
private CookieStore store;
@BeforeTest
public void beforeTest() {
//自动获取resources路径下的后缀名为properties的文件名为“application“的文件(相对路径)
//字符编码 Locale.CHINA
bundle = ResourceBundle.getBundle("application", Locale.CHINA);
url = bundle.getString("test.url");
}
@Test
public void testGetCookies() throws IOException {
String result;
//从配置文件中拼接url
String uri = bundle.getString("getCookies.uri");
String testUrl = this.url + uri;
HttpGet get = new HttpGet(testUrl);
//获取DefaultHttpClient请求
DefaultHttpClient client = new DefaultHttpClient();
//执行get方法
HttpResponse response = client.execute(get);
//拿到响应结果的实体entity
result = EntityUtils.toString(response.getEntity(), "utf-8");
System.out.println("result: " + result);
//获取cookies信息
this.store = client.getCookieStore();
List<Cookie> cookieList = store.getCookies();
for (Cookie cookie : cookieList) {
String name = cookie.getName();
String value = cookie.getValue();
System.out.println("cookie name = " + name
+ "; cookie value = " + value);
}
}
@Test(dependsOnMethods = {"testGetCookies"})
public void testPostWithCookie() throws IOException {
//获取访问地址:从配置文件中获取并拼接完整地址
String uri = bundle.getString("test.post.with.cookies");
String testUrl = this.url + uri;
//声明一个client对象,用来进行方法的执行
DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
//声明一个方法(post方法)
HttpPost post = new HttpPost(testUrl);
//添加入参
JSONObject param = new JSONObject();
param.put("name", "ella");
param.put("age", "18");
//设置请求头信息(json格式--contentType)
post.setHeader("content-type", "application/json");
//将入参添加到方法中
StringEntity entity = new StringEntity(param.toString(), "utf-8");
post.setEntity(entity);
//声明一个对象用于存储相应结果
String result;
//设置cookies信息
defaultHttpClient.setCookieStore(this.store);
//执行post方法
HttpResponse response = defaultHttpClient.execute(post);
//获取响应结果
result = EntityUtils.toString(response.getEntity(), "utf-8");
System.out.println(result);
//处理结果,断言,验证结果是否符合预期
//将返回的响应结果字符串转化为json对象
JSONObject object = new JSONObject(result);
String name = (String) object.getString("code");
Assert.assertEquals("success", name);
}
}
执行结果:
注意:没有json对象要先在pom文件引入json对象;要在properties文件中配置post方法的uri路径
可以优化点:
1.获取访问的url
2.请求头信息优化
3.入参添加优化
优化后代码:
package com.course.cookies;
import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
public class MyCookieForPost {
//优化http请求--调用请求方法的配置化--详见application.properties文件
//读取配置文件
private String url;
private ResourceBundle bundle;
//用来存储cookies信息的变量
private CookieStore store;
@BeforeTest
public void beforeTest() {
//自动获取resources路径下的后缀名为properties的文件名为“application“的文件(相对路径)
//字符编码 Locale.CHINA
}
@Test
public void testGetCookies() throws IOException {
String result;
//从配置文件中拼接url
String envUrl = "test.url";
String uri = "getCookies.uri";
String testUrl = getUrl(envUrl, uri);
//获取DefaultHttpClient请求
DefaultHttpClient client = new DefaultHttpClient();
//执行get方法
HttpGet get = new HttpGet(testUrl);
HttpResponse response = client.execute(get);
//拿到响应结果的实体entity
result = EntityUtils.toString(response.getEntity(), "utf-8");
System.out.println("result: " + result);
//获取cookies信息
this.store = client.getCookieStore();
List<Cookie> cookieList = store.getCookies();
for (Cookie cookie : cookieList) {
String name = cookie.getName();
String value = cookie.getValue();
System.out.println("cookie name = " + name
+ "; cookie value = " + value);
}
}
public String getUrl(String envUrl, String uriPath) {
bundle = ResourceBundle.getBundle("application", Locale.CHINA);
url = bundle.getString(envUrl);
String uri = bundle.getString(uriPath);
String testUrl = this.url + uri;
return testUrl;
}
public StringEntity addParamWithJson() {
StringEntity entity = null;
try {
JSONObject param = new JSONObject();
param.put("name", "ella");
param.put("age", "18");
entity = new StringEntity(param.toString(), "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return entity;
}
@Test(dependsOnMethods = {"testGetCookies"})
public void testPostWithCookie() throws IOException {
//获取访问地址:从配置文件中获取并拼接完整地址
String envUrl = "test.url";
String uriPath = "test.post.with.cookies";
String testUrl = getUrl(envUrl, uriPath);
//声明一个client对象,用来进行方法的执行
DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
//声明一个方法(post方法)
HttpPost post = new HttpPost(testUrl);
//设置请求头信息(json格式--contentType)
post.setHeader("content-type", "application/json");
//添加入参
//将入参添加到方法中
StringEntity entity = addParamWithJson();
post.setEntity(entity);
//设置cookies信息
defaultHttpClient.setCookieStore(this.store);
//执行post方法
HttpResponse response = defaultHttpClient.execute(post);
//声明一个对象用于存储相应结果
String result;
//获取响应结果
result = EntityUtils.toString(response.getEntity(), "utf-8");
System.out.println(result);
//处理结果,断言,验证结果是否符合预期
//将返回的响应结果字符串转化为json对象
JSONObject object = new JSONObject(result);
String name = object.getString("code");
Assert.assertEquals("success", name);
}
}