In this chapter, I will use the free Microsoft .NET framework SDK to make SOAP client using the command-line compilers. You can also use the free and wonderfull IDE #DEVELOP to edit and compile your code (work also for VB .NET projects!). I will begin with example code in C# .NET, followed by VB .NET. These examples will consume my JWSDP web service (if not on-line, use it as a template for your own use), and the Infobel .NET phone directory web service (up almost 24/7).
The first thing to do (if not already done) is to install the Microsoft .NET framework SDK (Software Development Kit) from MSDN. The SDK is free and allow you to compile, using the command-line, in C# .NET, C++ .NET, VB .NET and even in J# (I won't cover the use of the J# language to make a SOAP client in my doc). If you have a compliant OS and a fast internet connection, you should be able to install all the stuff easily. Note if you already have the Microsoft Visual Studio .NET installed on your machine, you do not need to install the SDK, Visual Studio .NET includes the SDK.
We will need to use the wsdl tool to generate the stub to consume web services. To run this tool, add it to your PATH environment variable (make a search on wsdl.exe to locate it). Do the same for the bin\ directory of your .NET SDK install, so it will be easy to run the compilers: csc.exe for C# and vbc.exe for VB .NET.
We first have to run the wsdl tool to generate our proxy. Run the tool as showed below for my on-line JWSDP web service. You can also replace the url pointing to the WSDL of my web service by one pointing to another valid WSDL document.
[your_prompt]:\> wsdl /l:cs /protocol:SOAP http://www.pascalbotte.be/rcx-ws/rcx?WSDL
The tool will generate, for the case of my web service, the MyRcxService class (in the MyRcxService.cs C# file). We will need this file to compile our client.
Now create our client C# file and copy/paste the code listed below:
Example 6-1. Simple C# SOAP client
In this example, We will call three simple remote procedure:
readLS(): this method take a string as param and return a string. This is an "echo string" demo.
readLSpercent(): this one take an int type as param and return an int. This method is supposed to return a real-time value of the amount of light (by the use of a light sensor) in percent (if my web service is up).
readTemp(): and this one take an int as param and return a float, which is the temperature (°C) in my office (again, if my web service is up).
using System;
namespace soapclient
{
class MainClass
{
public static void Main(string[] args)
{
Console.WriteLine("My first C# .NET command-line SOAP client!");
// replace eventually the class name by the one
// generated by wsdl.exe if you ran the tool
// for another web service
MyRcxService mrs = new MyRcxService();
// Consume the readLS() remote procedure
// use the wrapper,
// this is a document/literal web service
// see [1] for rpc/encoded
readLS rls = new readLS();
rls.String_1 = "your message or e-mail address";
readLSResponse rlsresp = mrs.readLS(rls);
Console.WriteLine("response: {0}", rlsresp.result);
// readLSpercent()
readLSpercent rlsp = new readLSpercent();
rlsp.int_1 = 1;
readLSpercentResponse rlspr = mrs.readLSpercent(rlsp);
Console.WriteLine("Light sensor: {0}", rlspr.result);
// temperature
readTemp rt = new readTemp();
rt.int_1 = 1;
readTempResponse rtr = mrs.readTemp(rt);
Console.WriteLine("Temperature: {0}", rtr.result);
// use eventually a Console.ReadLine() if
// you want to execute from an IDE,
// so you can see the results
//Console.ReadLine();
}
}
}
Now it is time to compile, run the command below to compile a PE (portable executable) as a mono-file assembly:
[your_prompt]:\> csc client.cs myrcxservice.cs
You should be able to run your exe on any computer having the .NET runtime installed. In the following sections we will create soap clients handling complex types.
Handling complex type with C# .NET is easy. You will find example code to consume procedure returning an object, an array of simple type and an array of object.
Add the code below to consume the following remote procedure:
status(): this procedure return an object containing the following elements:
battery level in volt
internal clock (ms)
free memory
total amount of memory
a message from the RCX
collInt(): return a simple array of type int
collPos(): return an array of object PosCol. This object contain two int value: XPos and YPos
status st = new status();
statusResponse stresp = mrs.status(st);
rcxResponse result = stresp.result;
Console.WriteLine("rcxResponse object:");
Console.WriteLine("\tBattery: {0}\n" +
"\tInternal clock: {1}\n" +
"\tFree memory: {2}\n" +
"\tTotal memory: {3}\n" +
"\tMessage: {4}\n",result.battery,
result.currentTime,
result.memory,
result.memoryTot,
result.status);
collInt ci = new collInt();
int[] ciresp = mrs.collInt(ci);
Console.WriteLine("Array of int");
for(int x=0; x < ciresp.Length; x++)
Console.WriteLine("\telement: {0} value: {1}",
x, ciresp[x]);
collPos cp = new collPos();
PosCol[] cpar = mrs.collPos(cp);
Console.WriteLine("Array of PosCol");
for(int x=0; x < cpar.Length; x++)
Console.WriteLine("PosCol element {0}: XPos -> {1}, YPos -> {2}",
x, cpar[x].XPos, cpar[x].YPos);
Consuming a more advanced WSDL interface with C# .NET stay a relatively simple task. See my example below, as usually with C# you can use the wsdl tool to generate your proxy.
![]() | Important note |
|---|---|
The data returned by this web service are obsolete, are from a few years ago, and can only be used for demo and testing purpose. |
Run the following command:
[your_prompt]:\> wsdl /l:cs /protocol:SOAP http://hal.kapitol.com/infobelservices/service1.asmx?WSDL
This will create the file InfobelSearchEngines.cs. See below how to use this class.
To compile this application, run the following command:
[your_prompt]:\> csc client.cs infobelsearchengine.cs
As usually, the command-line compiler will create a portable executable file. So you should be able to run it on any computer with, at least, the .NET runtime installed.
| [1] | No need of the wrapper classes to consume an rpc/encoded web service. You just have to call using // the following line will return a string
mrs.readLS("your message or e-mail"); |
![]() | ![]() | ![]() |
| Consume a .NET web service with gSOAP | ![]() My home page RSS | Make a SOAP client in VB .NET using the command-line |