Post接口的请求。

时间:2021-02-27 13:06:24   收藏:0   阅读:0

接口地址

String url = "http://testdplan.xaltserver.com/login";

请求方式:post

HttpPost post = new HttpPost(url);

准备测试数据

String mobilephone = "1333333333";

String pwd = "123456";

将测试数据存储到泛型为BasicNameValuePair的集合中,BasicNameValuePair是存储键值对的类

List <BasicNameValuePair> parameters = new ArrayList<>(BasicNameValuePair);

parameters.add(new BasicNameValuePair("mobilephone", mobilephone));

parameters.add(new BasicNameValuePair("pwd", pwd));

将参数设置到要发送的表单中,UrlEncodedFormEntity这个类是用来把输入数据编码成合适的内容

post.setEntity(new UrlEncodedFormEntity(parameters, "utf-8"));

创建一个默认的客户端

HttpClient client = HttpClients.createDefault();

用这个创建好的客户端发送请求(相当于我们在用jmeter或者postman时点击运行按钮)

HttpResponse httpResponse = client.execute(post);

获取状态码

int code = httpResponse.getStatusLine().getStatusCode();

System.out.println(code);

 获取响应报文。httpResponse.getEntity()获取响应报文为json格式的,通过工具类EntityUtils中的toString方法转换为字符串格式

String result = EntityUtils.toString(httpResponse.getEntity());

System.out.println(result);

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!