Visual Basic 8 (2005)WebFormsVisual Studio 2005.NET 2.0初学者开发Visual StudioWindows.NETVisual BasicASP.NET
使用 HTTP POST 的通用 Web 服务客户端
无需添加 Web 引用即可调用 Web 服务方法。
引言
本文档介绍了如何在不向项目添加 Web 引用的情况下使用 Web 服务。
使用代码
使用 WSRequestHelper
类来使用 Web 服务。这已经在 一些公共 Web 服务上进行了测试。
属性
URL
:Web 服务的 URL;例如:https:///Project.WS.Auth/AuthService.asmx。Method
:要调用的方法;例如:Login
。Parameters
:调用所需的参数。
方法
CallWebServiceReturnString
:调用 Web 服务并将响应作为字符串返回。CallWebService
:调用 Web 服务并将响应作为 XML 文档返回。
' Constructor for the Class
'The object that will manage the Soap Request
Dim SoapHelper As New WSRequestHelper(Me.txtWSDLAddress.Text, Me.txtMethod.Text)
'Calling the Web Service
Me.txtWSResponse.Text = SoapHelper.CallWebService().InnerText
' Core of the WSRequestHelper Class
'Create the WebClient
Dim Request As New WebClient
'Set up the request
Request.Headers.Add("Content-Type", "application/x-www-form-urlencoded")
Making the Call inside the class
'Request to the WS
Dim Req As WebClient = CreateRequest()
'Soap Message to send
Dim MessageToPost As String = CreateMessage()
Return Req.UploadString(Me.PostURL, "POST", MessageToPost)
'Request to the WS
Dim Req As WebClient = CreateRequest()
关注点
对 Web 服务的调用使用 HTTP POST 进行请求,因此某些方法可能无法使用这种方法调用。该代码尚未针对复杂类型进行测试。要使用 SOAP,请使用 Runtime.Serialization.Formatters.SoapMessage
类;它有效。我也将其与一些 Cold Fusion Web 服务一起使用过。