来自www.webserviceX.NET的Web客户端应用程序可以从SOAP-Server获取天气数据,并且运行良好。
见这里:http://www.webservicex.net/ws/WSDetails.aspx?CATID=12&WSID=56
现在,我尝试使用Java SOAPConnection类调用天气数据,但是我从这个服务器获得了一个异常。
我的环境是:jdk 1.8.0_45和eclipse
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)
<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>
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
此代码抛出异常:
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();
}
}
答案 0 :(得分:0)
您应该更改您呼叫的AppCompatDelegate
:
url
通过此更改,您将正确调用WebService。