urllib2访问soap

首先是得到wsdl,用xmlspy产生出一个request,

然后用urllib2写soap的头,把request放在soap的body里。

from ATOM.core import atomurllib2
def post_request_to_csa(soap_request_xml, etok=None):
    WebService_URL = "http://localhost/MyWebService/GetTimeService.asmx/GetDay"
    hdr = {
        'Content-Type': 'text/xml; charset=utf-8',
        'Host': 'localhost',
        'Content-Length': len(soap_request_xml),
        'SOAPAction': 'http://localhost/MyWebService/GetTimeService.asmx/GetDay'
    }

    try:
        urllib2_request_obj = atomurllib2.Request(WebService_URL, data=soap_request_xml, headers=hdr)
        opener = atomurllib2.build_opener()
        atomurllib2.install_opener(opener)
        f = atomurllib2.urlopen(urllib2_request_obj)

        if f.getcode()==200:
            soap_response = f.read()
        print soap_response

        return soap_response
    except Exception:
        print "error"

if __name__ == '__main__':
        print "start"
        xml='''<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <SOAP-ENV:Body>
        <m:GetDay xmlns:m="http://tempuri.org/"/>
        </SOAP-ENV:Body>
        </SOAP-ENV:Envelope>'''
        post_request_to_csa(xml)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值