我正在尝试使用以下 SOAPClient for Java(我从以下教程http://users.skynet.be/pascalbotte/rcx-ws-doc/saajpost.htm获得的详细信息)。
但是,它似乎引发了异常。
这是我的代码:
javax.xml.soap.SOAPMessage msg;
MessageFactory mf = MessageFactory.newInstance();
msg = mf.createMessage();
SOAPPart part = msg.getSOAPPart();
StreamSource source = new StreamSource(new File("samples/input1.xml”));
part.setContent(source);
msg.saveChanges();
String endpoint = "http://ws1.parasoft.com/glue/calculator";
SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
SOAPConnection conn = scf.createConnection();
javax.xml.soap.SOAPMessage response = conn.call(msg, endpoint);
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();
Source sc = response.getSOAPPart().getContent();
ByteArrayOutputStream ostream = new ByteArrayOutputStream();
StreamResult result = new StreamResult(ostream);
tf.transform(sc, result);
conn.close();
System.out.println(new String(ostream.toByteArray(), "UTF-8”));
在此示例中,我们假设samples/input1.xml
拥有以下内容:
<s11:Envelope xmlns:s11="http://schemas.xmlsoap.org/soap/envelope/">
<s11:Body>
<ns1:add xmlns:ns1="http://www.parasoft.com/wsdl/calculator/">
<ns1:x>248</ns1:x>
<ns1:y>365</ns1:y>
</ns1:add>
</s11:Body>
</s11:Envelope>
我正在尝试使用的示例网络服务可以在这里找到: http ://www.service-repository.com/client/operations
运行上述 Java 代码(使用 SOAPClient 库)时,会引发以下异常:
Jul 19, 2012 3:50:11 AM com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection post
SEVERE: SAAJ0008: Bad Response; cannot find /calculator
Exception in thread "main" com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Bad response: (404cannot find /calculator
at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:148)
at corebus.test.deprecated.TestMain.main(TestMain.java:1870)
Caused by: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Bad response: (404cannot find /calculator
at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:258)
at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:144)
... 1 more
CAUSE:
com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Bad response: (404cannot find /calculator
at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:258)
at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:144)
使用此网站,服务似乎已启动并运行,并且运行良好。
我什至通过执行一个简单的 cURL 请求验证了服务端点是活动的,这令人惊讶地产生了正确的输出。
curl -H "Content-Type: text/xml; charset=utf-8" -H "SOAPAction:http://www.parasoft.com/wsdl/calculator/" -d@soap-request.xml http://ws1.parasoft.com/glue/calculator
产生的输出是:
<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>
<n:addResponse xmlns:n='http://www.parasoft.com/wsdl/calculator/‘>
<n:Result xsi:type='xsd:float'>613.0</n:Result>
</n:addResponse>
</soap:Body>
</soap:Envelope>
所以,我的问题是:首先,Java 代码有什么问题?它怎么能被修复?或者,还有其他更好/更可靠的通用 SOAPClient 库可以推荐吗?