python3.x 使用http-post方式调用webservice

其实原理就是根据webservice协议指定的request的soap格式,用http post的method把参数传递过去就算是调用了,然后解析返回的soap就可以了

鉴于网上python3.x 的http post 实现代码比较少,我贴一段:


def InvokeWebservice(phone,msg):
    texturl='http://127.0.0.1:7789/SMSService.asmx?op=SendShortMessage'
    postcontent='<?xml version="1.0" encoding="utf-8"?>'
    postcontent+='<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/">'
    postcontent+='<soap:Body>'
    postcontent+='<SendShortMessage xmlns="http://tempuri.org/">'
    postcontent+='<phonenum>'+phone+'</phonenum>'#参数
    postcontent+='<message>'+msg+'</message>'#参数
    postcontent+='</SendShortMessage>'
    postcontent+='</soap:Body>'
    postcontent+='</soap:Envelope>'
    req=urllib.request.Request(texturl,data=postcontent.encode('utf-8'),headers={'Content-Type': 'text/xml'})
    urllib.request.urlopen(req)


posted @ 2013-04-23 17:45  爱知菜  阅读(827)  评论(0编辑  收藏  举报