Web Service in Smartphone 2002
Chung Webster
Microsoft UK, Developer Services
January 2003
Applies TO: Microsoft Smartphone 2002 Software Microsoft ASP.NET Microsoft MAppoint .NET
Summary: Learn how to create a SMARTPHONE client program for ASP.NET Web Services that provides geographic information. Note that the sample code is just to demonstrate how to use and is not guaranteed and supported.
Download the Microsoft Software Development Kit for Smartphone 2002.
Download The Smartphone 2002 and Web Services Source Code.
Contents
IntroductionWeb Services Basicssmartphone Web ServicesMapmapile & Mappoint .NET Web Services & MapPoint .NET Web Services & MapPoint .NET Web Service
Introduction
Introduction
Orange SPV - The first mobile phone that was officially launched, SMARTPHONE 2002, has begun to sell on the market, so it is time to start developing a Smartphone application. In this article, we will create a smartphone client to connect an ASP.NET Web Service that provides geographic information. This article assumes that the reader has the basics of Web Services, ASP.NET, and Win32 programming.
.NET Framework has a class library to help us connect (consume) Web Services, from the analysis WSDL document to automatically create a proxy class for programming calls. This makes the large part of Web Services' programming calls are very insignificant; however, Compact Framework cannot be used by Smartphone, so we need to look at other choices.
COM can use the SOAP kit, but it is also not only available for Smartphone; however, we can use it to help track our calls.
Without these technologies, we can still program a web service, we need to take a look at the format and transfer of the Web Service. Use two information standards, SOAP, and WSDL in .NET 1.0 Web Services.
Web service foundation
Without an API-based SOAP help us connect Web Services, we have to process SOAP messages directly, and their format is specified in the WSDL document. Note An advantage of using WSDL is that plan information is in type elements.
For SmartPhone, we must construct your SOAP message so that they can be accepted correctly by Web Service. These messages are sent to the server via HTTP POST, and our client waits for SOAP response. This process includes analysis, checking the SOAP error and returns the processed value. This seems to have a huge effort. In any case, it is very similar to the underlying API provided by the SOAP toolkit.
Manually constructing a SOAP message from pure WSDL is a complex task, such as displaying map.wsdl This requires the correct operation element to find the input message, and map the correct type. If the web service uses the ASP.NET, a simple method is to view the generated ASMX page, select WebMethod, and view examples of SOAP requests (see Figure 1).
Figure 1. Web service generated documentation from asp.net. If you can't access this, another way is to use a client that supports WSDL to call Web Service and analyze the SOAP package. The SOAP kit supports a Utility, Trace Utility (MSSOAPT3.EXE). This tool is often used frequently when debugging the Web Service.
Figure 2. Trace Utility, from Soap Toolkit 3.0
Smartphone Web Services
In the request above, we can see that constructing a SOAP message is easy. In this example, I created a class SOAPWRITER, providing the underlying function to write a SOAP message.
#include "soap.h"
SOAPWRITER * PSOAP = New soapwriter ();
PSoAP-> Startenvelop ();
PSoAP-> StartBody ();
PSoAP-> StartElement (l "getlatlong", l "http: // mapmobile");
PSoAP-> WriteElementstring (L "AddressLine", L "new bond street");
PSoAP-> WriteElementstring (l "city", l "bath");
PSoAP-> StartElement (l "postcode");
PSoAP-> endelement (l "postcode");
PSoap-> WriteElementstring (l "country", l "uk");
PSoAP-> endelement (l "getlatlong");
PSoAP-> endbody ();
PSoAP-> EndENVelope ();
PSoAP-> FinalizesoAP ();
In order to send this SOAP request to Web Service, I will use the Wininet API. In general, Wininet is used for HTTP and FTP communication. This is an advanced API that provides Internet access and does not require Winsock programming. Fortunately, the web service uses SOAP calls based on HTTP.
In order to easily send our SOAP request, I created a SOAPCONNECTOR class, allowing us to request, and get a response. Once connected to the web service, the client can perform multiple calls and use Accessors to accept the SOAP response.
#include "soap.h"
SOAPWRITER * PSOAP = New soapwriter ();
// Create The Soap Request Here
SOAPCONNECTOR * PCON = New soapConnector ();
PCON-> INIT ();
// Connect to the Server and Web Service
PCON-> Connect (l "http: // chungw02: 8080 / mapmobile / map.asmx");
// invoke with the soap message and soapaction
PCON-> Invoke (PSoAP, L "http://mapmobile / getlatlong");
// extract out the response
INT Ilen = 0;
PCON-> GetsoAplength (& Ilen); Tchar * PRESPONSE = New Tchar [Ilen 1];
PCON-> GetSoap (& president);
XML can be received by the client, then downloaded to the DOM analyzer and returns the process after processing. In this example, I created a class that encapsulates the IXmldomDocument interface in MSXML; COMs you need here will be introduced.
#include "soap.h"
// read soap response
PSoAPReader = new soapreader ();
PSoAPReader-> init ();
// load the soap response by passing in a soapconnector or xml string
PSoAPReader-> loadingXML (PCON);
Once loaded, the DOM can be accessed through the M_PDOM object to select the node value or run the XPath query.
MSXML :: ixmldomnode * pnode = null;
Msxml :: ixmldomnodelist * pnodelist = null;
MSXML :: ixmldomnode * ptextNode = null;
Tchar * lpnodevalue = NULL;
Tchar * xpath = new tchar [50];
_TCSCPY (XPATH, L "/ soap: envelope / soap: body / node");
Variant vNodeVal;
HRESULT HR;
// using previous created PSoAPReader
Try {
// select node
HR = PSoAPReader -> m_pdom-> selectsinglenode (xpath, & pnode);
IF (Failed (HR))
__leave;
IF (pnode == null)
__leave;
// Get Child Node
HR = pnode-> get_childnodes; cLDES;
IF (Failed (HR))
__leave;
// Get text node
HR = pnodelist-> get_item (0, & ptextNode);
IF (Failed (HR))
__leave;
// Get Value of TEXT NODE
Variantinit (& vNodeVal);
HR = ptextNode-> get_nodevalue (& vnodeval);
IF (Failed (HR)) {
VariantClear (& vNodeVal);
__leave;
}
// Assign the value to lpnodevalue
LPNODEVALUE = tchar [sysStringlen (vNodeVal.bstrval) 1];
_TCSCPY (LPNodeValue, vNodeVal.Bstrval);
VariantClear (& vNodeVal);
// Do Something with lpnodevalue
}
__finally {
IF (PNode! = NULL)
Pnode-> Release ();
IF (pnodelist! = null)
Pnodelist-> release ();
IF (PTEXTNode! = NULL)
PTEXTNODE-> Release ();
IF (lpnodevalue! = null) delete [] lpnodevalue;
}
As a selection, a help method, selectsingletextNode provides the parsed text node. Dispatched TCHAR length to fit the value of the text node.
// Note THESE ARE Allocated by The Helper Method SelectsingletExtNode
Tchar * latitude = NULL;
TCHAR * longitude = null;
// using previous created PSoAPReader
PSoAPReader-> SelectsingleTextNode (L "/ soap: envelope / soap: body / getlatlongResponse / latitude", & latitude);
PSoAPReader-> SelectsingletExtNode (L "/ soap: envelope / soap: body / getlatlongResponse / longitude", & longitude);
// Do Something with Return Values
//
// cleanup
IF (Latitude! = NULL)
delete [] latitude;
IF (longitude! = NULL)
DELETE [] longitude;
We call the Web Service to return the SOAP message containing data in the text node, so the SelectsingleTextNode method provides a useful mechanism to resolve data from the Web Service call. The SoapReader class can be extended to include other help methods, if you often need to parse different data, EG, such as attribute data or evaluate a NodeSet from different node axes.
To date, SOAPWRITER, SOAPCONNECTOR, and SOAPReader are used to provide functional functions constructed, call, and read requests from the web service. To use these classes to implement a client, we need to understand the format of the SOAP message. Below, we will see how to create a Web Service and create a smartphone client.
MapMobile & Mappoint .NET Web Service
This DEMO program receives map information from a web service and is displayed in Smartphone. Geographic Information Find and Map Information Provided by Mappoint.net - a business web service. Since the end users of Smartphone do not have their own mappoint .NET account, I created a package web service, mapMobile, used to call and identify mappoint .net. For SmartPhone 2002, there is no authentication provider, so if I want to talk directly to the mappoint .NET. Net. I have to manually implement the certification head. Additional reasons for encapsulation is to change the providers of geographic information without affecting the end user, such as transplaating mappoint NET 2.0 to 3.0.
Because there is already an article describing how to use Mappoint .NET and its SDK, I don't prepare for detailed details. More information, including the assessment account installation, please visit Mappoint .NET. View service's WSDL, visit http://staging.mappoint.net/standard-30/default.htm.
Figure 3. MapMobile Architecture
Our packaging service provides two simple Web Service Method to make the client can locate and display a map. The latitude and longitude of the return address will be called, and the second call returns a BYTE ARRAY containing GIF images. [WebMethod]
[SoapHeader ("_ phoneenumber", direction = soapheaderdirection.in)]
Public void getlatlong (String AddressLine, String City, String Postcode, String Country, Out Double Latitude, Out Double longitude)
[WebMethod]
[SoapHeader ("_ phoneenumber", direction = soapheaderdirection.in)]
Public Byte [] getMap (Double Latitude, Double Longitude, Double Zoom, Int Width, Int Height, String Tag)
To prevent unlimited call MapMobile Web Service, a SOAP header containing the SmartPhone phone number is included in all calls. At the server side, the head will check in the control valid number list. If the call fails, a SOAP error will be returned to the client. This verification mechanism is just an example, it is clear that it cannot disable the use of a valid number. There are many Web Service security options that depend on your web service, you can do not choose to authenticate, use digital signatures, or use new Global XML Web Services Architecture (GXA).
For more information about the future calls Security Web Service, please visit Security In A Web Services World: a proposed Architecture and RoadMap.
SMARTMAP Client
The client program provides an input screen for entering an address. Then it will call MapMobile Web Service and receive a map to save to the file system. This allows users to see the last download map.
Figure 4. SmartMap User Interface
Our client applications include a Splash Screen (Our Main Window Class) and three dialogs (input screen, web service call status, and map display). Initially, Splash Window is displayed, followed by the Find Location dialog. The phone number of the device is also extracted and saved, in order to be used in the SOAP header. This call does not work in the simulator, so if the call fails, we set a 1234567890 number as the test number.
Tchar * g_phonenumber = new tchar [40];
SMS_ADDRESS PADDR;
// Get the phone number
SMSGETPHONENUMBER (& PADDR);
_TCSCPY (g_phonenumber, paddr.ptsaddress);
When the user selects the Search menu option, the application constructs a correct SOAP call to the MapMobile Web Service. This includes a SOAP header containing the device phone number.
SOAPWRITER * PSOAP = New soapwriter ();
SW-> Startenvelope ();
// Construct the header
SW-> StartHeader ();
SW-> StartHeadeRelement (L "MapMobileHeader", L "http: // mapmobile"); sw-> writeheadelementstring (l "phoneenumber", g_phonenumber;
SW-> endheadelelement (L "MapMobileHeader");
SW-> endheader ();
// SOAP BODY
SW-> StartBody ();
// Construct the soap body here ..
SW-> endbody ();
SW-> EndENvelope ();
SW-> finalizesoap ();
Once these are completed, the SOAPConnector can be loaded into the SOAPReader to parse the base64 encoded data.
TCHAR * MAP64;
// this allocates memory Into the Target Variable
HRSEARCH = PSoAPReader-> SelectsingletExtNode (L "/ soap: envelope / soap: body / getmapresponse / getmapresult", & map64);
We will always check our HRESULTS, if the SOAPReader fails, then check if the response contains a SOAP error.