SOAPConnection call exception

There is a web client application here from www.webserviceX.NET to get weather data from SOAP-Server and it works great. See Here: http://www.webservicex.net/ws/WSDetails.aspx?CATID=12&WSID=56

Now I am trying to use weather data with the Java SOAPConnection class, but I am getting an exception from this server. My environment: jdk 1.8.0_45 and eclipse

Exception messages:

SOAPException: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Invalid Content-Type:text/html. 
Is this an error message instead of a SOAP response?
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(Unknown Source)

      

What am I sending to the server

<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" 
xmlns:GetWeather="http://www.webserviceX.NET/">
<env:Header/>
<env:Body>
<GetWeather:BlaBla>
<GetWeather:CountryName>Switzerland</GetWeather:CountryName>
<GetWeather:CityName>Zurich</GetWeather:CityName>
</GetWeather:BlaBla>
</env:Body>

      

What the server expects

POST /globalweather.asmx HTTP/1.1
Host: www.webservicex.net
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
    <soap12:Body>
    <GetWeather xmlns="http://www.webserviceX.NET">
        <CityName>string</CityName>
        <CountryName>string</CountryName>
    </GetWeather>
    </soap12:Body>
</soap12:Envelope>
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

      

My Java source

This code throws an exception:

SOAPMessage soapResponse = soapConnection.call(soapMessage, url);

public static void main(String args[])
    {
        try
        {
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection soapConnection = soapConnectionFactory.createConnection();

            String url = "http://www.webserviceX.NET/";

            MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);

            SOAPMessage soapMessage = messageFactory.createMessage();

            SOAPPart soapPart = soapMessage.getSOAPPart();
            String serverURI = "http://www.webserviceX.NET/";

            SOAPEnvelope envelope = soapPart.getEnvelope();
            envelope.addNamespaceDeclaration("GetWeather", serverURI);

            SOAPBody soapBody = envelope.getBody();
            SOAPElement soapBodyElem = soapBody.addChildElement("BlaBla", "GetWeather");

            soapBodyElem.addChildElement("CountryName", "GetWeather").addTextNode("Switzerland");       
            soapBodyElem.addChildElement("CityName", "GetWeather").addTextNode("Zurich");

            MimeHeaders headers = soapMessage.getMimeHeaders();
            headers.setHeader("Content-Type", "application/soap+xml; charset=utf-8");
            headers.addHeader("SOAPAction", serverURI + "BlaBla");

            soapMessage.saveChanges();
            soapMessage.writeTo(System.out);
            System.out.println();

            // call() throws a SOAPException 
            SOAPMessage soapResponse = soapConnection.call(soapMessage, url);   

            soapConnection.close();
        }
        catch (SOAPException ex)
        {
            System.out.println("SOAPException: " + ex.getMessage()); ;
            ex.printStackTrace();           
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }       
    }

      

+3


source to share


1 answer


You have to change url

which one you call:

String url = "http://www.webservicex.net/globalweather.asmx?WSDL";

      



With this change, you will make the correct call to the WebService.

0


source







All Articles