PB代理模式调用WebService

测试WebService接口

WebService地址:http://localhost:7777/pbservice/testsrv.asmx?wsdl
接口定义: public function string addstr (string s1, string s2)
方法名:addstr
入参类型:字符串s1 和 s2
返回值:两个字符串入参拼接的结果
使用SoapUI工具调试接口,参数s1传入abc,参数s2传入def,返回两个字符串拼接结果 abcdef
在这里插入图片描述

创建SoapClient代理类和XMLHTTP代理类

n_ole_proxy 代码:

$PBExportHeader$n_ole_proxy.sru
forward
global type n_ole_proxy from nonvisualobject
end type
end forward

global type n_ole_proxy from nonvisualobject
event connect_error ( string object_name )
end type
global n_ole_proxy n_ole_proxy

type variables
protected:
	oleobject obj
end variables

forward prototypes
public function oleobject get_object ()
end prototypes

event connect_error(string object_name);runtimeerror e
e = create runtimeerror
e.text = "Error connect to object " + object_name
throw e
end event

public function oleobject get_object ();return obj
end function

on n_ole_proxy.create
call super::create
TriggerEvent( this, "constructor" )
end on

on n_ole_proxy.destroy
TriggerEvent( this, "destructor" )
call super::destroy
end on

event destructor;if isvalid(obj) then
	obj.disconnectObject()
	destroy obj
end if
end event

event constructor;obj = create oleobject
end event


n_ole_soapclient 代码:

$PBExportHeader$n_ole_soapclient.sru
forward
global type n_ole_soapclient from n_ole_proxy
end type
end forward

global type n_ole_soapclient from n_ole_proxy
end type
global n_ole_soapclient n_ole_soapclient

type variables
protected:
	oleobject ole_soap
	
public:
//	connector property
//	string AuthPassword 				//The password used for end point authentication.
//	string AuthUser 						//The user name used for end point authentication.
//	string EndPointURL 					//The end point URL.
	string ProxyPassword 				//The password used for proxy authentication.
//	uint ProxyPort 						//The port of the proxy server to use.
	string ProxyServer 					//The IP address or host name of the proxy server.
	string ProxyUser 						//The user name used for proxy authentication.
//	string SOAPAction 					//The value used in the SOAPAction HTTP header.
	string SSLClientCertificateName 	//A string identifying the client certificate to use for the Secure Sockets Layer (SSL) protocol, if any. The syntax is:
												//		[CURRENT_USER | LOCAL_MACHINE\[store-name\]]cert-name
												//		with the defaults being CURRENT_USER\MY (the same store that Microsoft Internet Explorer uses).
	long Timeout 							//The timeout for HttpConnector. This timeout is in milliseconds.
	boolean UseSSL 						//	A Boolean value (true or false) that specifies the use of SSL.
	
end variables

forward prototypes
public function integer soap_init (string url)
end prototypes

public function integer soap_init (string url);
try
	ole_soap.mssoapinit(url)
catch(RuntimeError err)
	return -1
end try

//set connector properties
if not (isnull(ProxyPassword) or ProxyPassword='') then
	ole_soap.ConnectorProperty("ProxyPassword",ProxyPassword)
end if
if not (isnull(ProxyServer) or ProxyServer='') then
	ole_soap.ConnectorProperty("ProxyServer",ProxyServer)
end if
if not (isnull(ProxyUser) or ProxyUser='') then
	ole_soap.ConnectorProperty("ProxyUser",ProxyUser)
end if
if not (isnull(SSLClientCertificateName) or SSLClientCertificateName='') then
	ole_soap.ConnectorProperty("SSLClientCertificateName",SSLClientCertificateName)
end if
if Timeout > 0 then
	ole_soap.ConnectorProperty("Timeout",Timeout)
end if
if UseSSL then
	ole_soap.ConnectorProperty("UseSSL",UseSSL)
end if

return 0
end function

on n_ole_soapclient.create
call super::create
end on

on n_ole_soapclient.destroy
call super::destroy
end on

event constructor;call super::constructor;ole_soap = obj

if ole_soap.ConnectToNewObject("MSSOAP.SoapClient30") = 0 then return 0
if ole_soap.ConnectToNewObject("MSSOAP.SoapClient") = 0  then return 0

event connect_error("MSSOAP.SoapClient")
return -1
end event


n_ole_xmlhttp 代码:

$PBExportHeader$n_ole_xmlhttp.sru
forward
global type n_ole_xmlhttp from n_ole_proxy
end type
end forward

global type n_ole_xmlhttp from n_ole_proxy
end type
global n_ole_xmlhttp n_ole_xmlhttp

type variables
private:
	oleobject ole_xmlhttp
	string url_address

public:
	CONSTANT STRING METHOD_POST = "POST"
	CONSTANT STRING METHOD_GET = "GET"
	
	string method = METHOD_POST
	string content_type= "text/xml"
	
	long Timeout
end variables
forward prototypes
public function string send (string content)
public subroutine set_url (string url)
end prototypes

public function string send (string content);
try
	ole_xmlhttp.Open(method,url_address,True)
catch (runtimeerror e)
	return ''
end try

ole_xmlhttp.setRequestHeader("Content-Type",content_type)

ole_xmlhttp.send(content)

long ll_cpu
ll_cpu = cpu()
Do While ole_xmlhttp.readyState <> 4
	if Timeout > 0 and cpu() - ll_cpu > Timeout then
		ole_xmlhttp.abort()
		return ''
	end if
	Yield()
Loop

return ole_xmlhttp.responseText
end function

public subroutine set_url (string url);url_address = url
end subroutine

on n_ole_xmlhttp.create
call super::create
end on

on n_ole_xmlhttp.destroy
call super::destroy
end on

event constructor;call super::constructor;ole_xmlhttp = obj

if ole_xmlhttp.ConnectToNewObject("Msxml2.XMLHTTP.6.0") = 0 then return 0
if ole_xmlhttp.ConnectToNewObject("Msxml2.XMLHTTP.3.0") = 0  then return 0

event connect_error("Msxml2.XMLHTTP.3.0")
return -1
end event


创建测试接口代理

SoapClient方式调用

新增接口代理类继承n_ole_soapclient,constructor事件添加代码:

string ls_url

ls_url  = 'http://localhost:7777/pbservice/testsrv.asmx?wsdl'
soap_init(ls_url)

新增方法addstr,参数和返回值类型与目标接口一致(参数 string s1, string s2,返回string),添加代码:

return ole_soap.addstr(s1,s2)

测试效果:
在这里插入图片描述

XMLHTTP方式调用

新增接口代理类继承n_ole_xmlhttp,constructor事件添加代码:

string ls_url

ls_url = 'http://localhost:7777/pbservice/testsrv.asmx'
set_url(ls_url)

新增方法addstr,参数和返回值类型与目标接口一致(参数 string s1, string s2,返回string),添加代码:

string ls_request
string ls_response

//组装报文
ls_request = '<?xml version="1.0" encoding="utf-8"?>'+&
					'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'+&
					'  <soap:Body>'+&
					'    <addstr xmlns="http://tempurl.org">'+&
					'      <s1>'+s1+'</s1>'+&
					'      <s2>'+s2+'</s2>'+&
					'    </addstr>'+&
					'  </soap:Body>'+&
					'</soap:Envelope>'

//调用接口
ls_response = this.send(ls_request)

//解析返回
n_xml lxm_response
lxm_response = create n_xml
lxm_response.parse(ls_response)

return lxm_response.getitempath('/soap:Envelope/soap:Body/addstrResponse/addstrResult')

测试效果:
在这里插入图片描述

说明

本案例将连接SoapClient和XMLHTTP的代码封装到基类中,在需要的情况下继承使用,避免了大量重复代码的拷贝。
对于一个调用流程复杂的接口,可以采用代理模式将调用流程封装,提供出简单接口给外部程序使用,让程序流程更加清晰。
发现BUG请留言或私信,以便修正(QQ:768310524 TEL:18649713925)

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值