Skip to main content

Articles

Featured Products

Windows Mobile Developer Controls
Windows Mobile Developer Controls

Twitter Updates

    News

    New site design will be posted by Wednesday.
    6/2/2008 8:07:00 AM

    Windows Mobile Developer Controls
    Windows Mobile Developer Controls
    Skip Navigation Links Breadcrumb Articles BreadcrumbCompact Framework

    Consuming a web service from your Pocket PC

    Written by Derek Mitchell  [author's bio]  [read 42639 times]
    Edited by Derek

    Download the code

    Page 1  Page 2 

    Now you should see com.devbuzz.www in your project list:

    Consuming the web service reference to the application

    Now we come to the fun stuff - consuming the web service from you Pocket PC. It's pretty interesting when you look at consuming web services with an eye on performance - consuming any complex data type is a) very very slow and b) means that you are restricting your potential consumers to only those that are using a Windows platform, so XML is really the only way to go from both a standards and performance perspective. The first time you call a web service from the Compact Framework there seems to be a real performance issue; second and subsequent calls are much faster (I'm not caching the web service output). I'm using an XScale iPAQ for this tutorial and I recorded this video clip (242 kb) as an example of the performance.

    As you can see after the first call the subsequent calls are very fast.

    The Code - Option 1

    Let's look at the code that executes when you hit the Go button. This is how we consume the web service using the proxy class generated by adding the web reference:

    'download the XML from the web service
    If txtEmailAddress.Text.Length < 1 Then
    btnGetArticles.Enabled = False
    UIHelper.statusBarMsg(sbar, "Please enter an email address.")
    Exit Sub
    End If
    'clear the textbox
    TextBox1.Text = ""
    Me.Refresh()
    UIHelper.ShowWaitCursor(True)
    'we want to time this
    Dim objTime As New TimeIt
    UIHelper.statusBarMsg(sbar, "Retrieving data from web service.")
    objTime.Start()
    Dim cnt As Integer
    Dim nodeList As XmlNodeList
    Dim xNode As XmlNode
    Dim rootNode As XmlNode
    Dim ws As New com.devbuzz.www.WebAPI
    rootNode = ws.GetAuthorArticles(txtEmailAddress.Text)
    objTime.Finish()

    Dim strServPlusWriteTime As String = objTime.Elapsed.ToString
    UIHelper.statusBarMsg(sbar, "Received data - loading XML.")

    If rootNode.HasChildNodes() Then
    nodeList = rootNode.ChildNodes
    Dim sb As New StringBuilder
    For cnt = 0 To nodeList.Count - 1
    xNode = rootNode.ChildNodes(cnt)
    sb.Append("Title: ")
    sb.Append(xNode.Attributes("Title").Value)
    sb.Append(ControlChars.CrLf)
    sb.Append(UIHelper.showShortDate(xNode.Attributes("Date").Value))
    sb.Append(ControlChars.CrLf)
    sb.Append("Read: ")
    sb.Append(xNode.Attributes("ReadCounter").Value)
    sb.Append(ControlChars.CrLf)
    sb.Append(xNode.Attributes("DeskTopURL").Value)
    sb.Append(ControlChars.CrLf)
    sb.Append(ControlChars.CrLf)
    Next
    TextBox1.Text = sb.ToString
    sb = Nothing
    End If

    UIHelper.statusBarMsg(sbar, "Web service retrieval: " & _
    strServPlusWriteTime)
    UIHelper.ShowWaitCursor(False)

    The Code - Option 2

    Using a Web Request to consume the web service requires the following code to execute when you hit the Go button. Note you do not need to add a Web Reference to use the web service in this fashion:

    objTime.Start()
    Dim strURL As String = baseWebServiceURI & _
    "/class/WebAPI.asmx/GetAuthorArticles?thisEmailAddress=" & _
    txtEmailAddress.Text
    Dim webReq As HttpWebRequest = WebRequest.Create(strURL)
    Dim webRes As HttpWebResponse = webReq.GetResponse
    Dim cnt As Integer
    Dim dom As New XmlDocument
    Dim nodeList As XmlNodeList
    Dim xNode As XmlNode
    dom.Load(webRes.GetResponseStream)
    webReq.Abort()
    webRes.Close()
    objTime.Finish()

    First we concatenate the web service URL and method name to form a URI:

    Dim strURL As String = baseWebServiceURI & _
    "/class/WebAPI.asmx/GetAuthorArticles?thisEmailAddress=" & _
    txtEmailAddress.Text

    Next we create a HttpWebRequest using the URI in strURL:

    Dim webReq As HttpWebRequest = WebRequest.Create(strURL)

    and then call the GetResponse method to return a HttpWebResponse instance containing the response from the web service.

    Dim webRes As HttpWebResponse = webReq.GetResponse

    If you'll recall this web service is returning an XMLDocument so we can instantiate an XML document and load the XML response stream using the XMLDocument.Load method.

    dom.Load(webRes.GetResponseStream)

    If you're wondering what the webReq.Abort is doing in there - join the club. We shouldn't need it but without it after a few calls the web service hangs (thanks to Neil Cowburn for discovering this when he provided the code to check for an internet connection).

    Parsing the XML

    Dim rootNode As XmlNode
    rootNode = dom.DocumentElement
    If rootNode.HasChildNodes() Then
    nodeList = rootNode.ChildNodes
    Dim sb As New StringBuilder
    For cnt = 0 To nodeList.Count - 1
    xNode = rootNode.ChildNodes(cnt)
    sb.Append("Title: ")
    sb.Append(xNode.Attributes("Title").Value)
    sb.Append(ControlChars.CrLf)
    sb.Append(UIHelper.showShortDate(xNode.Attributes("Date").Value))
    sb.Append(ControlChars.CrLf)
    sb.Append("Read: ")
    sb.Append(xNode.Attributes("ReadCounter").Value)
    sb.Append(ControlChars.CrLf)
    sb.Append(xNode.Attributes("DeskTopURL").Value)
    sb.Append(ControlChars.CrLf)
    sb.Append(ControlChars.CrLf)
    Next
    TextBox1.Text = sb.ToString
    sb = Nothing
    dom = Nothing
    End If

    Parsing the XML couldn't be any easier as you can see by the code above. So there you have it. You can download the code here - this includes both the proxy and web request based code outlined above - but remember that this was built using beta 2 - you should be able to adapt it to beta 1 barring any platform (read XScale) XML and/or web service issues.

    Previous Page