VFP MSSOAPTOOKIT 使用SOAP Headers
.NET 有如下使用了自定义扩展HEADER来做验证 server.asmx代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using QiyuSoft.BLL;
using System.Web.Services.Protocols;
namespace DynamicIPService
{
/// <summary>
/// Service1 的摘要说明
/// </summary>
[WebService(Namespace = "http://www.tab163.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class DynamicIPService : System.Web.Services.WebService
{
public SOAPAuthHeader sHeader=new SOAPAuthHeader();
[SoapHeader("sHeader")]
[WebMethod]
public string HelloWorld()
{
//return sHeader.EncodedMustUnderstand12.ToString();
if (sHeader == null)
{
return "没有登录";
}
else
{
if (sHeader.UserName == "123")
return "Hello World";
else
return "用户名:"+sHeader.UserName + "没有登录";
}
}
}
}
自定义扩展Header类
public class SOAPAuthHeader : SoapHeader
{
public string UserName;
public string UserPwd;
}
VFP 要调用HelloWorld 要必须实现自定义SOAP Header头
如下调用方法
sc1 =CreateObject("MSSOAP.SoapClient")
sc1.mssoapinit ("http://localhost:8080/DynamicIPService.asmx?wsdl","DynamicIPService","DynamicIPServiceSoap")
sc1.HeaderHandler = 自定义头com组件
?sc1.helloworld()
查阅文档,要实现自定义头,要就实现IHeaderHandler接口
新建VFP项目
*-- 定义了USER_ELEMENT_1-3个元素,及 USER_ELEMENT_1_VALUE 值
Define Class MyData As Session OlePublic
Implements IHeaderHandler IN MSSOAP.ConnectorFactory30
HEADER_ELEMENT_NAMESPACE = "http://tempuri.org/"
HEADER_ELEMENT_NAME = "SOAPAuthHeader"
USER_ELEMENT_1 = ""
USER_ELEMENT_2 =""
USER_ELEMENT_3 =""
USER_ELEMENT_1_VALUE =""
USER_ELEMENT_2_VALUE =""
USER_ELEMENT_3_VALUE =""
Function IHeaderHandler_readHeader(pReader As SoapReader30, pHeaderNode As MSXML2.IXMLDOMNode, pObject As Object)
If pHeaderNode.baseName <> HEADER_ELEMENT_NAME Or pHeaderNode.namespaceURI <> HEADER_ELEMENT_NAMESPACE
IHeaderHandler_readHeader = .F.
RETURN
Endif
*this.UserName= pHeaderNode.selectSingleNode("UserName").Text
IHeaderHandler_readHeader = .T.
Endfunc
Function IHeaderHandler_willWriteHeaders()
IHeaderHandler_willWriteHeaders = .T.
Endfunc
Function IHeaderHandler_writeHeaders(pSerializer As MSSOAPLib30.ISoapSerializer,pObject As Object)
pSerializer.startHeaderElement(this.HEADER_ELEMENT_NAME,this.HEADER_ELEMENT_NAMESPACE)
IF !EMPTY(this.USER_ELEMENT_1)
pSerializer.StartElement(this.USER_ELEMENT_1,this.HEADER_ELEMENT_NAMESPACE)
pSerializer.writestring(this.USER_ELEMENT_1_VALUE)
pSerializer.endelement()
ENDIF
IF !EMPTY(this.USER_ELEMENT_2)
pSerializer.StartElement(this.USER_ELEMENT_2,this.HEADER_ELEMENT_NAMESPACE)
pSerializer.writestring(this.USER_ELEMENT_2_VALUE)
pSerializer.endelement()
ENDIF
IF !EMPTY(this.USER_ELEMENT_2)
pSerializer.StartElement(this.USER_ELEMENT_2,this.HEADER_ELEMENT_NAMESPACE)
pSerializer.writestring(this.USER_ELEMENT_2_VALUE)
pSerializer.endelement()
ENDIF
pSerializer.EndHeaderElement()
Endfunc
Enddefine
将项目编译成dll,然后在程序中调用
sc1 =CreateObject("MSSOAP.SoapClient")
sc1.mssoapinit ("http://localhost:8080/DynamicIPService.asmx?wsdl","DynamicIPService","DynamicIPServiceSoap")
sh1=CREATEOBJECT("soapheader.mydata")
sh1.HEADER_ELEMENT_NAMESPACE ="http://www.tab163.com/" &&这个很重要,我调试了很久,名称空间不能写错了
sh1.USER_ELEMENT_1 ="UserName"
sh1.USER_ELEMENT_1_Value ="123"
sc1.HeaderHandler = sh1
?sc1.helloworld()
返回helloworld,至此成功.