您的位置:首页 > 理论基础 > 计算机网络

Web Service系列之实例之使用http.client发送SOAP POST请求

2017-04-23 22:09 447 查看
原文链接: Web Service系列之实例之使用http.client发送SOAP POST请求

本文只给出代码, 更多内容请查看本系列另外一篇文章, Web Service系列之实例之使用urllib发送SOAP POST请求

完整代码:

import sys, http.client
import urllib.request

host = "localhost:9000"

SM_TEMPLATE = """<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<ns1:helloWorld xmlns:ns1="http://webservice.kylinux.com/">
<arg0>%s</arg0>
<arg0>%s</arg0>
</ns1:helloWorld>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
"""

SoapMessage = SM_TEMPLATE%("Kylin", "Shu")

#print(SoapMessage)

#construct and send the header
webservice = http.client.HTTPConnection(host)
headers = {"Content-type": "text/xml; charset=\"UTF-8\""}
webservice.request("POST", "/WS/HelloWorld/", body=SoapMessage, headers=headers)
#headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}

response = webservice.getresponse()
#print(response.status, response.reason)
data = response.read()
print(data)
webservice.close()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐