Imports System.Web
Imports System.Xml
Imports System.Collections
Imports System.Net
Imports System.Text
Imports System.IO
Imports System.Xml.Serialization
'By huangz 2008-3-19
'
''' <summary>
''' 利用WebRequest/WebResponse进行WebService调用的类,By 同济黄正 http://hz932.ys168.com 2008-3-19
''' </summary>
Public Class WebSvcCaller
'<webServices>
' <protocols>
' <add name="HttpGet"/>
' <add name="HttpPost"/>
' </protocols>
'</webServices>
Private Shared _xmlNamespaces As New Hashtable()
'缓存xmlNamespace,避免重复调用GetNamespace
'
''' <summary>
''' 需要WebService支持Post调用
''' </summary>
Public Shared Function QueryPostWebService(URL As [String], MethodName As [String], Pars As Hashtable) As XmlDocument
Dim request As HttpWebRequest = DirectCast(HttpWebRequest.Create(URL & "/" & MethodName), HttpWebRequest)
request.Method = "POST"
request.C
SetWebRequest(request)
Dim data As Byte() = EncodePars(Pars)
WriteRequestData(request, data)
Return ReadXmlResponse(request.GetResponse())
End Function
'
''' <summary>
''' 需要WebService支持Get调用
''' </summary>
Public Shared Function QueryGetWebService(URL As [String], MethodName As [String], Pars As Hashtable) As XmlDocument
Dim request As HttpWebRequest = DirectCast(HttpWebRequest.Create(URL & "/" & MethodName & "?" & ParsToString(Pars)), HttpWebRequest)
request.Method = "GET"
request.C
SetWebRequest(request)
Return ReadXmlResponse(request.GetResponse())
End Function
'
''' <summary>
''' 通用WebService调用(Soap),参数Pars为String类型的参数名?参数值
''' </summary>
Public Shared Function QuerySoapWebService(URL As [String], MethodName As [String], Pars As Hashtable) As XmlDocument
If _xmlNamespaces.ContainsKey(URL) Then
Return QuerySoapWebService(URL, MethodName, Pars, _xmlNamespaces(URL).ToString())
Else
Return QuerySoapWebService(URL, MethodName, Pars, GetNamespace(URL))
End If
End Function
Private Shared Function QuerySoapWebService(URL As [String], MethodName As [String], Pars As Hashtable, XmlNs As String) As XmlDocument
'By 同济黄正 http://hz932.ys168.com 2008-3-19
_xmlNamespaces(URL) = XmlNs
'加入缓存,提高效率
Dim request As HttpWebRequest = DirectCast(HttpWebRequest.Create(URL), HttpWebRequest)
request.Method = "POST"
request.C
request.Headers.Add("SOAPAction", """" & XmlNs & (If(XmlNs.EndsWith("/"), "", "/")) & MethodName & """")
SetWebRequest(request)
Dim data As Byte() = EncodeParsToSoap(Pars, XmlNs, MethodName)
WriteRequestData(request, data)
Dim doc As New XmlDocument(), doc2 As New XmlDocument()
doc = ReadXmlResponse(request.GetResponse())
Dim mgr As New XmlNamespaceManager(doc.NameTable)
mgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/")
Dim RetXml As [String] = doc.SelectSingleNode("//soap:Body/*/*", mgr).InnerXml
doc2.LoadXml("<root>" & RetXml & "</root>")
AddDelaration(doc2)
Return doc2
End Function
Private Shared Function GetNamespace(URL As [String]) As String
Dim request As HttpWebRequest = DirectCast(WebRequest.Create(URL & "?WSDL"), HttpWebRequest)
SetWebRequest(request)
Dim response As WebResponse = request.GetResponse()
Dim sr As New StreamReader(response.GetResponseStream(), Encoding.UTF8)
Dim doc As New XmlDocument()
doc.LoadXml(sr.ReadToEnd())
sr.Close()
Return doc.SelectSingleNode("//@targetNamespace").Value
End Function
Private Shared Function EncodeParsToSoap(Pars As Hashtable, XmlNs As [String], MethodName As [String]) As Byte()
Dim doc As New XmlDocument()
doc.LoadXml("<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:Envelope>")
AddDelaration(doc)
Dim soapBody As XmlElement = doc.CreateElement("soap", "Body", "http://schemas.xmlsoap.org/soap/envelope/")
Dim soapMethod As XmlElement = doc.CreateElement(MethodName)
soapMethod.SetAttribute("xmlns", XmlNs)
For Each k As String In Pars.Keys
Dim soapPar As XmlElement = doc.CreateElement(k)
soapPar.InnerXml = ObjectToSoapXml(Pars(k))
soapMethod.AppendChild(soapPar)
Next
soapBody.AppendChild(soapMethod)
doc.DocumentElement.AppendChild(soapBody)
Return Encoding.UTF8.GetBytes(doc.OuterXml)
End Function
Private Shared Function ObjectToSoapXml(o As Object) As String
Dim mySerializer As New XmlSerializer(o.[Gettype]())
Dim ms As New MemoryStream()
mySerializer.Serialize(ms, o)
Dim doc As New XmlDocument()
doc.LoadXml(Encoding.UTF8.GetString(ms.ToArray()))
If doc.DocumentElement IsNot Nothing Then
Return doc.DocumentElement.InnerXml
Else
Return o.ToString()
End If
End Function
Private Shared Sub SetWebRequest(request As HttpWebRequest)
request.Credentials = CredentialCache.DefaultCredentials
request.Timeout = 10000
End Sub
Private Shared Sub WriteRequestData(request As HttpWebRequest, data As Byte())
request.ContentLength = data.Length
Dim writer As Stream = request.GetRequestStream()
writer.Write(data, 0, data.Length)
writer.Close()
End Sub
Private Shared Function EncodePars(Pars As Hashtable) As Byte()
Return Encoding.UTF8.GetBytes(ParsToString(Pars))
End Function
Private Shared Function ParsToString(Pars As Hashtable) As [String]
Dim sb As New StringBuilder()
For Each k As String In Pars.Keys
If sb.Length > 0 Then
sb.Append("&")
End If
sb.Append(HttpUtility.UrlEncode(k) & "=" & HttpUtility.UrlEncode(Pars(k).ToString()))
Next
Return sb.ToString()
End Function
Private Shared Function ReadXmlResponse(response As WebResponse) As XmlDocument
Dim sr As New StreamReader(response.GetResponseStream(), Encoding.UTF8)
Dim retXml As [String] = sr.ReadToEnd()
sr.Close()
Dim doc As New XmlDocument()
doc.LoadXml(retXml)
Return doc
End Function
Private Shared Sub AddDelaration(doc As XmlDocument)
Dim decl As XmlDeclaration = doc.CreateXmlDeclaration("1.0", "utf-8", Nothing)
doc.InsertBefore(decl, doc.DocumentElement)
End Sub
End Class
袍哥按照你说的,我将c#转vb.net代码后,复制到全局代码中,红色的部分报错,我应该怎么改一下呢?
急需得到你的指点啊,要不晚上睡不好觉觉啊