???String?uri=url+bundle.getString("postCookie.uri");
????//生成httpclient對象
???//?CloseableHttpClient?httpClient?=?HttpClientBuilder.create().build();
???DefaultHttpClient?httpClient?=?new?DefaultHttpClient();
????//創(chuàng)建post請求
????HttpPost?httpPost=new?HttpPost(uri);
????//使用json入?yún)?,入?yún)⑹侵形臅r不行。
????JSONObject?param=new?JSONObject();
????param.put("name","qinzhenxia");
????param.put("age","28");
????//設(shè)置請求頭header
????httpPost.setHeader("Content-Type","application/json;charset=gbk");
????//傳入?yún)?shù)
????StringEntity?entity=new?StringEntity(param.toString(),"gbk");
????httpPost.setEntity(entity);
????//聲明一個client對象,用來進行方法的執(zhí)行,并設(shè)置cookies信息
????CloseableHttpClient?httpclient?=?HttpClients.custom().setDefaultCookieStore(this.store).build();
????//執(zhí)行請求并接收返回結(jié)果
????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接口失敗");
????}
????//獲取返回結(jié)果并轉(zhuǎn)換為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,"北三環(huán)");
????System.out.println(address);
}訪問/postwithcookiesandjsontwo接口失敗
2 回答


慕移動7568944
TA貢獻1條經(jīng)驗 獲得超0個贊
//DefaultHttpClient httpClient = new DefaultHttpClient();
上面這句注釋掉
HttpResponse httpResponse = httpClient.execute(httpPost);
httpClient改成httpclient,即
HttpResponse httpResponse = httpclient.execute(httpPost);
?
添加回答
舉報
0/150
提交
取消