webservice接口:Python调用 响应xml解析失败。

问题:使用python的suds库请求服务,响应xml解析失败,但是使用soupUI调用是成功的。

解决:
soupUI请求中含xml数据,需要将xml的外围增加 <![CDATA[xml]]>
python中suds.client调用方法传参时直接将数据中的xml作为字符串的格式传入即可。

CDATA:
在某些情况下,我们在xml中要使用大量XML敏感的字符,而我们又不希望逐一的对其进行转移。这时候使用CDATA段是最理想的。

   1)语法格式:< ! [ CDATA [忽略检查的文本]]>

   2)在CDATA中将文本的内容写入,那么这段文本内容会被忽略检查,无论里面是否包含XML敏感内容,全部被当作普通的文本去看待。

以下为soupUI调用示例、python的suds.client调用示例和postman调用:

  • soupUI调用示例:
  1. 第一步:创建服务项目

soupUI创建方式

  1. 第二步:

在这里插入图片描述
图中可以看出请求解析是成功的。(报错信息是后端返回的,由于文件服务器没有上传文件导致,与此次问题无关,请忽略!)

  • 使用python的suds.client调用
  1. 正确示例
from suds.client import Client
import json
client = Client('http://172.16.101.188:8081/OTC_DC_SERVR/services/OtcWebService?wsdl')
jsonvalue = { "orgId": "M80074", "userId": "M80074A50", "reportType": "1", "operationType": "2", "fuctionNumber": "FN_001", "channelsMarked": "4",
"data": "<?xml version='1.0' encoding='UTF-8'?><xbrli:xbrl xsi:schemaLocation='http://xbrl.org/2006/xbrldi http://www.xbrl.org/2006/xbrldi-2006.xsd' xmlns:qspp='http://www.interotc.com/taxonomy/privateproduct' xmlns:vis='http://www.interotc.com/taxonomy/supervision/2013-12-31' xmlns:link='http://www.xbrl.org/2003/linkbase' xmlns:gbicc='http://report.gbicc.net/xbrl' xmlns:xlink='http://www.w3.org/1999/xlink' xmlns:iso4217='http://www.xbrl.org/2003/iso4217' xmlns:xbrldi='http://xbrl.org/2006/xbrldi' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xbrli='http://www.xbrl.org/2003/instance'> <link:schemaRef defined-in='com.interotc.derivatives.report.otcwebservice.vo.XmlnsHead' xmlns:href='http://www.interotc.com/taxonomy/qspp.xsd' xmlns:type='simple'/> <link:schemaRef xmlns:href='http://www.interotc.com/taxonomy/qspp.xsd' xmlns:type='simple'/> <xbrli:context id='c_duration_20220101_20221231'> <xbrli:entity> <xbrli:identifier scheme='http://www.interotc.com/qsp'>IMP000000001010</xbrli:identifier> </xbrli:entity> <xbrli:period> <xbrli:startDate>2016-12-12</xbrli:startDate> <xbrli:endDate>2017-02-02</xbrli:endDate> </xbrli:period> </xbrli:context> <xbrli:context id='c_instant_20031231'> <xbrli:entity> <xbrli:identifier scheme='http://www.interotc.com/qsp'>IMP000000001010</xbrli:identifier> </xbrli:entity> <xbrli:period> <xbrli:instant>2003-12-31</xbrli:instant> </xbrli:period> </xbrli:context> <xbrli:unit id='U_shares'> <xbrli:measure>xbrli:shares</xbrli:measure> </xbrli:unit> <xbrli:unit id='U_pare'> <xbrli:measure>xbrli:pure</xbrli:measure> </xbrli:unit> <xbrli:unit id='U_RMB'> <xbrli:measure>iso4217:CNY</xbrli:measure> </xbrli:unit> <qspp:MainAgreementNumber2 contextRef='c_duration_20220101_20221231'>zdk20220909Z</qspp:MainAgreementNumber2> <qspp:SigningDateOfMasterAgreement contextRef='c_duration_20220101_20221231'>2019-02-27</qspp:SigningDateOfMasterAgreement> <qspp:PrimaryProtocolVersion contextRef='c_duration_20220101_20221231'>0</qspp:PrimaryProtocolVersion> <qspp:RoleOfFillingParty contextRef='c_duration_20220101_20221231'>1</qspp:RoleOfFillingParty> <qspp:NameOfCounterparty contextRef='c_duration_20220101_20221231'>'u4ea4'u6613'u5bf9'u624b'u65b9</qspp:NameOfCounterparty> <qspp:CounterpartyID contextRef='c_duration_20220101_20221231'>000000000000</qspp:CounterpartyID><qspp:ProfessionalCounterpartiesOrNot contextRef='c_duration_20220101_20221231'>0</qspp:ProfessionalCounterpartiesOrNot><qspp:CounterpartyType contextRef='c_duration_20220101_20221231'>0</qspp:CounterpartyType><qspp:NonFinancialInstitutionIndustryCode contextRef='c_duration_20220101_20221231'>000000000000</qspp:NonFinancialInstitutionIndustryCode><qspp:CounterpartyRegisteredCapital contextRef='c_duration_20220101_20221231'>11.00</qspp:CounterpartyRegisteredCapital><qspp:MasterAgreementRemark contextRef='c_duration_20220101_20221231'>'u4e3b'u534f'u8bae'u5907'u6ce8</qspp:MasterAgreementRemark><qspp:MasterAgreementAccessoriesTwo contextRef='c_duration_20220101_20221231'>编辑-风险揭示书.pdf|Z9TTItTnqWHc35sKvW500DZiy.pdf</qspp:MasterAgreementAccessoriesTwo><qspp:CounterpartyIdentity contextRef='c_duration_20220101_20221231'>1</qspp:CounterpartyIdentity><qspp:CODS contextRef='c_duration_20220101_20221231'>111111111111111111</qspp:CODS><qspp:LEI contextRef='c_duration_20220101_20221231'>111111111</qspp:LEI><qspp:CounterpartyInformationTuple id='tmp1'><qspp:Name contextRef='c_duration_20220101_20221231'>1</qspp:Name><qspp:DepartmentPosition contextRef='c_duration_20220101_20221231'>1</qspp:DepartmentPosition><qspp:TelephoneNumber contextRef='c_duration_20220101_20221231'>1</qspp:TelephoneNumber><qspp:PhoneNumber contextRef='c_duration_20220101_20221231'>13112145214</qspp:PhoneNumber><qspp:EmailAddress contextRef='c_duration_20220101_20221231'>1</qspp:EmailAddress></qspp:CounterpartyInformationTuple></xbrli:xbrl>" }
jsonvalue = json.dumps(jsonvalue)
print(client.service.reportInformationCollection(jsonvalue))

注意代码中data数据 去掉了<![CDATA[xml]]> ,解析成功(错误信息为后台业务校验返回,与此次问题无关)
在这里插入图片描述

  1. 错误示例
client = Client('http://172.16.101.188:8081/OTC_DC_SERVR/services/OtcWebService?wsdl')
# jsonvalue = {"orgId": "M80074", "userId": "M80074A50", "reportType": "1", "operationType": "2", "fuctionNumber": "FN_001", "channelsMarked": "4",
# "data": "<?xml version='1.0' encoding='UTF-8'?><xbrli:xbrl xsi:schemaLocation='http://xbrl.org/2006/xbrldi http://www.xbrl.org/2006/xbrldi-2006.xsd' xmlns:qspp='http://www.interotc.com/taxonomy/privateproduct' xmlns:vis='http://www.interotc.com/taxonomy/supervision/2013-12-31' xmlns:link='http://www.xbrl.org/2003/linkbase' xmlns:gbicc='http://report.gbicc.net/xbrl' xmlns:xlink='http://www.w3.org/1999/xlink' xmlns:iso4217='http://www.xbrl.org/2003/iso4217' xmlns:xbrldi='http://xbrl.org/2006/xbrldi' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xbrli='http://www.xbrl.org/2003/instance'> <link:schemaRef defined-in='com.interotc.derivatives.report.otcwebservice.vo.XmlnsHead' xmlns:href='http://www.interotc.com/taxonomy/qspp.xsd' xmlns:type='simple'/> <link:schemaRef xmlns:href='http://www.interotc.com/taxonomy/qspp.xsd' xmlns:type='simple'/> <xbrli:context id='c_duration_20220101_20221231'> <xbrli:entity> <xbrli:identifier scheme='http://www.interotc.com/qsp'>IMP000000001010</xbrli:identifier> </xbrli:entity> <xbrli:period> <xbrli:startDate>2016-12-12</xbrli:startDate> <xbrli:endDate>2017-02-02</xbrli:endDate> </xbrli:period> </xbrli:context> <xbrli:context id='c_instant_20031231'> <xbrli:entity> <xbrli:identifier scheme='http://www.interotc.com/qsp'>IMP000000001010</xbrli:identifier> </xbrli:entity> <xbrli:period> <xbrli:instant>2003-12-31</xbrli:instant> </xbrli:period> </xbrli:context> <xbrli:unit id='U_shares'> <xbrli:measure>xbrli:shares</xbrli:measure> </xbrli:unit> <xbrli:unit id='U_pare'> <xbrli:measure>xbrli:pure</xbrli:measure> </xbrli:unit> <xbrli:unit id='U_RMB'> <xbrli:measure>iso4217:CNY</xbrli:measure> </xbrli:unit> <qspp:MainAgreementNumber2 contextRef='c_duration_20220101_20221231'>zdk20220909Z</qspp:MainAgreementNumber2> <qspp:SigningDateOfMasterAgreement contextRef='c_duration_20220101_20221231'>2019-02-27</qspp:SigningDateOfMasterAgreement> <qspp:PrimaryProtocolVersion contextRef='c_duration_20220101_20221231'>0</qspp:PrimaryProtocolVersion> <qspp:RoleOfFillingParty contextRef='c_duration_20220101_20221231'>1</qspp:RoleOfFillingParty> <qspp:NameOfCounterparty contextRef='c_duration_20220101_20221231'>'u4ea4'u6613'u5bf9'u624b'u65b9</qspp:NameOfCounterparty> <qspp:CounterpartyID contextRef='c_duration_20220101_20221231'>000000000000</qspp:CounterpartyID><qspp:ProfessionalCounterpartiesOrNot contextRef='c_duration_20220101_20221231'>0</qspp:ProfessionalCounterpartiesOrNot><qspp:CounterpartyType contextRef='c_duration_20220101_20221231'>0</qspp:CounterpartyType><qspp:NonFinancialInstitutionIndustryCode contextRef='c_duration_20220101_20221231'>000000000000</qspp:NonFinancialInstitutionIndustryCode><qspp:CounterpartyRegisteredCapital contextRef='c_duration_20220101_20221231'>11.00</qspp:CounterpartyRegisteredCapital><qspp:MasterAgreementRemark contextRef='c_duration_20220101_20221231'>'u4e3b'u534f'u8bae'u5907'u6ce8</qspp:MasterAgreementRemark><qspp:MasterAgreementAccessoriesTwo contextRef='c_duration_20220101_20221231'>编辑-风险揭示书.pdf|Z9TTItTnqWHc35sKvW500DZiy.pdf</qspp:MasterAgreementAccessoriesTwo><qspp:CounterpartyIdentity contextRef='c_duration_20220101_20221231'>1</qspp:CounterpartyIdentity><qspp:CODS contextRef='c_duration_20220101_20221231'>111111111111111111</qspp:CODS><qspp:LEI contextRef='c_duration_20220101_20221231'>111111111</qspp:LEI><qspp:CounterpartyInformationTuple id='tmp1'><qspp:Name contextRef='c_duration_20220101_20221231'>1</qspp:Name><qspp:DepartmentPosition contextRef='c_duration_20220101_20221231'>1</qspp:DepartmentPosition><qspp:TelephoneNumber contextRef='c_duration_20220101_20221231'>1</qspp:TelephoneNumber><qspp:PhoneNumber contextRef='c_duration_20220101_20221231'>13112145214</qspp:PhoneNumber><qspp:EmailAddress contextRef='c_duration_20220101_20221231'>1</qspp:EmailAddress></qspp:CounterpartyInformationTuple></xbrli:xbrl>" }
jsonvalue = {"orgId": "M80074", "userId": "M80074A50", "reportType": "1", "operationType": "2", "fuctionNumber": "FN_001", "channelsMarked": "4", 
"data": "<![CDATA[<?xml version='1.0' encoding='UTF-8'?><xbrli:xbrl xsi:schemaLocation='http://xbrl.org/2006/xbrldi http://www.xbrl.org/2006/xbrldi-2006.xsd' xmlns:qspp='http://www.interotc.com/taxonomy/privateproduct' xmlns:vis='http://www.interotc.com/taxonomy/supervision/2013-12-31' xmlns:link='http://www.xbrl.org/2003/linkbase' xmlns:gbicc='http://report.gbicc.net/xbrl' xmlns:xlink='http://www.w3.org/1999/xlink' xmlns:iso4217='http://www.xbrl.org/2003/iso4217' xmlns:xbrldi='http://xbrl.org/2006/xbrldi' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xbrli='http://www.xbrl.org/2003/instance'> <link:schemaRef defined-in='com.interotc.derivatives.report.otcwebservice.vo.XmlnsHead' xmlns:href='http://www.interotc.com/taxonomy/qspp.xsd' xmlns:type='simple'/> <link:schemaRef xmlns:href='http://www.interotc.com/taxonomy/qspp.xsd' xmlns:type='simple'/> <xbrli:context id='c_duration_20220101_20221231'> <xbrli:entity> <xbrli:identifier scheme='http://www.interotc.com/qsp'>IMP000000001010</xbrli:identifier> </xbrli:entity> <xbrli:period> <xbrli:startDate>2016-12-12</xbrli:startDate> <xbrli:endDate>2017-02-02</xbrli:endDate> </xbrli:period> </xbrli:context> <xbrli:context id='c_instant_20031231'> <xbrli:entity> <xbrli:identifier scheme='http://www.interotc.com/qsp'>IMP000000001010</xbrli:identifier> </xbrli:entity> <xbrli:period> <xbrli:instant>2003-12-31</xbrli:instant> </xbrli:period> </xbrli:context> <xbrli:unit id='U_shares'> <xbrli:measure>xbrli:shares</xbrli:measure> </xbrli:unit> <xbrli:unit id='U_pare'> <xbrli:measure>xbrli:pure</xbrli:measure> </xbrli:unit> <xbrli:unit id='U_RMB'> <xbrli:measure>iso4217:CNY</xbrli:measure> </xbrli:unit> <qspp:MainAgreementNumber2 contextRef='c_duration_20220101_20221231'>zdk20220909Z</qspp:MainAgreementNumber2> <qspp:SigningDateOfMasterAgreement contextRef='c_duration_20220101_20221231'>2019-02-27</qspp:SigningDateOfMasterAgreement> <qspp:PrimaryProtocolVersion contextRef='c_duration_20220101_20221231'>0</qspp:PrimaryProtocolVersion> <qspp:RoleOfFillingParty contextRef='c_duration_20220101_20221231'>1</qspp:RoleOfFillingParty> <qspp:NameOfCounterparty contextRef='c_duration_20220101_20221231'>'u4ea4'u6613'u5bf9'u624b'u65b9</qspp:NameOfCounterparty> <qspp:CounterpartyID contextRef='c_duration_20220101_20221231'>000000000000</qspp:CounterpartyID><qspp:ProfessionalCounterpartiesOrNot contextRef='c_duration_20220101_20221231'>0</qspp:ProfessionalCounterpartiesOrNot><qspp:CounterpartyType contextRef='c_duration_20220101_20221231'>0</qspp:CounterpartyType><qspp:NonFinancialInstitutionIndustryCode contextRef='c_duration_20220101_20221231'>000000000000</qspp:NonFinancialInstitutionIndustryCode><qspp:CounterpartyRegisteredCapital contextRef='c_duration_20220101_20221231'>11.00</qspp:CounterpartyRegisteredCapital><qspp:MasterAgreementRemark contextRef='c_duration_20220101_20221231'>'u4e3b'u534f'u8bae'u5907'u6ce8</qspp:MasterAgreementRemark><qspp:MasterAgreementAccessoriesTwo contextRef='c_duration_20220101_20221231'>'u73e0'u6d77'u6a2a'u7434'u4e07'u65b9'u8d44'u4ea7'u7ba1'u7406'u5408'u4f19'u4f01'u4e1a'uff08'u6709'u9650'u5408'u4f19'uff09'uff08'u4ee3'u8868'u4e07'u65b9'u6398'u91d117'u53f7A'u671f'u79c1'u52df'u8bc1'u5238'u6295'u8d44'u57fa'u91d1'uff09-OTC-TRS-3005210617-0021'u8de8'u5883'u4e92'u6362.pdf|5e081568be1948cda5464b85888656e7.pdf</qspp:MasterAgreementAccessoriesTwo><qspp:CounterpartyIdentity contextRef='c_duration_20220101_20221231'>1</qspp:CounterpartyIdentity><qspp:CODS contextRef='c_duration_20220101_20221231'>111111111111111111</qspp:CODS><qspp:LEI contextRef='c_duration_20220101_20221231'>111111111</qspp:LEI><qspp:CounterpartyInformationTuple id='tmp1'><qspp:Name contextRef='c_duration_20220101_20221231'>1</qspp:Name><qspp:DepartmentPosition contextRef='c_duration_20220101_20221231'>1</qspp:DepartmentPosition><qspp:TelephoneNumber contextRef='c_duration_20220101_20221231'>1</qspp:TelephoneNumber><qspp:PhoneNumber contextRef='c_duration_20220101_20221231'>13112145214</qspp:PhoneNumber><qspp:EmailAddress contextRef='c_duration_20220101_20221231'>1</qspp:EmailAddress></qspp:CounterpartyInformationTuple></xbrli:xbrl>]]>" }
jsonvalue = json.dumps(jsonvalue)
print(client.service.reportInformationCollection(jsonvalue))

响应结果解析失败: 原因是data数据(data数据为xml格式)中加了<![CDATA[xml]]> ,所以导致解析失败。suds.client()调用不需要加CDATA
在这里插入图片描述

  • postman调用,请求方式为post,请求数据与soupui一致都是xml格式。相应数据也与soupUI(soupUI也可以查看请求头等数据)一致。 也可以使用python的requests去调用请求,postman可以将请求翻译成代码,这里就不写样例了。

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值