一、Httpclient发送json请求

public String RequestJsonPost(String url){
    String strresponse = null;
    try{
        HttpClient hc = new DefaultHttpClient();
       HttpPost hp = new HttpPost(url);
       JSONObject jsonParam = new JSONObject();
       jsonParam.put("user","admin");
       jsonParam.put("password", "123456");
       //设置数据为utf-8编码
       StringEntity entity = new StringEntity(jsonParam.toString(),"utf-8");
       //设置请求编码
       entity.setContentEncoding("utf-8");
       //设置请求类型
       entity.setContentType("application/json");
       hp.setEntity(entity);
       //请求并得到结果
       HttpResponse result = hc.execute(hp);
       strresponse = EntityUtils.toString(result.getEntity(),"utf-8").trim();
    }catch(Exception e){
       e.printStackTrace();
}
return strresponse;
}

 

相关文章:

  • 2022-12-23
  • 2021-08-04
  • 2022-01-13
  • 2021-07-11
  • 2022-01-02
  • 2021-12-13
  • 2021-12-31
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-11-11
  • 2021-12-06
  • 2022-12-23
  • 2022-12-23
  • 2021-12-22
  • 2022-02-05
相关资源
相似解决方案