title: 400Bad Request When palcing SOAP API Call
body: <p>I am having trouble placing an call to a SOAP API. I receive a 400 Bad Request message. I am unsure of why I am getting this bad response. I am following API documentation obtained directly by the software company. </p>
  
  <p>My Test Class:</p>
  
  <pre><code>import javax.xml.namespace.QName;
  import javax.xml.soap.MessageFactory;
  import javax.xml.soap.MimeHeader;
  import javax.xml.soap.MimeHeaders;
  import javax.xml.soap.SOAPBody;
  import javax.xml.soap.SOAPConnection;
  import javax.xml.soap.SOAPConnectionFactory;
  import javax.xml.soap.SOAPConstants;
  import javax.xml.soap.SOAPElement;
  import javax.xml.soap.SOAPEnvelope;
  import javax.xml.soap.SOAPException;
  import javax.xml.soap.SOAPHeader;
  import javax.xml.soap.SOAPMessage;
  import javax.xml.soap.SOAPPart;
  
  public class TestClass {
  
  
      public static void main(String args[]) {
          String soapEndpointUrl = "https://telehealth.foracare.com/WebService/WS_DataInterchangeService.asmx";
          String soapAction = "http://www.tdcare.com/DataInterchange";
          callSoapWebService(soapEndpointUrl, soapAction);
      }
  
      private static SOAPMessage createSoapEnvelope(SOAPMessage soapMessage) throws SOAPException {
          SOAPPart soapPart = soapMessage.getSOAPPart();
  
          String targetNamespace = "http://www.tdcare.com/";
  
          String xsi = "xsi";
          String xsiURI = "http://www.w3.org/2001/XMLSchema-instance";
  
          String xsd = "xsd";
          String xsdURI = "http://www.w3.org/2001/XMLSchema";
  
          // SOAP Envelope
          SOAPEnvelope envelope = soapPart.getEnvelope();
          envelope.addNamespaceDeclaration(xsi, xsiURI);
          envelope.addNamespaceDeclaration(xsd, xsdURI);
  
  
  
          //SOAP Header
          SOAPHeader soapHead = envelope.getHeader();
          QName qname = new QName(targetNamespace, "sValidationSoapHeader", new String());
          SOAPElement soapHeadElem = soapHead.addChildElement(qname);
          SOAPElement soapHeadElem1 =  soapHeadElem.addChildElement("Username");
          soapHeadElem1.addTextNode("****");
          SOAPElement soapHeadElem2 =  soapHeadElem.addChildElement("Password");
          soapHeadElem2.addTextNode("*****");
  
          // SOAP Body
          SOAPBody soapBody = envelope.getBody();
          QName dataInterchangeQName = new QName(targetNamespace, "DataInterchange", new String());
          SOAPElement dataInterchange = soapBody.addChildElement(dataInterchangeQName);
          SOAPElement iCMDType = dataInterchange.addChildElement("iCMDType");
          iCMDType.addTextNode("Q0001");
          SOAPElement iData = dataInterchange.addChildElement("iData");
          SOAPElement queryData = iData.addChildElement("QueryData");
          SOAPElement account = queryData.addChildElement("Account");
          account.addTextNode("*****");
          SOAPElement password = queryData.addChildElement("Password");
          password.addTextNode("******");
          SOAPElement qSDate = queryData.addChildElement("QSDate");
          qSDate.addTextNode("2010/01/01");
          SOAPElement qEDate = queryData.addChildElement("QEDate");
          qEDate.addTextNode("2010/02/01");
          SOAPElement qMType = queryData.addChildElement("QMType");
          qMType.addTextNode("1");
          SOAPElement qCase = queryData.addChildElement("QCase");
          return soapMessage;
      }
  
      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(SOAPConstants.SOAP_1_1_PROTOCOL);
          SOAPMessage soapMessage = messageFactory.createMessage();
  
          soapMessage =  createSoapEnvelope(soapMessage);
  
          MimeHeaders headers = soapMessage.getMimeHeaders();
          headers.addHeader("Host", "telehealth.foracare.com");
          headers.addHeader("SOAPAction", soapAction);
          headers.setHeader("Content-Type", "text/xml; charset=utf-8");
          soapMessage.saveChanges();
  
          Iterator i = headers.getAllHeaders();
          while(i.hasNext()) {
              MimeHeader header = (MimeHeader)i.next();
             System.err.println(header.getName() + " : "+ header.getValue());
          }
  
          /* Print the request message, just for debugging purposes */
          System.out.println("Request SOAP Message:");
          soapMessage.writeTo(System.out);
          System.out.println("\n");
  
          return soapMessage;
      }
  
  
  
  }
  </code></pre>
  
  <p>This is a sample XML request that I am attempting to follow:</p>
  
  <p><a href="https://i.stack.imgur.com/hhHGP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hhHGP.png" alt="enter image description here"></a></p>
  
  <p>And this is the message that my code constructs:</p>
  
  <pre><code> /*
                  Constructed SOAP Request Message:
  
                  &lt;SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
                         xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
                         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
                      &lt;SOAP-ENV:Header&gt;
                          &lt;sValidationSoapHeader xmlns="http://www.tdcare.com/"&gt;
                              &lt;Username&gt;*****&lt;/Username&gt;
                              &lt;Password&gt;*****&lt;/Password&gt;
                          &lt;/sValidationSoapHeader&gt;
                      &lt;/SOAP-ENV:Header&gt;
                      &lt;SOAP-ENV:Body&gt;
                          &lt;DataInterchange xmlns="http://www.tdcare.com/"&gt;
                              &lt;iCMDType&gt;Q0001&lt;/iCMDType&gt;
                              &lt;iData&gt;
                                  &lt;QueryData&gt;
                                      &lt;Account&gt;*****&lt;/Account&gt;
                                      &lt;Password&gt;*****&lt;/Password&gt;
                                      &lt;QSDate&gt;2010/01/01&lt;/QSDate&gt;
                                      &lt;QEDate&gt;2010/02/01&lt;/QEDate&gt;
                                      &lt;QMType&gt;1&lt;/QMType&gt;
                                      &lt;QCase/&gt;
                                  &lt;/QueryData&gt;
                              &lt;/iData&gt;
                          &lt;/DataInterchange&gt;
                      &lt;/SOAP-ENV:Body&gt;
                  &lt;/SOAP-ENV:Envelope&gt;
                  */
  </code></pre>
  
  <p>Below is the trace and error messages:</p>
  
  <pre><code>Apr 25, 2018 12:06:51 AM com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection post
  SEVERE: SAAJ0008: Bad Response; Bad Request
  
  Error occurred while sending SOAP Request to Server!
  Make sure you have the correct endpoint URL and SOAPAction!
  
  com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Bad response: (400Bad Request
      at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:149)
      at com.hersa.foraclient.TestClass.callSoapWebService(TestClass.java:131)
      at com.hersa.foraclient.TestClass.main(TestClass.java:39)
  Caused by: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Bad response: (400Bad Request
      at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:264)
      at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:145)
      ... 2 more
  
  CAUSE:
  
  com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Bad response: (400Bad Request
      at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:264)
      at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:145)
      at com.hersa.foraclient.TestClass.callSoapWebService(TestClass.java:131)
      at com.hersa.foraclient.TestClass.main(TestClass.java:39)
  
  CAUSE:
  
  com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Bad response: (400Bad Request
      at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:264)
      at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:145)
      at com.hersa.foraclient.TestClass.callSoapWebService(TestClass.java:131)
      at com.hersa.foraclient.TestClass.main(TestClass.java:39)
  </code></pre>
  
  <p>Any help is appreciated! </p>