JAVA利用HttpClient发送post请求,将请求数据放到body里

    /**
     * post请求 ,请求数据放到body里
     * @param url    请求地址
     * @param bodyData  参数
     * @author wangyj
     * @date 2019年4月20日
     */
    public static String doPostBodyData(String url, String bodyData) throws Exception{
        String result = "";
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        try {
            HttpPost httpPost = getHttpPost(url, null); // 请求地址
            httpPost.setEntity(new StringEntity(bodyData, Encoding));
            httpClient = getHttpClient();
            // 得到返回的response
            response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            result = getResult(entity, Encoding);
        } catch (Exception e) {
            throw e;
        } finally {
            // 关闭httpClient
            if (null != httpClient) {
                httpClient.close();
            }
            // 关闭response
            if (null != response) {
                EntityUtils.consume(response.getEntity()); // 会自动释放连接
                response.close();
            }
        }
        return result;
    }

 

你可能感兴趣的:(工具类整理,HttpClient,post,body)