Visual Basic 8 (2005)WebFormsVisual Studio 2005.NET 2.0BeginnerDevVisual StudioWindows.NETVisual BasicASP.NET
Universal Web Services Consumer Using HTTP POST
Call Web Services methods without adding a web reference.
Introduction
This article explains how to consume Web Services without adding a Web Reference to the project.
Using the code
Use the WSRequestHelper
class to consume the Web Service. This has been tested on some public Web Services.
Properties
URL
: The URL of the Web Service; example: http://localhost/Project.WS.Auth/AuthService.asmx.Method
: The method to call; example:Login
.Parameters
: The parameters needed for the call.
Methods
CallWebServiceReturnString
: Call the Web Service and return the response as a string.CallWebService
: Call the Web Service and return the response as an XML document.
' 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()
Points of Interest
The call to the Web Service uses HTTP POST for the request, so may be some methods cannot be invoked with this approach. The code has not been tested with complex types. To use SOAP, use the Runtime.Serialization.Formatters.SoapMessage
class; it works. I have used this with some Cold Fusion Web Services as well.