加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

gSoap 的简单应用

发布时间:2020-12-17 01:18:36 所属栏目:安全 来源:网络整理
导读:最近关注一些 Application Server 开发的问题,试用了一下 gSoap。 ? gSoap 实际上一个编译器,包含两个工具: ? wsdl2h 用于将 WSDL 文件转换成头文件 ? soapcpp2 用于将头文件转换成编写 SOAP 服务器和客户端所需要的文件 ? 还是从最简单的 HelloWorld 程

最近关注一些 Application Server 开发的问题,试用了一下 gSoap。

?

gSoap 实际上一个编译器,包含两个工具:

?

wsdl2h 用于将 WSDL 文件转换成头文件

?

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。

?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读