String uri=url+bundle.getString("postCookie.uri");
//生成httpclient对象
// CloseableHttpClient httpClient = HttpClientBuilder.create().build();
DefaultHttpClient httpClient = new DefaultHttpClient();
//创建post请求
HttpPost httpPost=new HttpPost(uri);
//使用json入参,入参是中文时不行。
JSONObject param=new JSONObject();
param.put("name","qinzhenxia");
param.put("age","28");
//设置请求头header
httpPost.setHeader("Content-Type","application/json;charset=gbk");
//传入参数
StringEntity entity=new StringEntity(param.toString(),"gbk");
httpPost.setEntity(entity);
//声明一个client对象,用来进行方法的执行,并设置cookies信息
CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(this.store).build();
//执行请求并接收返回结果
HttpResponse httpResponse = httpClient.execute(httpPost);
String result=EntityUtils.toString(httpResponse.getEntity());
//判断返回code值
int statuscode=httpResponse.getStatusLine().getStatusCode();
if (statuscode==200){
System.out.println(result);
} else {
System.out.println("访问/postwithcookiesandjsontwo接口失败");
}
//获取返回结果并转换为jsonobject格式,然后获取某个具体的值通过断言判断
JSONObject resultjson=new JSONObject(result);
String msg =resultjson.getString("msg");
//判断msg等于success
Assert.assertEquals(msg,"success");
JSONObject p2pdata=resultjson.getJSONObject("p2pdata");
String address=p2pdata.getString("address");
Assert.assertEquals(address,"北三环");
System.out.println(address);
}访问/postwithcookiesandjsontwo接口失败
2 回答
慕移动7568944
TA贡献1条经验 获得超0个赞
//DefaultHttpClient httpClient = new DefaultHttpClient();
上面这句注释掉
HttpResponse httpResponse = httpClient.execute(httpPost);
httpClient改成httpclient,即
HttpResponse httpResponse = httpclient.execute(httpPost);
添加回答
举报
0/150
提交
取消