From: 贾晓磊 <jia...@gm...> - 2011-12-02 06:35:20
|
HI, all: Recently I am doing something about python webservice. With the help of some groups, most of problems has been solved. Now, how to pass SOAP heasers makes me confused. I try to get help from web, such as http://stackoverflow.com/questions/2469988/how-to-pass-soap-headers-into-python-suds-that-are-not-defined-in-wsdl-file , http://stackoverflow.com/questions/2964867/add-header-section-to-soap-request-using-soappy, they indeed provide some valuable advises, but the problem still exists. I will describe it in detail as follows: part 1: I need to construct this soapheaders using SUDS. <soapenv:Header xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <ReqSOAPHeader soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="0" xmlns="http://common.v1_0.obj.protocol.xxt"> <serviceCode>PABB4BEIJING</serviceCode> <servicePwd>QWERTPABB</servicePwd> </ReqSOAPHeader> </soapenv:Header> part 2: my code import suds def test_sms9(): #step 1: import a encode from suds.xsd.doctor import ImportDoctor, Import imp = Import('http://schemas.xmlsoap.org/soap/encoding/') d = ImportDoctor(imp) #step 2: set the logging import logging logging.basicConfig(level=logging.ERROR) # step 3: construt client url = " http://211.137.45.104:9006/LnXxtCpInterface/services/LNXxtSyncService?wsdl" client = suds.client.Client(url,doctor=d,cache=None,xstq=False,faults=False) # step 4: create the header from suds.sax.element import Element from suds.sax.attribute import Attribute code = Element('serviceCode').setText('PABB4BEIJING') pwd = Element('servicePwd').setText('QWERTPABB') header_list = [code, pwd] client.set_options(soapheaders=header_list) # setp 5: provide the parameters for soap body item1 = "{'msgid':'1234567890','bizcode':'15140237310','serviceId':'1234567','recomobile':'15110791945','sendtime':'1322573860','content':'hi, this is just a test. you can ignore it. --jiaxiaolei'}" item2 = "{'msgid':'1234567891','bizcode':'15140237310','serviceId':'1234567','recomobile':'15110791946','sendtime':'1322573870','content':'hi, this is just a test. you can ignore it. --jiaxiaolei'}" req = [item1, item2] aoss = client.factory.create('ArrayOf_soapenc_string') aoss.item = req print 'client', client output = client.service.sendMt(aoss) print 'SOAP Request:\n', client.last_sent(), '\n' print 'SOAP Response:\n', client.last_received(), '\n' print 'the return: \n', output, '\n' #output: SOAP Request: <?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:ns0="service.global.v1_0.wsdl.protocol.xxt" xmlns:ns1="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance" x mlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/ encoding/"> <SOAP-ENV:Header> <serviceCode>PABB4BEIJING</serviceCode> <servicePwd>QWERTPABB</servicePwd> </SOAP-ENV:Header> <SOAP-ENV:Body> <ns0:sendMt> <mtInfo xsi:type="ArrayOf_soapenc_string"> <item xsi:type="ns1:string">{'msgid':'1234567890','bizcode':'151402 37310','serviceId':'1234567','recomobile':'15110791945','sendtim e':'1322573860','content':'hi, this is just a test. you can ignore it. --jiaxiaolei& apos;}</item> <item xsi:type="ns1:string">{'msgid':'1234567891','bizcode':'151402 37310','serviceId':'1234567','recomobile':'15110791946','sendtim e':'1322573870','content':'hi, this is just a test. you can ignore it. --jiaxiaolei'}</item> </mtInfo> </ns0:sendMt> </SOAP-ENV:Body> </SOAP-ENV:Envelope> SOAP Response: <?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd=" http://www.w3.org/2001/XMLSchema"> <soapenv:Body> <soapenv:Fault> <faultcode>soapenv:Server.generalException</faultcode> <faultstring/> <detail> <ns1:fault xmlns:ns1="service.global.v1_0.wsdl.protocol.xxt" href="#id0"/> <ns2:exceptionName xmlns:ns2="http://xml.apache.org/axis/ ">xxt.protocol.obj.v1_0.common.ServiceException</ns2:exceptionName> <ns3:hostname xmlns:ns3="http://xml.apache.org/axis/ ">ln_xxt_mh01</ns3:hostname> </detail> </soapenv:Fault> <multiRef xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns4="http://common.v1_0.obj.protocol.xxt" xsi:type="ns4:ServiceException" soapenc:root="0" soapenv:encodingStyle=" http://schemas.xmlsoap.org/soap/encoding/" id="id0"> <messageId xsi:type="soapenc:string">SEV_0003</messageId> <text xsi:type="soapenc:string">SoapHeader is null</text> </multiRef> </soapenv:Body> </soapenv:Envelope> the return: (500, (detail){ fault = (fault){ _href = "#id0" } exceptionName = "xxt.protocol.obj.v1_0.common.ServiceException" hostname = "ln_xxt_mh01" }) Then, I modify the code: # step 4: create the header from suds.sax.element import Element from suds.sax.attribute import Attribute code = Element('serviceCode').setText('PABB4BEIJING') pwd = Element('servicePwd').setText('QWERTPABB') header_list = [code, pwd] client.set_options(soapheaders=header_list) ==> # step 4: create the header from suds.sax.element import Element from suds.sax.attribute import Attribute code = Element('serviceCode').setText('PABB4BEIJING') pwd = Element('servicePwd').setText('QWERTPABB') reqsoapheader = Element('ReqSOAPHeader').insert(code) reqsoap_attribute = Attribute('xmlns', "http://schemas.acme.eu/") reqsoapheader.append(reqsoap_attribute) client.set_options(soapheaders=reqsoapheader) # output: SOAP Request: <?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:ns0="service.global.v1_0.wsdl.protocol.xxt" xmlns:ns1="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance" x mlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/ encoding/"> <SOAP-ENV:Header> <ReqSOAPHeader xmlns="http://schemas.acme.eu/"> <serviceCode>PABB4BEIJING</serviceCode> </ReqSOAPHeader> </SOAP-ENV:Header> <SOAP-ENV:Body> .... # NOTE: the body is omitted. SOAP Response: # it's same with mentioned above. I try to change the code to "reqsoapheader = Element('ReqSOAPHeader').insert([code,pwd]) ". it failed. Last, how to generate a soapheader as : <SOAP-ENV:Header> <ReqSOAPHeader> <serviceCode>xxxx</serviceCode> <servicePwd>yyy</servicePwd> </ReqSOAPHeader> <SOAP-ENV:Header> Any advice will be highly appreciated. Thanks and regards ahead for the the time and concentration. Thanks again! -- Jia Xiaolei -- NAME: 贾晓磊/Jia Xiaolei MOBILE: 13011292217 QQ: 281304051 MICRO-BLOG: http://weibo.com/2183890715 GMAIL: jia...@gm... <gmail%3Aj...@gm...> |