利用python 组装http请求 调用webservice 接口

调用webservice 接口一般有以下几种方法:
一。 使用gsoap 工具生成客户端代理调用代码。
	其中会用到 wdsl2h 和 soapcpp2 工具, 此种办法在c++ 工程中常见。
二。 使用python 提供的suds 库调用接口。
	from suds.client import Client
	url = 'http://ip:port/?wsdl'
	cilent=Client(url)
	print cilent 
以上两种方法在通常情况下是可以使用的。我在使用的过程中发现,如果服务器提供的接口中调用了其他服务接口,上面两种方法可能行不通, 生成的接口不正常, 或者是抛出异常。 具体的原因, 如果大家发现了可以在评论区提出,一起讨论。
下面我们自己组装http 请求可以实现同样的功能, 此种方法应该是最通用的方法, 不过要自己解析响应。
大体格式如下:
def InvokeWebservice(phone,msg):
    texturl='http://ip:port/?wsdl'
    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+='</SendShortMes
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值