我正在尝试调用 SOAP 服务但是在我构建了SOAPMessage
之后,如果我调用SOAPMessage.writeTo(out)
服务调用成功完成但是当我省略它时失败。
我非常确定在发送请求之前调用writeTo()
不是必须的步骤,我做错了。
有什么想法吗?
以下是详细信息
我的客户
public class Test {
public static void main(String args[]) throws Exception {
// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
// Send SOAP Message to SOAP Server
String url = "https://mydomain.com/webservices/gateway/IdMgt/CorporateDirectoryLookupPort";
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
// print SOAP Response
soapResponse.writeTo(System.out);
soapConnection.close();
}
private static SOAPMessage createSOAPRequest() throws Exception {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
+ "<soapenv:Header>"
+ "<ns1:Security soapenv:mustUnderstand=\"0\" xmlns:ns1=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">"
+ "<ns1:UsernameToken>"
+ "<ns1:Username></ns1:Username>"
+ "<ns1:Password></ns1:Password>"
+ "</ns1:UsernameToken>"
+ "</ns1:Security>"
+ "</soapenv:Header>"
+ "<soapenv:Body>"
+ "<GetAccountDetailsRequest2 xmlns=\"http://anotherdomain/schema/tCorporateDirectoryLookupV1\">"
+ "<MessageHeader xmlns=\"\"/><UserID xmlns=\"\"></UserID>"
+ "<AccountID xmlns=\"\">ServiceDeskAPIprd</AccountID>"
+ "</GetAccountDetailsRequest2></soapenv:Body>"
+ "</soapenv:Envelope>";
InputStream is = new ByteArrayInputStream(xml.getBytes());
SOAPMessage soapMessage = MessageFactory.newInstance().createMessage(
null, is);
/* Print the request message */
soapMessage.writeTo(System.out);
System.out.println();
return soapMessage;
}
}
当soapMessage.writeTo(System.out)
中的createSOAPRequest
NOT 被注释掉时,我会得到一个有效的回复,但当它被注释掉时我会得到
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<soapenv:Fault>
<faultcode xmlns:ns1="http://xml.apache.org/axis/">ns1:Client.NoSOAPAction</faultcode>
<faultstring>no SOAPAction header!</faultstring>
<detail>
<ns2:hostname xmlns:ns2="http://xml.apache.org/axis/">lxvirt150</ns2:hostname>
</detail>
</soapenv:Fault>
</soapenv:Body>
</soapenv:Envelope>
答案 0 :(得分:1)
通过查看writeTo方法的实现,我发现他们设置了&#34; SOAPAction&#34;标题末尾:
if (isCorrectSoapVersion(4)) {
String[] soapAction = this.headers.getHeader("SOAPAction");
if ((soapAction == null) || (soapAction.length == 0)) {
this.headers.setHeader("SOAPAction", "\"\"");
}
}
因此,如果您想避免调用writeTo方法,您可以在创建SOAPMessage之后立即自己设置标头:
SOAPMessage soapMessage = MessageFactory.newInstance().createMessage(
null, is);
soapMessage.getMimeHeaders().addHeader("SOAPAction", "\"\"");
这对我有用。我希望有所帮助!
皮尔
答案 1 :(得分:0)
我遇到了同样的问题,但发现saveChanges()方法解决了它。来自java doc:
Updates this SOAPMessage object with all the changes that
have been made to it. This method is called automatically when
SOAPMessage writeTo(OutputStream) is called. However, if
changes are made to a message that was received or to one that has
already been sent, the method saveChanges needs to be
called explicitly in order to save the changes. The method saveChanges also generates any changes that can be read back (for example, a MessageId in profiles that support a message id). All MIME headers in a message that is created for sending purposes are guaranteed to have valid values only after saveChanges has been called.
In addition, this method marks the point at which the data from all
constituent AttachmentPart objects are pulled into the
message.