码迷,mamicode.com
首页 > 微信 > 详细

httpclient 发送 json数据,微信security.msgSecCheck,

时间:2020-11-11 15:44:03      阅读:19      评论:0      收藏:0      [点我收藏+]

标签:tpc   ash   发送   entity   system   post请求   jdk   必须   ==   

在微信中,发送httpclient请求

使用了apache的组件

/**
     * post请求
     * @param url
     * @param json
     * @return
     */
    public static JSONObject doPost(String url,JSONObject json){
        
        CloseableHttpClient httpclient = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost(url);
        JSONObject response = null;
        try {
            StringEntity s = new StringEntity(json.toString(),"UTF-8");
            s.setContentEncoding("UTF-8");
            s.setContentType("application/json");//发送json数据需要设置contentType
            post.setEntity(s);
            System.out.println(post);
            HttpResponse res = httpclient.execute(post);
            if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                String result = EntityUtils.toString(res.getEntity());// 返回json格式:
                response = JSONObject.fromObject(result);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return response;
    }
    

 

需要引入pom

<dependency>
  <groupId>net.sf.json-lib</groupId>
  <artifactId>json-lib</artifactId>
  <version>2.4</version>
  <!-- 必须加jdk的版本号 -->
  <classifier>jdk15</classifier>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>

 

然后创建一个jsonobject传入

Map<String, String> contentmap = new HashMap<String, String>();

contentmap.put("content", "xidada");

JSONObject json = JSONObject.fromObject(contentmap);

System.out.println("json is = "+json);
String msg = doPost(url, json).toString();

 

httpclient 发送 json数据,微信security.msgSecCheck,

标签:tpc   ash   发送   entity   system   post请求   jdk   必须   ==   

原文地址:https://www.cnblogs.com/sdgtxuyong/p/13763592.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!