通过HTTP请求,将XML以SOAP消息的方式发给JWSDP、.NET的webservice
来源:互联网 发布:打鼓谱软件 编辑:程序博客网 时间:2024/06/07 00:26
通过HTTP请求,将XML以SOAP消息的方式发给JWSDP、.NET的webservice
本节将展示一种代码模板,它会以‘手动’方式向JWSDP的webservice(document/literal)传递SOAP消息。后面也会用相同的方法访问 Infobel webservice。
1.XML通过HTTP请求到JWSDP webservice
使用python向web服务器传送一个文件相对容易些。对于JWSDP webservice,不要忘了在消息头中增加Content-type,只有这样,调用才能被服务器正常接收。下面是一个命令行示例,SOAP响应都不加解析地被打印出来。由于返回数据相对复杂,我会在稍后的Infobel web service示例中讲解如何使用PyXml解析SOAP消息。
例3.如何用python传递SOAP消息
在接下来的示例中我们访问readLS(),它带有一个string类型的返回值。
# post方式提交 xml的 soap 消息
import sys, httplib
# a "as lighter as possible" soap message:
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:readLS xmlns:ns1="http://phonedirlux.homeip.net/types">
<String_1>%s</String_1>
</ns1:readLS>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
"""
SoapMessage = SM_TEMPLATE%("Your message or e-mail")
print SoapMessage
#构造并发送消息头
webservice = httplib.HTTP("www.pascalbotte.be")
webservice.putrequest("POST", "/rcx-ws/rcx")
webservice.putheader("Host", "www.pascalbotte.be")
webservice.putheader("User-Agent", "Python post")
webservice.putheader("Content-type", "text/xml; charset=/"UTF-8/"")
webservice.putheader("Content-length", "%d" % len(SoapMessage))
webservice.putheader("SOAPAction", "/"/"")
webservice.endheaders()
webservice.send(SoapMessage)
# 获取返回
statuscode, statusmessage, header = webservice.getreply()
print "Response: ", statuscode, statusmessage
print "headers: ", header
res = webservice.getfile().read()
print res
如果你想使用我的webservice测试这段代码,请点击这里(如果在线)。通常,你可以使用自己喜爱的开发工具做一个客户端,并修改SOAP消息访问别的webservice,用以捕获客户端与服务端的SOAP消息交换。
2.使用PyXml的DOM做一个SOAP消息请求到webservice
如何投递(POST)动态的SOAP消息呢?下面示例介绍怎样使用PyXml的DOM创建SOAP消息:
# post 一个 xml 文件, 使用 DOM 创建SOAP消息
import sys, httplib
from xml.dom import implementation
from xml.dom.ext import PrettyPrint
import StringIO
# 定义所需的名字空间
ec = "http://schemas.xmlsoap.org/soap/encoding/"
soapEnv = "http://schemas.xmlsoap.org/soap/envelope/"
myns = "http://phonedirlux.homeip.net/types"
# DOM 文档
domdoc = implementation.createDocument(None, '', None)
# SOAP 封装名字空间
seObj = domdoc.createElementNS(soapEnv, "SOAP-ENV:Envelope")
seObj.setAttributeNS(soapEnv, "SOAP-ENV:encodingStyle", ec)
# 加到根部
domdoc.appendChild(seObj)
header = domdoc.createElement("SOAP-ENV:Header")
seObj.appendChild(header)
body = domdoc.createElement("SOAP-ENV:Body")
readls = domdoc.createElementNS(myns, "ns1:readLS")
string_1 = domdoc.createElement("String_1")
string_1.appendChild(domdoc.createTextNode("Message created with PyXml, your e-mail"))
readls.appendChild(string_1)
body.appendChild(readls)
seObj.appendChild(body)
soapStr = StringIO.StringIO()
PrettyPrint(domdoc, soapStr)
# 查看SOAP消息
print soapStr.getvalue()
# 构造头部并投递
webservice = httplib.HTTP("www.pascalbotte.be")
webservice.putrequest("POST", "/rcx-ws/rcx")
webservice.putheader("Host", "www.pascalbotte.be")
webservice.putheader("User-Agent", "My post")
webservice.putheader("Content-type", "text/xml; charset=/"UTF-8/"")
webservice.putheader("Content-length", "%d" % len(soapStr.getvalue()))
webservice.putheader("SOAPAction", "/"/"")
webservice.endheaders()
webservice.send(soapStr.getvalue())
# 获取返回
statuscode, statusmessage, header = webservice.getreply()
print "Response: ", statuscode, statusmessage
print "headers: ", header
res = webservice.getfile().read()
print res
转自:http://hi.baidu.com/moatlzy/blog/item/410fe43b5a12ebf83a87ce99.html
- 通过HTTP请求,将XML以SOAP消息的方式发给JWSDP、.NET的webservice
- java调用webservice天气预报(SOAP请求的方式获取天气信息并解析返回的XML)
- java调用webservice天气预报(SOAP请求的方式获取天气信息并解析返回的XML)
- java调用webservice天气预报(SOAP请求的方式获取天气信息并解析返回的XML)
- 将wcf 以webservice的方式调用
- 通过webservice提交xml数据以及soap协议的使用
- webservice调用方式http和soap的区别
- .net 从WebService的restful api接口以POST方式请求并获取返回值
- HTTP 通讯 消息内容以 XML 的方式进行相互之间的通信 实例
- java 调用.net webservice 带soap验证头消息方式
- 以DELETE方式发送带请求体的HTTP请求
- iOS WebService SOAP 请求的实现
- PB9.0 通过http协议以POST方式将XML发送到协议服务器
- 发送xml的http请求并返回xml消息体
- .NET WebService 调试,允许通过IE输入参数的设置,允许Perl以Post方式调用的配置项
- IOS 使用Http模拟SOAP请求Webservice时内容Unicode编码的实现
- Webservice 用http get方式无法请求到的解决办法
- java webservice soap消息请求例子
- 关于STM32的systick定时器的详细说明
- Python Post 请求
- ssh 后台执行java 程序
- STM32 v3.5固件库systick ms延时
- 2年似一场梦
- 通过HTTP请求,将XML以SOAP消息的方式发给JWSDP、.NET的webservice
- 移动应用开发辅助服务推荐
- ARM9存储器
- python
- 游戏化学习法:牛人教你如何赢得谷歌面试
- 给Python中通过urllib2.urlopen获取网页的过程中,添加gzip的压缩与解压缩支持
- 利用搜狗输入法构建企业级云输入法平台
- 模拟登陆网站 之 Python版(内含两种版本的完整的可运行的代码)
- 黑马程序员_学习记录11:多线程