title: How to fix Invalid Content-Type:text/html in SOAP client using Java
body: <p>I am trying to send a request to <a href="http://www.webservicex.com/globalweather.asmx" rel="nofollow noreferrer">http://www.webservicex.com/globalweather.asmx</a></p>
  
  <p>I tried running this SOAP Webservice Call but getting the error below. 
  Where can I edit the Context type</p>
  
  <pre><code>import javax.xml.soap.MessageFactory;
  import javax.xml.soap.SOAPBody;
  import javax.xml.soap.SOAPConnection;
  import javax.xml.soap.SOAPConnectionFactory;
  import javax.xml.soap.SOAPElement;
  import javax.xml.soap.SOAPEnvelope;
  import javax.xml.soap.SOAPMessage;
  import javax.xml.soap.SOAPPart;
  import javax.xml.transform.Source;
  import javax.xml.transform.Transformer;
  import javax.xml.transform.TransformerFactory;
  import javax.xml.transform.stream.StreamResult;
  
  
  public class Main2
  {
  
          private static SOAPMessage createSOAPRequest() throws Exception
          {
                  MessageFactory messageFactory = MessageFactory.newInstance();
                  SOAPMessage soapMessage = messageFactory.createMessage();
                  SOAPPart soapPart = soapMessage.getSOAPPart();
  
  
                  // SOAP Envelope
                  SOAPEnvelope envelope = soapPart.getEnvelope();
                  envelope.addNamespaceDeclaration("web", "http://www.webserviceX.NET");
  
                  // SOAP Body
                  SOAPBody soapBody = envelope.getBody();
                  SOAPElement soapBodyElem = soapBody.addChildElement("GetCitiesByCountry", "web");
                  SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("CountryName", "web");
                  soapBodyElem1.addTextNode("Brazil");
  
                  soapMessage.saveChanges();
  
                  // Check the input
                  System.out.println("Request SOAP Message = ");
                  soapMessage.writeTo(System.out);
                  System.out.println();
                  return soapMessage;
          }
  
          /**
           * Method used to print the SOAP Response
           */
          private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception
          {
                  TransformerFactory transformerFactory = TransformerFactory.newInstance();
                  Transformer transformer = transformerFactory.newTransformer();
                  Source sourceContent = soapResponse.getSOAPPart().getContent();
                  System.out.println("\nResponse SOAP Message = ");
                  StreamResult result = new StreamResult(System.out);
                  transformer.transform(sourceContent, result);
          }
  
          /**
           * Starting point for the SAAJ - SOAP Client Testing
           */
          public static void main(String args[])
          {
                  try
                  {
                          SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
                          SOAPConnection soapConnection = soapConnectionFactory.createConnection();
  
                          String url = "http://www.dneonline.com/calculator.asmx?WSDL";
                          SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
  
                          soapConnection.close();
                  }
                  catch (Exception e)
                  {
                          System.err.println("Error occurred while sending SOAP Request to Server");
                          e.printStackTrace();
                  }
          }
  }
  </code></pre>
  
  <p>Here is the output in the Console:</p>
  
  <pre><code>Request SOAP Message = 
  &lt;SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.webserviceX.NET"&gt;&lt;SOAP-ENV:Header/&gt;&lt;SOAP-ENV:Body&gt;&lt;web:GetCitiesByCountry&gt;&lt;web:CountryName&gt;Brazil&lt;/web:CountryName&gt;&lt;/web:GetCitiesByCountry&gt;&lt;/SOAP-ENV:Body&gt;&lt;/SOAP-ENV:Envelope&gt;
  Feb 16, 2017 8:54:40 PM com.sun.xml.internal.messaging.saaj.soap.MessageImpl identifyContentType
  SEVERE: SAAJ0537: Invalid Content-Type. Could be an error message instead of a SOAP message
  Error occurred while sending SOAP Request to Server
  com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Invalid Content-Type:text/html. Is this an error message instead of a SOAP response?
      at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:148)
      at com.hpe.snow.utils.snowwstester.Main2.main(Main2.java:109)
  Caused by: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Invalid Content-Type:text/html. Is this an error message instead of a SOAP response?
      at com.sun.xml.internal.messaging.saaj.soap.MessageImpl.identifyContentType(MessageImpl.java:602)
      at com.sun.xml.internal.messaging.saaj.soap.MessageFactoryImpl.createMessage(MessageFactoryImpl.java:86)
      at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:328)
      at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:144)
      ... 1 more
  
  CAUSE:
  
  com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Invalid Content-Type:text/html. Is this an error message instead of a SOAP response?
      at com.sun.xml.internal.messaging.saaj.soap.MessageImpl.identifyContentType(MessageImpl.java:602)
      at com.sun.xml.internal.messaging.saaj.soap.MessageFactoryImpl.createMessage(MessageFactoryImpl.java:86)
      at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:328)
      at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:144)
      at com.hpe.snow.utils.snowwstester.Main2.main(Main2.java:109)
  
  CAUSE:
  
  com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Invalid Content-Type:text/html. Is this an error message instead of a SOAP response?
      at com.sun.xml.internal.messaging.saaj.soap.MessageImpl.identifyContentType(MessageImpl.java:602)
      at com.sun.xml.internal.messaging.saaj.soap.MessageFactoryImpl.createMessage(MessageFactoryImpl.java:86)
      at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:328)
      at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:144)
      at com.hpe.snow.utils.snowwstester.Main2.main(Main2.java:109)
  </code></pre>
  
  <p>What needs to be added? Or Edited?</p>
  
  <p>UPDATE</p>
  
  <p>I inserted</p>
  
  <pre><code>        soapBodyElem1.addTextNode("Brazil");
  
          MimeHeaders headers = soapMessage.getMimeHeaders();
          headers.addHeader("Content-Type", "application/html");
  
  
          soapMessage.saveChanges();
  </code></pre>
  
  <p>but I got error</p>
  
  <pre><code>com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection post
  SEVERE: SAAJ0010: Unable to read response
  java.net.SocketException: Software caused connection abort: recv failed
      at java.net.SocketInputStream.socketRead0(Native Method)
      at java.net.SocketInputStream.read(SocketInputStream.java:129)
      at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
      at java.io.BufferedInputStream.read1(BufferedInputStream.java:258)
      at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
      at sun.net.www.MeteredStream.read(MeteredStream.java:116)
      at java.io.FilterInputStream.read(FilterInputStream.java:116)
      at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:2707)
      at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:2702)
      at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.readFully(HttpSOAPConnection.java:567)
      at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:314)
      at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:144)
      at com.hpe.snow.utils.snowwstester.Main2.main(Main2.java:75)
  Error occurred while sending SOAP Request to Server
  com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Unable to read response: Software caused connection abort: recv failed
      at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:148)
      at com.hpe.snow.utils.snowwstester.Main2.main(Main2.java:75)
  Caused by: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Unable to read response: Software caused connection abort: recv failed
      at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:338)
      at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:144)
      ... 1 more
  
  CAUSE:
  
  com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Unable to read response: Software caused connection abort: recv failed
      at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:338)
      at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:144)
      at com.hpe.snow.utils.snowwstester.Main2.main(Main2.java:75)
  
  CAUSE:
  
  com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Unable to read response: Software caused connection abort: recv failed
      at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:338)
      at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:144)
      at com.hpe.snow.utils.snowwstester.Main2.main(Main2.java:75)
  </code></pre>