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

来源:互联网 发布:字符串压缩 java 编辑:程序博客网 时间:2024/06/05 07:20

原文链接: Web Service系列之实例之使用http.client发送SOAP POST请求

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

完整代码:
import sys, http.clientimport urllib.requesthost = "localhost:9000"SM_TEMPLATE = """<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:EnvelopeSOAP-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 headerwebservice = 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()
1 0