使用的WebService地址为http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx所使用的服务方法为getWeatherbyCityName,访问上述地址可以看到该服务的说明。

   方法getWeatherbyCityName的Http请求报文格式示例如下:

SOAP 1.1

POST /WebServices/WeatherWebService.asmx HTTP/1.1
Host: webservice.webxml.com.cn
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://WebXml.com.cn/getWeatherbyCityName"

<?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>
    <getWeatherbyCityName xmlns="http://WebXml.com.cn/">
      <theCityName>string</theCityName>
    </getWeatherbyCityName>
  </soap:Body>
</soap:Envelope>
HTTP GET
GET /WebServices/WeatherWebService.asmx/getWeatherbyCityName?theCityName=string HTTP/1.1
Host: webservice.webxml.com.cn
HTTP POST
POST /WebServices/WeatherWebService.asmx/getWeatherbyCityName HTTP/1.1
Host: webservice.webxml.com.cn
Content-Type: application/x-www-form-urlencoded
Content-Length: length

theCityName=string

对于以上三种Http请求报文,使用python的httplib库构造起来很方便。

通过Http请求来访问该WebService的代码示例如下:

 
  
  1. import httplib 
  2. import urllib
  3.  
  4. *host值通过上面报文即可得到 
  5. host = "webservice.webxml.com.cn"
  6. *建立连接 
  7. conn = httplib.HTTPConnection(host)
  8.  
  9. *SOAP 1.1 
  10. def bySoap():
  11. *soap报文内容一定要copy原WebService给出的示例,写错非常难debug 
  12.     soapMessage ='''<?xml version="1.0" encoding="utf-8"?> 
  13.     <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  14.                    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  15.                    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
  16.       <soap:Body> 
  17.         <getWeatherbyCityName xmlns="http://WebXml.com.cn/"> 
  18.           <theCityName>beijing</theCityName> 
  19.         </getWeatherbyCityName> 
  20.       </soap:Body> 
  21.     </soap:Envelope>''' 
  22. *Http报文头
  23.     headers = {"Content-Type":"text/xml; charset=utf-8"
  24.                "Content-Length":"%d" % len(soapMessage), 
  25.                "SOAPAction":"\"http://WebXml.com.cn/getWeatherbyCityName\""
  26.     *发送请求
  27. conn.request("POST""/WebServices/WeatherWebService.asmx"'', headers) 
  28.     *发送Soap报文
  29. conn.send(soapMessage)
  30. *获得响应 
  31.     r = conn.getresponse() 
  32.     print r.read() 
  33.  
  34. *HTTP GET
  35. def byHttpGet():
  36.   *发送请求
  37.     conn.request("GET""/WebServices/WeatherWebService.asmx/getWeatherbyCityName?theCityName=beijing"
  38.     *获得响应
  39. r = conn.getresponse() 
  40.     print r.read() 
  41.  
  42. *HTTP POST
  43. def byHttpPost(): 
  44.     *post参数需要urlencode
  45. params = urllib.urlencode({'theCityName':'beijing'}) 
  46.     *Http报文头
  47. headers = {'Content-Type''application/x-www-form-urlencoded'
  48.     *发送请求
  49. conn.request("POST""/WebServices/WeatherWebService.asmx/getWeatherbyCityName", params, headers) 
  50.     *获得响应
  51. r = conn.getresponse() 
  52.     print r.read() 
  53.  
  54. def main(): 
  55.     bySoap() 
  56.     byHttpGet() 
  57.     byHttpPost() 
  58. *关闭连接
  59.     conn.close() 
  60.  
  61. if __name__ == '__main__'
  62.     main() 
  63.