调用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
利用python 组装http请求 调用webservice 接口
最新推荐文章于 2024-01-23 13:17:25 发布