Java 响应引发错误:服务器未识别HTTP标头SOAPAction的值
我正在尝试测试soap url。但每次发送soap请求时,我都会得到问题中提到的错误响应。我的代码如下所示:Java 响应引发错误:服务器未识别HTTP标头SOAPAction的值,java,spring,soap,Java,Spring,Soap,我正在尝试测试soap url。但每次发送soap请求时,我都会得到问题中提到的错误响应。我的代码如下所示: public static void main(String[] args) { try { SOAPConnectionFactory sfc = SOAPConnectionFactory.newInstance(); SOAPConnection connection = sfc.createConnection(); Me
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>
POST/LabelService/EwsLabelService.asmx HTTP/1.1
主机:elstestserver.endicia.com
内容类型:text/xml;字符集=utf-8
内容长度:长度
SOAPAction:“www.envmgr.com/LabelService/GetPostageLabelXML”
…一些xml
在这个问题上花了大约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>
soap:客户端
服务器未识别HTTP标头SOAPAction:的值。
如果发现此代码中有任何错误,请告诉我。我在我的
mimeHeaders.addHeader("SOAPAction", "http://www.envmgr.com/LabelService/GetPostageLabelXML");
当我删除http://后缀时,它工作得很好。所以改变的路线是
mimeHeaders.addHeader("SOAPAction", "www.envmgr.com/LabelService/GetPostageLabelXML");