为了账号安全,请及时绑定邮箱和手机立即绑定

httpclient调接口post出现无响应400

httpclient调接口post出现无响应400

qq_fly_5 2020-01-07 15:17:13
   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);

 

查看完整回答
反对 回复 2021-02-03
?
安浪创想

TA贡献81条经验 获得超23个赞

你用http 测试工具访问正常不正常再检查代码。也就是服务端接口正常了,才能说明你的客户端代码有问题。

查看完整回答
反对 回复 2020-01-28
  • 2 回答
  • 0 关注
  • 1600 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信