1、调用第三方webservice服务时使用postman一直400
之前一直使用axis自动生成请求第三方的接口代码,这次想使用postman调用试试,结果接口一直400。
对方提供的示例:
POST /Service1.asmx HTTP/1.1
Host: IP
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://uri.org/Pay"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Pay xmlns="http://tempuri.org/">
<msg>
<xml>
<body>充值</body>
<fee>1.00</fee>
<code>000000000000</code>
<out_trade_no>20220000001</out_trade_no>
</xml>
</msg>
</Pay>
</soap:Body>
</soap:Envelope>
结果接口一直400:
2、后来想到转义符的问题,使用转义符占位修改后:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Pay xmlns="http://tempuri.org/">
<msg><xml>
<body>充值</body>
<fee>1.00</fee>
<code>000000000000</code>
<out_trade_no>200011000000</out_trade_no>
</xml></msg>
</Pay>
</soap:Body>
</soap:Envelope>
这次成功返回结果:
3、后来了解到在xml中英文问号“?”是可以被正常解析的,但是以下这几种符号是不能正常解析的:分别是“&”、“<”、“>”、“’”、“””。“<” 会产生错误,因为解析器会把该字符解释为新元素的开始。“&” 也会产生错误,因为解析器会把该字符解释为字符实体的开始。
解决方法:
1、上面的几种字符修改为转义符,具体对应的转义符可以百度搜索,示例如上。
2、使用<![CDATA[内容]]>,CDATA DTD的属性类型,全名character data,指不由xml解析器进行解析的文本数据。在标记的CDATA下,所有的标记、实体引用(特殊字符)都会被忽略,而被当作字符数据来看待。
示例:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Pay xmlns="http://tempuri.org/">
<msg><![CDATA[<xml><body>微信条码付就诊卡充值</body><fee>1.00</fee><code>0000000000000</code><out_trade_no>20010000000</out_trade_no></xml>]]></msg>
</Pay>
</soap:Body>
</soap:Envelope>
结果: