收藏
回答

谁能给个微信退款的示例代码,httpclient不知道怎么提交退款的XML数据

ByteArrayInputStream isc = new ByteArrayInputStream(xml.getBytes());
//HttpEntity e = new InputStreamEntity(isc,ContentType.APPLICATION_XML);
//post.setEntity(e);
post.setEntity(new ByteArrayEntity(xml.getBytes()));


xml是退款参数

InputStreamEntity 和 ByteArrayEntity都试过,一直返回


<xml><return_code><![CDATA[FAIL]]></return_code>
<return_msg><![CDATA[缺少参数]]></return_msg>
</xml>


下面的代码基本是抄官网的demo(官网也不提示的完整一些是怎么提交xml,还有到处查资料)

/**

     * 执行请求
     * @param xml 封装的参数
     * @return 返回执行结果
     */
    private String doSslRequest(String xml) {
        // Trust own CA and all self-signed certs
        CloseableHttpClient httpclient = null;
        CloseableHttpResponse response = null;
        HttpEntity httpEntity = null;
        InputStreamReader entityStream = null;
        try {
            char[] mchid = AppConfig.getPaymchId().toCharArray();
            KeyStore keyStore = getKeyStore(mchid);
            SSLContext sslcontext = SSLContexts.custom()
                    .loadKeyMaterial(keyStore, mchid).build();
            // Allow TLSv1 protocol only
            String[] tlsv1 = { "TLSv1" };
            HostnameVerifier hostname = SSLConnectionSocketFactory.getDefaultHostnameVerifier();
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                    sslcontext, tlsv1, null, hostname);
            httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
 
            // HttpGet httpget = new HttpGet(D_URL_PAYREFUND);
            HttpPost post = new HttpPost(D_URL_PAYREFUND);
            ByteArrayInputStream isc = new ByteArrayInputStream(xml.getBytes());
            //HttpEntity e = new InputStreamEntity(isc,ContentType.APPLICATION_XML);
            //post.setEntity(e);
            post.setEntity(new ByteArrayEntity(xml.getBytes())); 
            response = httpclient.execute(post);
            httpEntity = response.getEntity();
 
            log.debug("----------------------------------------");
            log.debug(response.getStatusLine().toString());
            if (httpEntity == null) {
                log.error("请求返回null");
                return null;
            }
 
            log.debug("Response content length: "
                    + httpEntity.getContentLength());
            entityStream = new InputStreamReader(httpEntity.getContent(),
                    Tools.encode.UTF8);
            char[] buf = new char[1024 * 16];
            StringBuilder responseText = new StringBuilder();
            int l;
            while ((l = entityStream.read(buf)) != -1) {
                responseText.append(buf, 0, l);
            }
 
            return responseText.toString();
        } catch (Exception e) {
            log.error("微信支付ssl请求出错", e);
            throw new YMException("微信支付ssl请求出错:" + e.getMessage());
        } finally {
            Tools.io.close(entityStream);
            close(httpEntity);
            Tools.io.close(response);
            Tools.io.close(httpclient);
        }
    }


回答关注问题邀请回答
收藏

1 个回答

  • 千年不变,万年如一🎾
    千年不变,万年如一🎾
    2018-08-10

    补充下XML的内容

    <xml><appid><![CDATA[是正确的]]></appid>
    <mch_id><![CDATA[是正确的]]></mch_id>
    <nonce_str><![CDATA[201808101626400971]]></nonce_str>
    <notify_url><![CDATA[https://www.netsupper.top/print/wx/payment/paynotify/refund]]></notify_url>
    <out_refund_no><![CDATA[188AG2640X660000]]></out_refund_no>
    <out_trade_no><![CDATA[188AF2642X660000]]></out_trade_no>
    <refund_desc><![CDATA[退还赏金]]></refund_desc>
    <refund_fee><![CDATA[1]]></refund_fee>
    <sign_type><![CDATA[MD5]]></sign_type>
    <total_fee><![CDATA[1]]></total_fee>
    <transaction_id><![CDATA[]]></transaction_id>
    </xml>


    2018-08-10
    有用
    回复
登录 后发表内容