调用webservice接口代码demo
时间: 2023-11-01 20:54:16 浏览: 44
这是一个使用 Python 的 requests 库调用 WebService 接口的示例代码:
```python
import requests
# 设置请求头
headers = {'Content-Type': 'text/xml; charset=utf-8'}
# 设置请求体
body = """
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.webserviceX.NET">
<soapenv:Header/>
<soapenv:Body>
<web:GetWeather>
<web:CityName>Beijing</web:CityName>
<web:CountryName>China</web:CountryName>
</web:GetWeather>
</soapenv:Body>
</soapenv:Envelope>
"""
# 发送POST请求
response = requests.post('http://www.webservicex.net/globalweather.asmx', headers=headers, data=body)
# 打印响应结果
print(response.content.decode('utf-8'))
```
这个示例代码调用了一个公开的 WebService 接口,获取了北京的天气信息。你可以根据自己的实际需求来修改请求头、请求体和请求地址。