Encoding/Decoding Web Service: Web Service Provider
This article gives you a brief introduction to web services. The tasks the service accomplishes are described, as well as some of its uses (such as Base64 encoding strings). A step by step method of creating an encoding service and testing the service in the VS2003 IDE is also described.
In this tutorial, the web service provider will be described. The provider provides the service, namely when the web service consumer (Client) makes requests of the provider with certain information, the provider operates on that information using methods defined to process the information and returns the processed information. For example, the Microsoft Passport service provides authentication services when the consumer sends the username and password credentials. In a wider context, every machine would function both as a server and a client relaying information from one point to another, processing only what is intended for that particular machine.
Interoperability is at the heart of the web services. Hence, all this communication between computers takes place regardless of the programming language and the operating system of the computers involved. This was not possible in the earlier distributed applications. This is has become possible because of the XML based architecture with open standard protocols. The web service protocol stack is shown in the following picture. During this tutorial you will be seeing some of the protocols from this stack at work in the example.
We will not be looking at UDDI, but we will come across the other protocols. After Web Service made its debut and Microsoft integrated it within the .NET Frame work and the VS 2003 IDE, the company added many enhancements to make it work better and more securely, described in what are known as Web Services Enhancements. With this bird's eye view of web services, the functionality that is provided by the example in the tutorial will be described.
It is often necessary in the best interests of security to obfuscate data from prying eyes. Strong encryption of authentication information and data is one of the best ways. However, in some cases it is sufficient to make the data difficult to read. For example, the information in a connection string has several items whose security is highly desired. Keeping this information in clear text is obviously not desirable. In this case, the data can be transformed into something that is difficult to read. The System.Text.ASCIIEncoding.ASCII class provides methods that can be used to encode data. The service described in this tutorial carries out this transformation.
In the VS 2003 create a new project by choosing File --> New--> Project from the File Menu item. This opens up the New Project window and highlights the Web Services as shown below. The default web service is WebService1. Change this to something different; in the present tutorial it is Hencode.
The project explorer view with this choice is shown here. To the usual references you will see the System.Web.Services reference is also added. The Service1.asmx is the file that will contain the web methods that we are going to code.
Service1.asmx is the file that will contain the web methods. This file has a design view as well as a code view, Service1.asmx.vb. We will not be using the design view but our code will be inserted into the Service1.asmx.vb pane.
In VS 2003 the Service1.asmx will come with a sample method (although commented out), the well known HelloWorld() method. What we will be doing is creating two methods, one of them for encoding and the other for decoding functionality. Arguments have to be passed to these functions from the calling program (the Client). We will be looking at the client in part 2.
Let's look at the default, Public Function HelloWorld() web method. All this function does is display "HelloWorld" to the client. It is very important to use the correct syntax. If there are more methods you will be calling more functions as we shall see later.
'WEB SERVICE EXAMPLE ' The HelloWorld() example service returns the string Hello World. ' To build, uncomment the following lines then save and build the project. ' To test this web service, ensure that the .asmx file is the start page ' and press F5. ' '<WebMethod()> _ 'Public Function HelloWorld() As String ' Return "Hello World" 'End Function
In order to get the encoding functionality we will create an encoding function, enc() which takes a string as an argument and returns a string. For the reverse case we will be passing a string to a function called denc() that we create, which will provide a decoded string back. In each of these cases we will be using the methods of the System.Text.ASCIICoding.ASCII class for proper conversions and encoding/decoding. The two web methods are shown here:
<WebMethod()> _ Public Function enc (ByVal StrEncode As String) Dim encodedStr As String encodedStr = _ Convert.ToBase64String(System.Text.ASCIIEncoding. _ ASCII.GetBytes(StrEncode)) Return (encodedStr) End Function
<WebMethod()> _ Public Function denc (ByVal StrDecode As String) Dim decodedStrg As String decodedStrg = System.Text.ASCIIEncoding.ASCII. _ GetString(Convert.FromBase64String(StrDecode)) Return decodedStrg End Function
With this accomplished the Web Service provider is complete. The next section shows how you may test it within the VS 20043 IDE. However, for client access you may have to build a project so that they may access this service over the web. That will be described in part 2.
It is very easy to test whether the service is working. Right click the Service1.asmx file in the project explorer and pick up View in Browser. What will be displayed is shown in the next picture. There are three hyperlinks in this screen. The enc and denc are the web methods you created.
The Service Description will take you to the details of what the service is doing (the "under the hood" stuff). Let's take a quick look at that in the next screen by clicking on the hyperlink, which will take you to the following URL: http://localhost/Hencode/Service1.asmx?WSDL. This is the protocol that was described in the introduction. It describes the service provided, using the other protocols. The result is shown in a compressed format (most nodes collapsed), but it has the message transport that uses the SOAP protocol with all the required definitions of namespaces, schemas, messages, ports, and so on. The tutorial format is too limited to discuss all the details but you will find articles, books and volumes of materials on this subject on the Internet.
Now let's take a look at the testing, which is our primary, limited interest. Click on the hyperlink enc. The following screen will be displayed:
This screen shows the two protocols involved for the success of this service, the Soap Protocol -- a lightweight protocol -- and the HTTP request/response Protocol(which this screen shot has not captured completely because of length). To test the service, insert a string such as 'Hodentek' into the textbox with the title strEncode and click the Invoke button. This uses the HTTP protocol and you will see the following displayed in the browser. You may notice that Hodentek has been encoded as SG9kZW50ZWs=. Similarly if you now paste the above encoded value in the textbox for the other link, denc you will notice that the string 'Hodentek' shown in the picture after the next is displayed in the browser.
This is proof positive that the web service is working correctly.
Summary
After a very brief introduction to web services, the tasks the service accomplishes were described. This service can be used to Base64 encode strings. A step by step method of creating the service and testing the service in the VS2003 IDE was also described. In part 2, creating a web consumer will be described. As described this service is hosted on the local web server. While VS2003 IDE and .NET Framework were used, these are not essential for creating a web service. In fact, language neutrality is one of the main attributes of web services.