方法主要是利用类的继承
继承WEB服务的代理类,然后添加新的方法,实现对SOAP请求信封的头内添加信息。
以下是生成的继类的代码,继承类的名为GetHttpHeadersProxy.vb
Imports System.Net
Imports System.Collections.Specialized
Public Class GetHttpHeadersProxy
Inherits getheaders.header
Private headerstoadd As NameValueCollection
Public Sub New()
headerstoadd = New NameValueCollection
End Sub
Public Sub addhttpheader(ByVal headername As String, ByVal headervalue As String)
headerstoadd.Add(headername, headervalue)
End Sub
Protected Overrides Function GetWebRequest(ByVal uri As Uri) As WebRequest
Dim request As WebRequest
request = MyBase.GetWebRequest(uri)
request.Headers.Add(headerstoadd)
Return request
End Function
End Class
Imports System.Collections.Specialized
Public Class GetHttpHeadersProxy
Inherits getheaders.header
Private headerstoadd As NameValueCollection
Public Sub New()
headerstoadd = New NameValueCollection
End Sub
Public Sub addhttpheader(ByVal headername As String, ByVal headervalue As String)
headerstoadd.Add(headername, headervalue)
End Sub
Protected Overrides Function GetWebRequest(ByVal uri As Uri) As WebRequest
Dim request As WebRequest
request = MyBase.GetWebRequest(uri)
request.Headers.Add(headerstoadd)
Return request
End Function
End Class
其中用绿色底高亮显示的地方为对WEB服务生成的引用类
在使用时如下方法
dim proxy as GetHttpHeadersProxy=new GetHttpHeadersProxy
proxy.AddHttpHeader("name","aowind")
proxy.AddHttpHeader("sex","man")
proxy.AddHttpHeader("name","aowind")
proxy.AddHttpHeader("sex","man")