python调用webservice接口

韩非丶 / 2024-03-02 / 原文

最近工作中需要调用webservice接口,经过一番资料查询后,整理出下面几个测试方法

SOAP UI 查看wsdl文件

image-20240302104949793

两个测试网站:

QQ 在线状态查询接口:http://ws.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl

  • 参数:QQ 号码
  • 返回:Y、N、E(Y 代表在线、N 代表离线、E 代码参数有误!)

天气预报查询接口:http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl

可进行测试,选择Request -> run ,可以看到response

image-20240302105059321

用python模拟请求

QQ 在线状态查询接口

import requests
import xml.etree.ElementTree as ET
def test_webservice_qq():
    wsdl_url = "http://ws.webxml.com.cn/webservices/qqOnlineWebService.asmx"
    headers = {'content-type': 'text/xml;charset=UTF-8',
               'User-Agent': 'Apache-HttpClient/4.5.5 (Java/16.0.1)',
               }

    body = """
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://WebXml.com.cn/">
       <soapenv:Header/>
       <soapenv:Body>
          <web:qqCheckOnline>
             <!--Optional:-->
             <web:qqCode>1251865476</web:qqCode>
          </web:qqCheckOnline>
       </soapenv:Body>
    </soapenv:Envelope>
    """

    response = requests.post(wsdl_url, data=body, headers=headers, verify=False)
    print(response.text)

    # 将返回结果转换成Element对象
    root = ET.fromstring(response.text)

    # 定义命名空间
    namespace = {'web': 'http://WebXml.com.cn/'}

    # 找到返回数据的元素
    element = root.find('.//{http://WebXml.com.cn/}qqCheckOnlineResult', namespace)
    print(element)

    if element is None:
        print("No data found")
        return None

    # 提取数据
    data = element.text
    # 打印数据
    print(data)
test_webservice_qq()

天气预报查询接口

def test_weather():
    wsdl_url = "http://ws.webxml.com.cn/WebServices/WeatherWS.asmx"
    headers = {'content-type': 'text/xml;charset=UTF-8',
               'User-Agent': 'Apache-HttpClient/4.5.5 (Java/16.0.1)',
               }

    body = """
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://WebXml.com.cn/">
       <soapenv:Header/>
       <soapenv:Body>
          <web:getRegionCountry/>
       </soapenv:Body>
    </soapenv:Envelope>
       """

    response = requests.post(wsdl_url, data=body, headers=headers, verify=False)
    print(response.text)

    # 将返回结果转换成Element对象
    root = ET.fromstring(response.text)

    # 定义命名空间
    namespace = {'web': 'http://WebXml.com.cn/'}
    # 找到返回数据的元素
    elements = root.findall('.//{http://WebXml.com.cn/}string', namespace)

    for ele in elements:
        # 提取数据
        data = ele.text
        # 打印数据
        print(data)


def test_weather2():
    wsdl_url = "http://ws.webxml.com.cn/WebServices/WeatherWS.asmx"
    headers = {'content-type': 'text/xml;charset=UTF-8',
               'User-Agent': 'Apache-HttpClient/4.5.5 (Java/16.0.1)',
               }

    body = """
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://WebXml.com.cn/">
       <soapenv:Header/>
       <soapenv:Body>
          <web:getRegionDataset/>
       </soapenv:Body>
    </soapenv:Envelope>
       """

    response = requests.post(wsdl_url, data=body, headers=headers, verify=False)
    print(response.text)
test_weather()
# test_weather2()

参考链接:

https://www.jianshu.com/p/881c6f5301d6

https://www.jianshu.com/p/30d2ea3f3772