|
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Features Invoking .NET Web Services from Mobile Devices Part 1 of 2
Pocket PCs put .NET Web services at your fingertips
Invoking .NET Web Services from Mobile Devices Part 1 of 2
Pocket PCs put .NET Web services at your fingertips
By: Derek Ferguson
Jan. 1, 2000 12:00 AM
In May 2000 I was invited to Microsoft's corporate headquarters in Redmond for a special "technology experts" summit. At this summit, the forty or so of us in attendance were given a special sneak preview of a technology upon which Microsoft planned to "bet the farm," so to speak. They called the technology "ASP+ Web Methods." Of course, nowadays we all know these as .NET Web services. The fundamental technologies upon which they are based (XML, SOAP, SDL, UDDI, DISCO, etc.) are probably familiar to just about all of this publication's readership in one form or another. What many Web service developers are not aware of, however, is how good .NET is at "playing nice" with all sorts of different mobile devices. The Mobile Internet Toolkit, for example, adds out-of-the-box support for WAP and i-Mode clients to .NET's already formidable Web application infrastructure. Similarly, the Smart Device Extensions for .NET and the .NET Compact Frameworks bring the power of the .NET Common Language Runtime (CLR) environment to a wide range of wireless Internet devices, such as the Pocket PC. The focus of this magazine, however, is on Web services. For this reason, in this article I'll show you how .NET Web services may be accessed and utilized from many different devices. The code excerpts in this article have all been taken from my book, Mobile .NET (Apress, 2001). If you find this article interesting, I recommend that you purchase a copy!
Creating the .NET Web Service So, for our example mobile Web service, we'll develop a Web service that takes a single, four-letter stock symbol as its sole parameter and returns a single string to indicate the current price of this stock. To fuel the Web service with up-to-the-minute data, we'll make use of Lycos' excellent Finance Web site. To create the sample Web service, follow these steps:
Now that you've entered all of the code for your Web service, it makes sense to pause for a moment to review exactly what it all means. The first two "imports" lines in Listing 1 tell .NET to include support for Web services within our Visual Basic application. Specifically, the first line tells .NET to include general support for Web services. The second line tells .NET to include support for Web service protocols other than .NET's native Web services protocol. This second line is very important. Looking at the industry as a whole, simple SOAP is still the most common protocol used for invoking Web services. Many of the toolkits out there for building Web services clients do not yet support Microsoft's full Web services protocol. For this reason, it's easier to make .NET "speak their language" than vice versa! After these first two lines, we have a couple of lines that ask .NET to include support for low-level networking (e.g., TCP/IP socket communications) and regular expression parsing. Regular expressions are a new addition to Visual Basic with .NET, and will be greatly welcomed by those with previous experience in other languages such as Perl. We make our class into a Web service under .NET by inheriting it from System.Web. Services.WebService. The tag preceding our class declaration is .NET nomenclature for declaring a namespace for our Web service. A similar tag is prepended to our getQuote method's declaration to indicate that this method should, indeed, be exposed to the Internet as a Web service. However, within this tag there is also the indication "SoapRpc Method()." This is how we leverage .NET's support for industry-standard Web services protocols to expose our method as a standard remote SOAP method. Our getQuote method takes a single parameter, the symbol of the stock for which we are interested, in receiving a quote. The first thing our method does is use .NET's regular expressions parsing capabilities to verify that the symbol consists of exactly four letters. If this isn't the case, an exception is thrown and the Web service call terminates. Assuming that the symbol is properly-formed, however, the next thing that our method does is to pass this symbol along to Lycos' Quote.com Web site to get a price quote. This is accomplished by using .NET's TCP/IP capabilities to send a simple Web request across the Internet. The contents of the page returned are stored as a (very long) string is the strResponse variable. At this point, we once again leverage .NET's regular expressions to parse the Web page for the first occurrence of the phrase "Last Sale" that is followed by numbers, a decimal, and then more numbers. This is the general area of the page where our most recent stock price has been returned. From within this section, the numerical portion is finally parsed out. This is the value that is returned by our method as the results of any Web service invocation.
Creating the Pocket PC Client
The package will come to you as a standard Windows installer. Simply walk through the installation wizard and make sure that you choose to install eMbedded Visual Basic and (if you don't have a "real" Pocket PC) the Pocket PC Software Development Kit (SDK). Simon Fell's Pocket SOAP software is currently the most popular tool for invoking SOAP-style Web services from Pocket PCs. To obtain this and install it onto your device or emulator, follow these steps:
Putting the Pocket PC install onto an actual Pocket PC device is a fairly easy process. First, make sure that your PC is connected to your computer and that ActiveSync is functioning properly. Then, run the Pocket PC install. It will walk you through an installation wizard, then ActiveSync will finish the installation of the software onto your device. Installing the x86 binaries onto the emulator is a little more complicated. You must:
The first several lines of code in Listing 2 are concerned with creating two of the most important objects in Pocket SOAP: the Envelope and the Transport. A Pocket SOAP Envelope is where the body of the SOAP message is assembled. The Transport is responsible for making sure that the message created by the Envelope gets from the device to the Web service, and for receiving the Web service's response. Several kinds of Transport are possible within the framework of SOAP: SMTP, FTP, etc. For our purposes, we've chosen to use the HTTP Transport, which will convey our messages using standard Web traffic. The next three lines of Listing 2 set various properties for our SOAP request. Specifically, the URI must be set equal to our Web service's namespace. The MethodName should be the name of the method within our Web service that we wish to invoke. Finally, you should call CreateParameter with the name and value of every parameter that you must pass in. The next thing we do is call the Serialize method on our Envelope object to translate our message into a simple string. We store this string in a variable and then call MsgBox to display it for your inspection. This is strictly for demonstration and debugging purposes. In a true, production Mobile .NET application, this step would typically not be taken. We call the Send method on our HTTP Transport object and pass in the URL of our Web service and our Envelope as parameters. This is how our SOAP request travels from the Pocket PC to the remote Web service. We then call the Receive method on our HTTPTransport object - this call blocks until our Web service responds. The response is returned as a string which we store in a variable named strResponse. By passing this string into our Envelope object's parse method, we're able to translate the text message that our Web service sent back into a real SOAP response. The code concludes by extracting the price of our stock out of this response and displaying it in a message box for the user.
In Conclusion Recently, however, Microsoft publicly released a brand new technology that promises to make calling .NET Web services from Pocket PCs even easier. This technology is known as the .NET Compact Framework and will be discussed at length in the next installment of this two-part series. In the meantime, if you have any questions or comments, please contact me via my Web site at www.MobileDotNet.com . Reader Feedback: Page 1 of 1
Latest Cloud Developer Stories
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week
Breaking Cloud Computing News
|
|||||||||||||||||||||||||||||||||||||||||||||||||