httpClient发送post请求,传递json字符串

来源:互联网 发布:易打单软件 编辑:程序博客网 时间:2024/05/20 08:45

传文件的已经写了,传json字符串的稍微简单点。

  public static String doPostJson(String url, String json,Log logger) {        // 创建Httpclient对象        CloseableHttpClient httpClient = HttpClients.createDefault();        CloseableHttpResponse response = null;        String resultString = "";        try {            // 创建Http Post请求            HttpPost httpPost = new HttpPost(url);            // 创建请求内容            StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);            httpPost.setEntity(entity);            // 执行http请求            response = httpClient.execute(httpPost);            resultString = EntityUtils.toString(response.getEntity(), "utf-8");        } catch (Exception e) {            logger.error("端口数据获取Bug:"+e);            e.printStackTrace();        } finally {            try {                response.close();            } catch (IOException e) {                e.printStackTrace();            }        }        return resultString;    }

原创粉丝点击