惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

云风的 BLOG
云风的 BLOG
IT之家
IT之家
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
博客园 - 司徒正美
美团技术团队
Last Week in AI
Last Week in AI
月光博客
月光博客
博客园 - 叶小钗
MongoDB | Blog
MongoDB | Blog
U
Unit 42
T
Tailwind CSS Blog
GbyAI
GbyAI
T
The Blog of Author Tim Ferriss
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
H
Hackread – Cybersecurity News, Data Breaches, AI and More
酷 壳 – CoolShell
酷 壳 – CoolShell
Google DeepMind News
Google DeepMind News
H
Help Net Security
Hugging Face - Blog
Hugging Face - Blog
爱范儿
爱范儿
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
Netflix TechBlog - Medium
B
Blog RSS Feed
大猫的无限游戏
大猫的无限游戏
aimingoo的专栏
aimingoo的专栏
A
About on SuperTechFans
Y
Y Combinator Blog
罗磊的独立博客
D
DataBreaches.Net
有赞技术团队
有赞技术团队
MyScale Blog
MyScale Blog
博客园_首页
博客园 - 三生石上(FineUI控件)
G
Google Developers Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
雷峰网
雷峰网
博客园 - 【当耐特】
Engineering at Meta
Engineering at Meta
博客园 - Franky
M
MIT News - Artificial intelligence
B
Blog
The Cloudflare Blog
Apple Machine Learning Research
Apple Machine Learning Research
I
InfoQ
S
SegmentFault 最新的问题
F
Fortinet All Blogs
阮一峰的网络日志
阮一峰的网络日志
Stack Overflow Blog
Stack Overflow Blog
Microsoft Security Blog
Microsoft Security Blog

博客园 - DingJun

Javascript 继承方法3 Javascript 继承方法2 Javascript 继承方法1 Cannot load type (加载页面出错) 几个主流的浏览器引擎及判定 防止数据库日志文件增长 配置发布数据库服务器时碰到错误18483 一些有关。NET界面处理与多线程的文章 不可恢复的生成错误 在.Net安装项目中如何判断操作系统的版本 修改dataConfiguration.config文件 SQL Server 使用外部连接 在.NET下利用目录服务操纵本机用户和用户组 System.windows.forms.datagrid控件使用技巧 读取配置文件中的自定义节 区域设置与格式化(1) 默认的 IIS MIME 类型关联 在.NET中使用XPath查找指定元素时遇到的麻烦(以dataConfiguration.config为例) 使用Data access block
Call web service from excel
DingJun · 2007-08-31 · via 博客园 - DingJun

最近有个项目需要从Excel中调用Web Service, Google 了一翻果然可以,总结如下:
一、安装开发包。
      要从Excel中通过宏调用Web Service,需要安装Microsoft Office 2003 Web Services Toolkit 2.01,可从这里下载,下载
二、调用Web Service
      安装好开发包以后,调用Web Service就变得很容易了,操作跟在VS 2003中差不多。切换到宏编辑器(Visual Basic Editor),从Tools菜单中点击“Web Service Reference”打开添加Web service引用的对话框。添加好引用后,开发包就会自动为你封装好一些用Soap调用Web Service的相关类。调用这些类中的相关方法就可以调用Web Service方法了。
三、读写SoapHeader内容
有些Web Service需要提供SoapHeader内容,比如通过SoapHeader提供帐号信息以供服务端验证客户身份。下面就是一个例子
服务端:
自定义SoapHeader类:

    public class ServiceHead : System.Web.Services.Protocols.SoapHeader
    
{
        
private string userName;
        
private string password;

        
public string UserName
        
{
            
get return userName; }
            
set { userName = value; }
        }

        
public string Password
        
{
            
get return password; }
            
set { password = value; }
        }

    }

Web Service方法:

[WebMethod(Description = "User name and password are reuired!")]
[SoapHeader(
"accountHead")]
public MonthPriceCol GetPriceInfo(string pSymbol,string pBeginDate,string pEndDate)
{
   .
   
if(!CheckUserIdentity(accountHead)) return null;
   .
}

客户端:
定义一个专门的类用于封装对SoapHeader的操作,类名为clsAccountHeader:


Option Explicit
Implements IHeaderHandler

Private Const NameSpace As String = "http://tempuri.org/"
Private myUserName As String
Private myPassword As String

Public Property Let UserName(ByVal name As String)
    myUserName 
= name
End Property


Public Property Get UserName() As String
    UserName 
= myUserName
End Property


Public Property Let Password(ByVal password1 As String)
    myPassword 
= password1
End Property


Public Property Get Password() As String
    Password 
= myPassword
End Property



Private Function IHeaderHandler_ReadHeader( _
    
ByVal pReader As SoapReader30, _
    
ByVal pHeaderNode As MSXML2.IXMLDOMNode, _
    
ByVal pObject As Object) _
    
As Boolean
    
    IHeaderHandler_ReadHeader 
= False
End Function


Private Function IHeaderHandler_WillWriteHeaders() As Boolean
    IHeaderHandler_WillWriteHeaders 
= True
End Function



'<ServiceHead xmlns="http://tempuri.org/">
'
      <UserName>string</UserName>
'
      <Password>string</Password>
'
</ServiceHead>
Private Sub IHeaderHandler_WriteHeaders(ByVal pSerializer As SoapSerializer30, ByVal pObject As Object)
    
    
'Dim doc As New MSXML2.DOMDocument40
    
    
'Create the string for UserInfoRouteHeader.
    Dim userDoc As String
    userDoc 
= "<ServiceHead xmlns=""" & NameSpace & """>"
    
    
If myUserName <> "" Then
        userDoc 
= userDoc & "<UserName>" & myUserName & "</UserName>"
    
End If
    
    
If myPassword <> "" Then
        userDoc 
= userDoc & "<Password>" & myPassword & "</Password>"
    
End If
    
    userDoc 
= userDoc & "</ServiceHead>"
    
    pSerializer.WriteXml userDoc
End Sub


在调用Web Service方法时提供SoapHeader内容:

Private Sub Class_Initialize()
    
'*****************************************************************
    'This subroutine will be called each time the class is instantiated.
    'Creates sc_ComplexTypes as new SoapClient30, and then
    'initializes sc_ComplexTypes.mssoapinit2 with WSDL file found in
    'http://172.20.18.191/StockService/StockService.asmx?wsdl.
    '*****************************************************************

    
Dim myAccount As New clsAccountHeader
    
Dim str_WSML As String
    str_WSML 
= "<servicemapping>"
    str_WSML 
= str_WSML & "<service name='StockService'>"
    str_WSML 
= str_WSML & "<using PROGID='MSOSOAP.GenericCustomTypeMapper30' cachable='0' ID='GCTM'/>"
    str_WSML 
= str_WSML & "<types>"
    str_WSML 
= str_WSML & "<type name='StockMonthPrice' targetNamespace='http://tempuri.org/' uses='GCTM' targetClassName='struct_StockMonthPrice'/>"
    str_WSML 
= str_WSML & "</types>"
    str_WSML 
= str_WSML & "</service>"
    str_WSML 
= str_WSML & "</servicemapping>"

    
Set sc_StockService = New SoapClient30

    sc_StockService.MSSoapInit2 c_WSDL_URL, str_WSML, c_SERVICE, c_PORT, c_SERVICE_NAMESPACE
    
'Use the proxy server defined in Internet Explorer's LAN settings by
    'setting ProxyServer to <CURRENT_USER>
    sc_StockService.ConnectorProperty("ProxyServer"= "<CURRENT_USER>"
    
'Autodetect proxy settings if Internet Explorer is set to autodetect
    'by setting EnableAutoProxy to True
    sc_StockService.ConnectorProperty("EnableAutoProxy"= True

    
Set sheet = ActiveWorkbook.Sheets("Tickers")
    myAccount.UserName 
= sheet.txtEmail.Value
    myAccount.Password 
= sheet.txtPassword.Value
    
Set sc_StockService.HeaderHandler = myAccount
    
    
Set sc_StockService.ClientProperty("GCTMObjectFactory"= New clsof_Factory_StockService

End Sub

关于读SoapHeader的实现可参考婷篇文章:利用 SOAP 头保持 EJB 状态(http://www.ibm.com/developerworks/cn/webservices/ws-ejbsoap/index.html