java发送post请求 ,请求数据放到body里

java利用httpclient发送post请求 ,请求数据放到body里。

/**
     * post请求 ,请求数据放到body里
     *
     * @author lifq
     *
     *         2017年3月15日 下午3:47:04
     */
    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;
    }

 

限时领取免费Java相关资料,涵盖了Java、Redis、MongoDB、MySQL、Zookeeper、Spring Cloud、Dubbo/Kafka、Hadoop、Hbase、Flink等高并发分布式、大数据、机器学习等技术。

资料传送门:https://mp.weixin.qq.com/s/u2b_NVNuMuAPE0w4lc45fw

 

关注下方公众号即可免费领取:

Java碎碎念公众号

posted @ 2017-04-11 15:30  Java碎碎念  阅读(23821)  评论(1编辑  收藏  举报