收藏
回答

微信支付 沙箱环境 请求API密钥 返回【请求参数xml格式错误】

请求的参数为:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xml>
<nonce_str>WSRQ7N7FFTVUG2DUSMY74C913XJK1ZNI</nonce_str>
<sign>3FB43D66CFD8111E007F8641E0907C89</sign>
<mch_id>1603461441</mch_id>  //为安全考虑,商户ID已做了更改
</xml>

返回的结果为:

<xml>  
<return_code><![CDATA[FAIL]]></return_code>  
<retmsg><![CDATA[输入请求参数xml格式错误]]></retmsg>  
<retcode><![CDATA[1]]></retcode>
</xml>

Map转xml的代码用的是官方jdk里的,如下

        //沙箱环境下需先请求获取key
  	Map testMap = new HashMap<>();
  	testMap.put("mch_id", mchId);
  	testMap.put("nonce_str",getRandomString());
  	//生成符合规格的签名串
  	String stringTestSignTemp = getParamStr(testMap);
  	//进行MD5运算,并转为大写,获得sign参数的值
  	String signTestValue = MD5(stringTestSignTemp.toString()).toUpperCase();
  	//把sign放入map中
  	testMap.put("sign",signTestValue);
  	//map转义为xml格式
  	String requestTestParam = mapToXml(testMap);
	LOG.info("request sandbox API key - [parameters]:" +requestTestParam);
	//发送请求
	String testResult = sendPostParam(sandBoxUrl, requestTestParam);
	LOG.info("request sandbox API key - [results]:" +testResult);

//Map转xml方法  
    public static DocumentBuilder newDocumentBuilder() throws ParserConfigurationException {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
        documentBuilderFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
        documentBuilderFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        documentBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        documentBuilderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
        documentBuilderFactory.setXIncludeAware(false);
        documentBuilderFactory.setExpandEntityReferences(false);


        return documentBuilderFactory.newDocumentBuilder();
    }

    public static String mapToXml(Map data) throws Exception {
        org.w3c.dom.Document document = newDocumentBuilder().newDocument();
        org.w3c.dom.Element root = document.createElement("xml");
        document.appendChild(root);
        for (String key: data.keySet()) {
            String value = data.get(key);
            if (value == null) {
                value = "";
            }
            value = value.trim();
            org.w3c.dom.Element filed = document.createElement(key);
            filed.appendChild(document.createTextNode(value));
            root.appendChild(filed);
        }
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        DOMSource source = new DOMSource(document);
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        transformer.transform(source, result);
        String output = writer.getBuffer().toString(); 
        try {
            writer.close();
        }
        catch (Exception ex) {
        }
        return output;
    }
最后一次编辑于  2020-10-29
回答关注问题邀请回答
收藏

3 个回答

  • 守在格林威治
    守在格林威治
    2020-10-30

    有没有好心人帮忙邀请一下官方技术

    2020-10-30
    有用 1
    回复
  • 郑能量
    郑能量
    2020-12-14

    这篇文章详细操作

    https://blog.csdn.net/weixin_44186072/article/details/111051118


    2020-12-14
    有用
    回复
  • peng
    peng
    2020-10-30
     standalone="no"  或者 <?xml version="1.0" encoding="UTF-8" standalone="no"?>干掉试试,不行再和我说
    
    2020-10-30
    有用
    回复 1
    • 守在格林威治
      守在格林威治
      2020-11-01
      2020-11-01
      回复
登录 后发表内容
问题标签