gpt4 book ai didi

java - SOAP REQUEST 到竞争服务

转载 作者:行者123 更新时间:2023-12-02 11:25:36 25 4
gpt4 key购买 nike

我需要创建一个 SOAP 请求来检查 this archive 中是否存在女巫“vat” 通过使用 this web service 。这是我的代码

  import org.apache.axis.AxisFault;
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceException;
import javax.xml.soap.*;

public class test_vies {

public static void main(String[] args) throws Exception {



String endpoint="http://ec.europa.eu/taxation_customs/vies/checkVatService";
String action="";
callSoapWebService(endpoint, action);
}
private static void createSoapEnvelope(SOAPMessage soapMessage) throws SOAPException {
SOAPPart soapPart = soapMessage.getSOAPPart();

String myNamespace = "urn";
String myNamespaceURI = "urn:ec.europa.eu:taxud:vies:services:checkVat:types";

// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration(myNamespace, myNamespaceURI);



// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement("countryCode", myNamespace);
SOAPElement soapBodyElem1 = soapBody.addChildElement("vatNumber", myNamespace);
soapBodyElem.addTextNode("IT");
soapBodyElem1.addTextNode("05006900962");
}

private static void callSoapWebService(String soapEndpointUrl, String soapAction) {
try {
// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();

// Send SOAP Message to SOAP Server
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(soapAction), soapEndpointUrl);

// Print the SOAP Response
System.out.println("Response SOAP Message:");
soapResponse.writeTo(System.out);
System.out.println();

soapConnection.close();
} catch (Exception e) {
System.err.println("\nError occurred while sending SOAP Request to Server!\nMake sure you have the correct endpoint URL and SOAPAction!\n");
e.printStackTrace();
}
}

private static SOAPMessage createSOAPRequest(String soapAction) throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();

createSoapEnvelope(soapMessage);

MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", soapAction);

soapMessage.saveChanges();

/* Print the request message, just for debugging purposes */
System.out.println("Request SOAP Message:");
soapMessage.writeTo(System.out);
System.out.println("\n");

return soapMessage;
}


}

即使 URL 正确,我仍然收到 404 响应。可能是什么问题? soapAction?或 pheraphs 我需要使用不同的方式来使用 WS?我对 java 还很陌生,我无法理解这个问题。

最佳答案

问题出在:

SOAPElement soapBodyElem = soapBody.addChildElement("countryCode", myNamespace);
SOAPElement soapBodyElem1 = soapBody.addChildElement("vatNumber", myNamespace);
soapBodyElem.addTextNode("IT");
soapBodyElem1.addTextNode("05006900962");

应该是:

    SOAPElement soapBodyElem = soapBody.addChildElement("checkVat", myNamespace);
SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("countryCode", myNamespace);
soapBodyElem1.addTextNode("IT");
SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("vatNumber", myNamespace);
soapBodyElem2.addTextNode("05006900962");

关于java - SOAP REQUEST 到竞争服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49650330/

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