gpt4 book ai didi

java - 未调用 SOAPMessage.writeTo 时 SOAP 服务调用失败

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:01:56 27 4
gpt4 key购买 nike

我正在尝试调用一个 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;
}
}

createSOAPRequest 中的 soapMessage.writeTo(System.out)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>

最佳答案

通过查看 writeTo 方法的实现,我发现他们在其末尾设置了“SOAPAction” header :

if (isCorrectSoapVersion(4)) {
String[] soapAction = this.headers.getHeader("SOAPAction");

if ((soapAction == null) || (soapAction.length == 0)) {
this.headers.setHeader("SOAPAction", "\"\"");
}
}

因此,如果您想避免调用 writeTo 方法,您实际上可以在创建 SOAPMessage 后立即自行设置 header :

SOAPMessage soapMessage = MessageFactory.newInstance().createMessage(
null, is);
soapMessage.getMimeHeaders().addHeader("SOAPAction", "\"\"");

这对我有用。希望对您有所帮助!

皮埃尔

关于java - 未调用 SOAPMessage.writeTo 时 SOAP 服务调用失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27865079/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com