«^»
10.4. Accessing ATotal from a .NET program

We could use Microsoft's Visual Studio.NET to create a client of the ATotal Web Service [12, 25]. For example, we could use the wizard that creates a client that is a Windows Form Application. The form could have a textbox to obtain the increment, a button to submit the query, and a label that can be used to report back the latest value of the total. By default, the wizard would create a class called Form with private variables called textbox1, button1 and label1. The wizard also generates a skeleton for a method called button1_Click and we could supply the following body for this method. (This code is given in C#.) This method would be executed whenever there is a click of the button:

0292: private void button1_Click(object sender, System.EventArgs e)
0293: {
0294:    double tIncrement = double.Parse(textBox1.Text);
0295:    double tNewTotal = iATotalService.inc(tIncrement);
0296:    label1.Text = tNewTotal.ToString();
0297: }
This code assumes that iATotalService is declared as a private variable of the Form class:
private ATotalService iATotalService;
and that it is pointing to an object of the proxy class for the Web Service. This object can be created using the statement:
iATotalService = new ATotalService();
The client needs to store the cookie it receives from the first call of inc and pass it in the headers of the HTTP requests of subsequent calls. This can be done using the statements:

CookieContainer tCookieContainer = new CookieContainer();
iATotalService.CookieContainer = tCookieContainer;
where CookieContainer is a class declared in the System.Net namespace. The last three statements need to be executed once. So they can go in the constructor of the Form class.

The above code assumes the existence of a proxy class called ATotalService. Visual Studio.NET has a wizard (called Add Web Reference) that can be used to generate a proxy class. It needs you to supply the URL of a WSDL document, e.g.:
http://localhost:8080/axis/services/ATotal?WSDL
If you supply a URL (like this one) that includes a port number, you may find that Visual Studio.NET ignores the port number when generating the proxy class. If this is the case, you will need to edit the constructor of the proxy class to include the port number:

public ATotalService() 
{
   this.Url = "http://localhost:8080/axis/services/ATotal";
}