请求的参数为:
<?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;
}
有没有好心人帮忙邀请一下官方技术
这篇文章详细操作
https://blog.csdn.net/weixin_44186072/article/details/111051118
standalone="no" 或者 <?xml version="1.0" encoding="UTF-8" standalone="no"?>干掉试试,不行再和我说
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xml>
<nonce_str>88IJ2HHEEQ9F3WK624ZKGW73994XVWBV</nonce_str>
<sign>2F7BA92DD089D3CDA7AECD28731906F7</sign>
<mch_id>1603461445</mch_id>
</xml>
<retmsg><![CDATA[输入请求参数xml格式错误]]></retmsg>
还是格式错误,我直接用字符串拼接的话不会报格式错误,但显示[请确认请求参数是否正确param mch_id invalid]