java 微信分账POST请求 (java代码调用微信api)

今天用java 调用 微信分账api 由于上传的数据是xml 格式, 用post请求发送 。在网上找的现成函数,微信服务器老是返回签名错误,但是我用postman 发送返回的数据没有问题,于是经过自己探索,写了个测试可用的post 发送函数,和大家分享一下。

public static String httpPostRequest(String url, String requestStr) throws IOException {

        HttpClientBuilder httpClientBuilder = HttpClients.custom();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new StringEntity(new String(requestStr.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1)));
        try (CloseableHttpClient httpclient = httpClientBuilder.build()) {
            httpPost.setEntity(new StringEntity(new String(requestStr.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1)));
            try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
                String responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
                _log.info("\n【请求地址】:{}\n【请求数据】:\n{}\n【响应数据】:\n {}", url, requestStr, responseString);
                return responseString;
            }
        }catch (Exception e){
            _log.info("\n【请求地址】:{}\n【请求数据】:\n{}\n【异常信息】:\n{}\n", url, requestStr, e.getMessage());
        }
        finally {
            httpPost.releaseConnection();
        }

        return null;
    }

微信服务器返回的数据如下

<xml>
<return_code><![CDATA[SUCCESS]]></return_code>
<result_code><![CDATA[SUCCESS]]></result_code>
<mch_id><![CDATA[xxxxxxx]]></mch_id>
<appid><![CDATA[xxxxxxxxx]]></appid>
<receiver><![CDATA[{"type":"PERSONAL_WECHATID","account":"xxxx","relation_type":"PARTNER"}]]></receiver>
<nonce_str><![CDATA[bfec794fe03a30b4]]></nonce_str>
<sign><![CDATA[31528954CE0A3CAAE3725AC7BF3CD5F49D604E0DF4F715383F6202B80232D7A0]]></sign>
</xml>
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值