SOAP REQUEST来争夺服务

时间:2018-04-04 11:53:14

标签: java soap

我需要创建一个SOAP请求来检查女巫" vat"存在于this archive  使用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;
}


}

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

1 个答案:

答案 0 :(得分:1)

问题出在:

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");