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

本文提供了一个使用Java的HttpClient库发送POST请求的例子,详细展示了如何将请求数据放入请求体中,并通过示例代码介绍了具体的实现步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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碎碎念公众号

转载于:https://www.cnblogs.com/haha12/p/6693754.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值