好吧,所以我完全不熟悉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