oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

.NET Framework Essentials, 2nd Ed.: Web Services, Part 3
Pages: 1, 2, 3, 4

HTTP POST Consumer

In the section "HTTP GET Consumer," we saw the automatic creation of a Web Services consumer by merely hitting the URL of the Web Services, http:// localhost/PubsWS/PubsWS.asmx. It is now time for us to see how a web client can use HTTP POST and SOAP to access a Web Service. This time around, we are going write a C# Web Service consumer.

The Microsoft .NET SDK has a rich set of tools to simplify the process of creating or consuming Web Services. We are going to use one of these tools, wsdl, to generate source code for the proxies to the actual Web Services:

wsdl /l:CS /protocol:HttpPost http://localhost/PubsWS/PubsWS.asmx?WSDL

This command line creates a proxy for the PubsWS Web Service from the WSDL (Web Services Description Language) document from the URL http://localhost/PubsWS/PubsWS.asmx?WSDL. The proxy uses HTTP POST as its protocol to talk to the Web Service; it is generated as a C# source file. The wsdl tool can also take a WSDL file as its input instead of a URL pointing to the location where the WSDL can be obtained.

Related Reading

.NET Framework Essentials
By Thuan L. Thai, Hoang Lam

This C# proxy source file represents the proxy class for the PubsWS Web Service that the clients can compile against. This generated C# file contains a proxy class PubsWS that derives from HttpPostClientProtocol class. If you use the /protocol:HttpGet or /protocol:SOAP parameters, the PubsWS derives from either the HttpGetClientProtocol or SoapHttpClientProtocol class.

After generating the C# source file PubsWS.cs, we have two choices for how this proxy can be used. One way is to include this source file in the client application project using Visual Studio.NET. The project has to be a C# project if you choose this route. To make use of the proxy, you also have to add to your project any references that the proxy depends on. In this example, the necessary references for the proxy file are System.Web.Services, System.Web.Services.Protocols, System.Xml.Serialization, and System.Data.

The other way to use the proxy is more flexible. You can compile the C# source file into a dynamic link library (DLL) and then add a reference to this DLL to any project you want to create. This way you can even have a VB project use the DLL.

Below is the command line used to compile the C# proxy source into a DLL. Notice that the three references are linked to PubsWS.cs so that the resulting PubsWS.DLL is self-contained (type the entire command on one line):

    csc /t:library

Regardless of how you choose to use the proxy, the client application code will still look the same. Consider the next two code examples containing C# and VB code. For both languages, the first lines create an instance of the proxy to the Web Service, PubsWS. The second lines invoke the GetAuthors web method to get a DataSet as the result. The remaining lines bind the default view of the table Authors to the data grid, add the data grid to a form, and display the form. Note that these examples use the Windows Forms API, which we'll discuss in Chapter 8. Here is the C# Web Service client, TestProxy.cs:

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
public class TestProxy
  public static void Main(  )
    /* Create a proxy. */
    PubsWS oProxy = new PubsWS(  );
    /* Invoke GetBooks(  ) over HTTPPOST and get the data set. */
    DataSet oDS = oProxy.GetAuthors(  ); 
    /* Create a data grid and connect it to the data set. */
    DataGrid dg = new DataGrid(  );
    dg.Size = new Size(490, 270);
    dg.DataSource = oDS.Tables["Authors"].DefaultView;
    /* Set the properties of the form and add the data grid. */
    Form myForm = new Form(  );
    myForm.Text = "DataGrid Sample";
    myForm.Size = new Size(500, 300);
    /* Display the form. */

If you created the DLL as previously directed, you can compile this with the following command:

csc TestProxy.cs /r:PubsWS.dll

This creates the executable TestProxy.exe, which gets a DataSet using a HTTP POST call, and displays a data grid containing that dataset. Figure 6-4 shows the output of the C# client after obtaining the data from the PubsWS Web Service via HTTP POST protocol.

Figure 6-4. C# Web Service client after calling GetAuthors( )


Here is the VB Web Service client, TestProxyVB.vb:

imports System
imports System.Drawing
imports System.Windows.Forms
imports System.Data
Module TestProxyVB
  Sub Main(  )
    ' Create a proxy.
    dim oProxy as PubsWS = new PubsWS(  )
    ' Invoice GetBooks(  ) over SOAP and get the data set.
    dim oDS as DataSet = oProxy.GetAuthors(  )
    ' Create a data grid and connect it to the data set.
    dim dg as DataGrid = new DataGrid(  )
    dg.Size = new Size(490, 270)
    dg.DataSource = oDS.Tables("Authors").DefaultView
    ' Set the properties of the form and add the data grid.
    dim myForm as Form = new Form(  )
    myForm.Text = "DataGrid Sample"
    myForm.Size = new Size(500, 300)
    ' Display the form.
  End Sub
End Module

You can compile the VB Web Service client with this command (type the entire command on one line):

vbc TestProxyVB.vb 

Instead of using wsdl to generate the proxy and include the proxy in your application, you can also rely on VS.NET to automate the whole process. In VS.NET, you can just add a Web Reference to your application. The process of adding a Web Reference to an application involves the discovery of the Web Service, obtaining the WSDL, generating the proxy, and including the proxy into the application.

Pages: 1, 2, 3, 4

Next Pagearrow