好吧,所以我完全不熟悉web服务和我正在开发的项目,我正在试图围绕整个SOAP事物进行包装。我认为我对发生的事情有一个模糊的理解,但我错过了一些具体的信息,而且我根本找不到任何有助于Google搜索的东西。发送SOAP请求到特定的服务
我读过其他人问这样的问题SOAP request to WebService with java,但我仍然无法完全弄清楚发生了什么。
具体我试图使用与这里的WSDL文件http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl
我试图适应在对上述问题给出的例子在这里http://ec.europa.eu/taxation_customs/vies/vatRequest.html提供的服务,但我想不出什么值增加地方所以它适用于这个特定的服务。我所得到的是一个“405方法不允许”的回应。这是我试图适应:
package at.kmds.soaptest;
import javax.xml.soap.*;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
public class Main {
public static void main(String args[]) {
try {
// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
// Send SOAP Message to SOAP Server
String url = "http://ec.europa.eu/taxation_customs/vies/vatRequest.html";
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
// Process the SOAP Response
printSOAPResponse(soapResponse);
soapConnection.close();
} catch (Exception e) {
System.err.println("Error occurred while sending SOAP Request to Server");
e.printStackTrace();
}
}
private static SOAPMessage createSOAPRequest() throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
String serverURI = "http://ec.europa.eu/";
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("example", serverURI);
// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement("checkVat");
SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("countryCode");
soapBodyElem1.addTextNode("...");
SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("vatNumber");
soapBodyElem2.addTextNode("...");
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", serverURI + "checkVat");
soapMessage.saveChanges();
/* Print the request message */
System.out.print("Request SOAP Message = ");
soapMessage.writeTo(System.out);
System.out.println();
return soapMessage;
}
private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
Source sourceContent = soapResponse.getSOAPPart().getContent();
System.out.print("\nResponse SOAP Message = ");
StreamResult result = new StreamResult(System.out);
transformer.transform(sourceContent, result);
}
}
如果有人能向我解释,究竟我做错了,如何解决这个问题,或者甚至给我一个工作的例子,我会感激不尽......
太感谢你了,现在的工作!你认为你可以对ELI5如此善良,你怎么知道哪些值在哪里使用?或者你知道一个解释这个东西的教程吗?我从来不会去找到这个...... – Szernex
很高兴帮助。我刚刚提到了https://docs.oracle.com/cd/E19879-01/819-3669/bnbhw/index.html。我基本上复制粘贴你的代码。通过在SOAP UI工具上生成示例SOAP请求,我发现名称空间不正确。我认为需要添加命名空间,我只是这样做了。 –
好的,我会再看一下,再次感谢! – Szernex