title: Sending SOAP request to a specific service
body: <p>Okay, so I'm totally new to webservices and for a project I'm working on I'm trying to wrap my head around the whole SOAP thing. I think I have a vague understanding of what is going on, but I'm missing some specific information and I simply can't find anything helpful by googling.</p>
  
  <p>I've read the questions others asked like this one <a href="https://stackoverflow.com/questions/19291283/soap-request-to-webservice-with-java">SOAP request to WebService with java</a> but I still can't fully figure out what is going on.</p>
  
  <p>Specifically I'm trying to use the service provided here <a href="http://ec.europa.eu/taxation_customs/vies/vatRequest.html" rel="nofollow noreferrer">http://ec.europa.eu/taxation_customs/vies/vatRequest.html</a> with its wsdl file here <a href="http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl" rel="nofollow noreferrer">http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl</a></p>
  
  <p>I tried adapting the example given in the above question, but I just can't figure out what values to add where so it works with this specific service. All I get is a "405 Method not allowed" response. Here is my attempted adaption:</p>
  
  <pre><code>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);
      }
  }
  </code></pre>
  
  <p>If someone could explain to me what exactly I'm doing wrong and how to fix it or maybe even give me a working example I'd be eternally grateful...</p>