httpclient调接口post出现无响应400
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接口失败 |
免责声明:本内容仅代表回答会员见解不代表天盟观点,请谨慎对待。
版权声明:作者保留权利,不代表天盟立场。
|
|
|
|
//DefaultHttpClient httpClient = new DefaultHttpClient();
上面这句注释掉
HttpResponse httpResponse = httpClient.execute(httpPost);
httpClient改成httpclient,即
HttpResponse httpResponse = httpclient.execute(httpPost); |
|
|
|
|
你用http 测试工具访问正常不正常再检查代码。也就是服务端接口正常了,才能说明你的客户端代码有问题。 |
|
|
|
|