通過Http請求訪問WebService

  使用的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.      

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章