[英]Response throwing error: Server Did not recognize the value of HTTP Header SOAPAction
我正在嘗試測試肥皂網址。 但是我每次發送肥皂請求時都會收到問題中提到的錯誤響應。 我的代碼如下所示:
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>
如果此代碼中發現任何錯誤,請告訴我。
我在我的帳戶中添加了一個額外的“ http://”
mimeHeaders.addHeader("SOAPAction", "http://www.envmgr.com/LabelService/GetPostageLabelXML");
當我刪除http://后綴時,它工作正常。 所以改變的線是
mimeHeaders.addHeader("SOAPAction", "www.envmgr.com/LabelService/GetPostageLabelXML");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.