java post cookie_http 带cookie值的Post请求(关联测试)

本文主要包含一下无点。

1、如何get请求如何获取cookie信息。   getCookieStore()

2、如何发送带cookie信息的post 请求。  setCookieStore(); HttpPost();

3、testng依耐测试,带cookie信息的post请求需要依耐于获取cookie信息的get请求。    @Test(dependsOnMethods = {"getTestCookie"})

4、如何使用moco框架构建带返回cookie信息的get请求和一个带cookie和json参数的post请求且响应结果也是json格式。

5、如何获取请求返回为json格式的参数。

6、如何对响应结果进行断言。

java代码

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 }

接口信息配置文件application.properties如下

1 test.url=http://127.0.0.1:8888

2 getCookies.uri=/getCookies3 testPostWithCookies.uri=/post/with/cookies

moco模拟接口信息如下

[

{"description": "这是一个会返回cookies信息的get请求","request": {"uri": "/getCookies","method": "get"},"response": {"headers": {"Content-Type": "text/html;charset=gbk"},"cookies": {"login": "true"},"text": "恭喜你获得cookies信息成功"}

},

{"description": "这是一个带cookies的post请求","request": {"uri": "/post/with/cookies","method": "post","cookies": {"login": "true"},"json": {"name": "zhangshan","age": "18"}

},"response": {"headers": {"Content-Type": "text/html;charset=gbk"},"status": 200,"json": {"zhangshan": "成功","status": 1}

}

}

]

原文:https://www.cnblogs.com/linxinmeng/p/12616497.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值