我需要创建一个肥皂请求,以检查巫婆"增值税"。 通过使用此Web服务。这是我的代码
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
?还是我需要使用不同的方法来使用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");