简体   繁体   English

SOAPConnection调用抛出异常

[英]SOAPConnection call throws exception

There's a Web-Client application from www.webserviceX.NET to get weather data from SOAP-Server and it works perfectly. 有一个来自www.webserviceX.NET的Web客户端应用程序可以从SOAP-Server获取天气数据,它可以很好地工作。 See here: http://www.webservicex.net/ws/WSDetails.aspx?CATID=12&WSID=56 Now, I try my self to call weather data with Java SOAPConnection class, but I get an exception from this server. 请参阅此处: http://www.webservicex.net/ws/WSDetails.aspx?CATID=12&WSID=56http://www.webservicex.net/ws/WSDetails.aspx?CATID=12&WSID=56 ?CATID = 12&WSID = 56现在,我尝试使用Java SOAPConnection类调用天气数据,但是我从此服务器获得异常。 My Environment is: jdk 1.8.0_45 and eclipse 我的环境是:jdk 1.8.0_45和eclipse

Exception-Message: 异常消息:

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 I send to 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 expect 服务器的期望

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 我的Java-Source

This code throws the 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();
        }       
    }

You should change url that you calling: 你应该改变你打电话的url

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

With this change you will do proper call to WebService. 通过此更改,您将正确调用WebService。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM