wsdl地址:http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl
//soap请求体
POST /WebServices/WeatherWS.asmx HTTP/1.1 Host: ws.webxml.com.cn Content-Type: text/xml; charset=utf-8 Content-Length: length SOAPAction: "http://WebXml.com.cn/getWeather" <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <getWeather xmlns="http://WebXml.com.cn/"> <theCityCode>string</theCityCode> <theUserID>string</theUserID> </getWeather> </soap:Body> </soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
//soap响应体 <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <getWeatherResponse xmlns="http://WebXml.com.cn/"> <getWeatherResult> <string>string</string> <string>string</string> </getWeatherResult> </getWeatherResponse> </soap:Body> </soap:Envelope>
创建jiava项目
java类
public class Demo { /** * 访问服务 * @param wsdl wsdl地址 * @param ns 命名空间 * @param method 方法名 * @param params 参数 * @return * @throws Exception */ public static void main(String[] args) { //String wsdl,String ns,String method,Map<String,String> params,String result String wsdl="http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl";//wsdl地址 int time = 30000;//设置请求超时时间 HttpClient client = new HttpClient(); PostMethod postMethod = new PostMethod(wsdl); //设置请求连接超时时间 client.getHttpConnectionManager().getParams().setConnectionTimeout(time*3); //设置读取时间超时 client.getHttpConnectionManager().getParams().setSoTimeout(3*time); //拼接SOAP StringBuffer soapRequestData = new StringBuffer(""); soapRequestData.append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"); soapRequestData.append("<soap:Body>"); soapRequestData.append(" <getWeather xmlns=\"http://WebXml.com.cn/\">"); soapRequestData.append("<theCityCode>宁波</theCityCode>"); soapRequestData.append("<theUserID></theUserID>"); soapRequestData.append(" </getWeather>"); soapRequestData.append(" </soap:Body>"); soapRequestData.append("</soap:Envelope>"); try { // 然后把Soap请求数据添加到PostMethod中 RequestEntity requestEntity = new StringRequestEntity(soapRequestData.toString(), "text/xml", "UTF-8"); postMethod.setRequestEntity(requestEntity); System.out.println(soapRequestData.toString()); int status = client.executeMethod(postMethod); System.out.println("status:"+status); //获得响应体输入流 InputStream response= postMethod.getResponseBodyAsStream(); String resp = IOUtils.toString(response,"UTF-8"); System.out.println(resp); } catch (Exception e) { e.printStackTrace(); } } }
响应 结果
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body> <getWeather xmlns="http://WebXml.com.cn/"><theCityCode>宁波</theCityCode><theUserID></theUserID> </getWeather> </soap:Body></soap:Envelope> status:200 <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><getWeatherResponse xmlns="http://WebXml.com.cn/"><getWeatherResult><string>浙江 宁波</string><string>宁波</string><string>2106</string><string>2016/11/13 15:36:11</string><string>今日天气实况:气温:22℃;风向/风力:东南风 2级;湿度:81%</string><string>紫外线强度:最弱。空气质量:良。</string><string>紫外线指数:最弱,辐射弱,涂擦SPF8-12防晒护肤品。 感冒指数:少发,无明显降温,感冒机率较低。 穿衣指数:较舒适,建议穿薄外套或牛仔裤等服装。 洗车指数:不宜,有雨,雨水和泥水会弄脏爱车。 运动指数:较不宜,有降水,推荐您在室内进行休闲运动。 空气污染指数:良,气象条件有利于空气污染物扩散。 </string><string>11月13日 小雨转阴</string><string>16℃/21℃</string><string>东南风转南风微风</string><string>7.gif</string><string>2.gif</string><string>11月14日 小雨</string><string>15℃/21℃</string><string>北风微风转东北风3-4级</string><string>7.gif</string><string>7.gif</string><string>11月15日 小雨转阴</string><string>13℃/18℃</string><string>东北风3-4级</string><string>7.gif</string><string>2.gif</string><string>11月16日 多云转阴</string><string>15℃/20℃</string><string>东北风转东风微风</string><string>1.gif</string><string>2.gif</string><string>11月17日 多云</string><string>15℃/21℃</string><string>东风转东南风微风</string><string>1.gif</string><string>1.gif</string></getWeatherResult></getWeatherResponse></soap:Body></soap:Envelope>需要的架包
commons-codec-1.9.jar
commons-httpclient-3.1.jar
commons-io-2.2.jar
commons-logging-1.2.jar