【发布时间】:2016-09-20 14:19:51
【问题描述】:
好的,所以我对 Web 服务完全陌生,对于我正在从事的项目,我正试图围绕整个 SOAP 事物展开思考。我想我对正在发生的事情有一个模糊的了解,但我错过了一些具体的信息,我根本无法通过谷歌搜索找到任何有用的信息。
我已经阅读过其他人提出的类似SOAP request to WebService with java 的问题,但我仍然无法完全弄清楚发生了什么。
具体来说,我正在尝试使用http://ec.europa.eu/taxation_customs/vies/vatRequest.html 此处提供的服务及其 wsdl 文件http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl
我尝试调整上述问题中给出的示例,但我无法弄清楚要在何处添加哪些值,因此它适用于此特定服务。我得到的只是“405 Method not allowed”响应。这是我尝试的改编:
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);
}
}
如果有人可以向我解释我到底做错了什么以及如何解决它,或者甚至给我一个可行的例子,我将永远感激...
【问题讨论】:
标签: java web-services soap wsdl