我正在尝试测试一个肥皂网址。但是每次发送soap请求时,我都会收到问题中提到的错误响应。我的代码如下所示:
public static void main(String[] args) {
try {
SOAPConnectionFactory sfc = SOAPConnectionFactory.newInstance();
SOAPConnection connection = sfc.createConnection();
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage sm = mf.createMessage();
SOAPPart soapPart = sm.getSOAPPart();
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");
envelope.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");
SOAPHeader sh = sm.getSOAPHeader();
SOAPBody sb = sm.getSOAPBody();
sh.detachNode();
QName postageLabelXML = new QName("www.envmgr.com/LabelService", "GetPostageLabelXML");
SOAPBodyElement bodyElement = sb.addBodyElement(postageLabelXML);
QName qn = new QName("LabelRequestXML");
SOAPElement quotation = bodyElement.addChildElement(qn);
QName qnLabelRequest = new QName("LabelRequest");
SOAPElement qnLabelRequestQuotation = quotation.addChildElement(qnLabelRequest);
MimeHeaders mimeHeaders = sm.getMimeHeaders();
mimeHeaders.addHeader("Host", "https://elstestserver.endicia.com");
mimeHeaders.addHeader("Content-Length", "65536");
mimeHeaders.addHeader("Content-Type", "text/xml; charset=utf-8");
mimeHeaders.addHeader("SOAPAction", "http://www.envmgr.com/LabelService/GetPostageLabelXML");
System.out.println("\n Soap Request:\n");
sm.writeTo(System.out);
System.out.println();
URL endpoint = new URL("https://elstestserver.endicia.com/LabelService/EwsLabelService.asmx");
SOAPMessage response = connection.call(sm, endpoint);
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.writeTo(out);
String strMsg = new String(out.toByteArray());
System.out.println(response.getContentDescription());
System.out.println("xml -= " + strMsg);
} catch (Exception ex) {
ex.printStackTrace();
}
}
上面的代码生成以下xml作为请求:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Body>
<GetPostageLabelXML xmlns="www.envmgr.com/LabelService">
<LabelRequestXML>
<LabelRequest />
</LabelRequestXML>
</GetPostageLabelXML>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
所需的xml是:
POST /LabelService/EwsLabelService.asmx HTTP/1.1
Host: elstestserver.endicia.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "www.envmgr.com/LabelService/GetPostageLabelXML"
<?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>
<GetPostageLabelXML xmlns="www.envmgr.com/LabelService">
<LabelRequestXML>
<LabelRequest>...some xml </LabelRequest>
</LabelRequestXML>
</GetPostageLabelXML>
</soap:Body>
</soap:Envelope>
在此问题上花了大约2个小时后,我无法猜出此错误响应背后的真正问题:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<soap:Fault>
<faultcode>soap:Client</faultcode>
<faultstring>Server did not recognize the value of HTTP Header SOAPAction: .</faultstring>
<detail />
</soap:Fault>
</soap:Body>
</soap:Envelope>
如果此代码中发现任何错误,请与我们联系。
答案 0 :(得分:0)
我在
中添加了额外的“http://”mimeHeaders.addHeader("SOAPAction", "http://www.envmgr.com/LabelService/GetPostageLabelXML");
当我删除http://后缀时,它工作正常。所以改变的行是
mimeHeaders.addHeader("SOAPAction", "www.envmgr.com/LabelService/GetPostageLabelXML");