Httpclient 发送 post 请求封装json 格式参数

Httpclient 发送 post 请求可以封装多格式的参数,这篇我们封装json格式的:

1、客户端代码如下:

 /**
     * 参数以json形式传送
     * @param request
     * @return
     * @throws IOException
     */
    public NmpResponse doPostJson(NmpRequest request) throws IOException {
        logger.info("------------开始POST请求-nmpHttpClient-----------");

        HttpClient httpClient = new HttpClient();
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
        PostMethod postMethod = new PostMethod(NMP_ROOT_URL+request.getUrl());
        postMethod = setHeaderAuthInfo(postMethod);

        //添加请求参数
        String jsonParams = request.getJsonParam();
        System.out.println("推送参数:"+jsonParams);
        postMethod.setRequestEntity(new StringRequestEntity(jsonParams,null,null));
        NmpResponse response = new NmpResponse();

        try {
            logger.info("------------开始执行POST请求,url:{}------------",NMP_ROOT_URL+request.getUrl());
            int statusCode = httpClient.executeMethod(postMethod);
            logger.info("------------POST请求结束,code={}------------",statusCode);

            if (statusCode == 200){
                response.setRespBody(postMethod.getResponseBody());
                response.setResponseStr(postMethod.getResponseBodyAsString());
                response.setRespHeaders(postMethod.getRequestHeaders());
            } else{
                logger.error("请求出错code={}:{}" ,statusCode ,postMethod.getStatusLine());
            }
        } catch (IOException e){
            logger.error("请求出错IOException:{}" ,e);
            throw e;
        } finally {
            postMethod.releaseConnection();
            logger.info("------------结束POST请求-nmpHttpClient-----------");
        }
        return response;
    }

2、服务端接收代码:

@RequestMapping(value = "/download/getListData")
    public Object queryPartitionList(@RequestBody PartitionVO partitionVO){
        if(Objects.isNull(partitionVO) ||StringUtils.isEmpty(partitionVO.getLayName()) ||CollectionUtils.isEmpty(partitionVO.getParNameList())){
            logger.error("Method:queryPartitionList 入参不能为空");
            return new JSONObject();
        }
        if (StringUtils.isEmpty(partitionVO.getProductVersion())){
            partitionVO.setProductVersion(Constant.ProductVersionLastest);
        }
       JSONObject jsonObject = new JSONObject();
       //业务代码省略

        return jsonObject;
    }

3、服务端 partitionVO和客户端 jsonParams 数据里的参数一一对应。

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以使用HttpClient类来发送参数POST请求。以下是一个简单的示例代码: ``` import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; public class HttpClientExample { public static void main(String[] args) throws Exception { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost("http://example.com/api/post"); StringEntity entity = new StringEntity("param1=value1&param2=value2"); httpPost.setEntity(entity); httpPost.setHeader("Accept", "application/json"); httpPost.setHeader("Content-type", "application/x-www-form-urlencoded"); CloseableHttpResponse response = httpClient.execute(httpPost); HttpEntity responseEntity = response.getEntity(); System.out.println(EntityUtils.toString(responseEntity)); response.close(); httpClient.close(); } } ``` 在上面的代码中,我们创建了一个`CloseableHttpClient`对象,并使用`HttpPost`类来发送POST请求请求地址为`http://example.com/api/post`。我们使用`StringEntity`类来封装请求参数,并通过设置请求头来告诉服务器请求内容的类型。最后,我们使用`CloseableHttpResponse`对象获取服务器的响应,并通过`EntityUtils.toString`方法将响应内容转换为字符串。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

寅灯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值