urllib2访问soap

来源:互联网 发布:单例模式作用 java 编辑:程序博客网 时间:2024/05/21 15:05

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

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

from ATOM.core import atomurllib2def 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)


原创粉丝点击