最近关注一些 Application Server 开发的问题,试用了一下 gSoap。
gSoap 实际上一个编译器,包含两个工具:
soapcpp2 用于将头文件转换成编写 SOAP 服务器和客户端所需要的文件
还是从最简单的 HelloWorld 程序来认识 gSoap
下面的是实现两个整数相加的 SOAP 程序
//gsoap ns service name: add
//gsoap ns service namespace: http://192.168.88.1/add.wsdl
//gsoap ns service location: http://192.168.88.1
//gsoap ns service executable: add.cgi
//gsoap ns service encoding: encoded
//gsoap ns schema namespace: urn:add
int ns__add( int num1,int num2,int* sum );
需要执行的过程是:
soapcpp2.exe add.h
然后编写服务器和客户端的代码。
为了进一步了解 SOAP 底层的过程,可以截取数据包进行查看
客户端发生的信息:
POST / HTTP/1.0
Host: 192.168.88.1
User-Agent: gSOAP/2.1
Content-Type: text/xml; charset=utf-8
Content-Length: 438
SOAPAction: ""
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:ns="urn:add" xmlns="urn:add" SOAP-ENV:encodingStyle="encoded">
<SOAP-ENV:Body>
<ns:add>
<num1>2</num1>
<num2>5</num2>
</ns:add>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
服务器的响应信息:
HTTP/1.0 200 OK
Server: gSOAP/2.1
Content-Type: text/xml; charset=utf-8
Content-Length: 438
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:ns="urn:add" xmlns="urn:add" SOAP-ENV:encodingStyle="encoded">
<SOAP-ENV:Body><ns:addResponse>
<sum>7</sum>
</ns:addResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
得到实际的 HTTP 信息之后,直接利用 python 可以很容易写出客户端的代码
import urllib2
import sys,httplib
def SendRtx(num1,num2):
SENDTPL = /
'''<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns="urn:add" xmlns="urn:add" SOAP-ENV:encodingStyle="encoded">
<SOAP-ENV:Body>
<ns:add>
<num1>%s</num1>
<num2>%s</num2>
</ns:add>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>'''
SoapMessage = SENDTPL % (num1,num2)
webservice = httplib.HTTP("192.168.88.1",4567)
webservice.putrequest("POST","/")
webservice.putheader("Host","192.168.88.1")
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)
# get the response
statuscode,statusmessage,header = webservice.getreply()
print "Response: ",statuscode,statusmessage
print "headers: ",header
print webservice.getfile().read()
SendRtx(1,1)
之后尝试利用 jQuery 编写 ajax 代码来访问 SOAP 服务,悲剧的是居然有兼容性问题,只有 IE 能够正常调用。
像 firefox 和 chrome 都会发送一些糟糕的 Internal Server Error。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。