3 4 5 6 7 8 9 WebClient client; 10 // Constructor public MainPage() { InitializeComponent(); client...

16
7 Session 5.1 Connecting to a Data Service

description

3

Transcript of 3 4 5 6 7 8 9 WebClient client; 10 // Constructor public MainPage() { InitializeComponent(); client...

Page 1: 3 4 5 6 7 8 9 WebClient client; 10 // Constructor public MainPage() { InitializeComponent(); client = new WebClient(); client.DownloadStringCompleted.

7Session 5.1

Connecting to a Data Service

Page 2: 3 4 5 6 7 8 9 WebClient client; 10 // Constructor public MainPage() { InitializeComponent(); client = new WebClient(); client.DownloadStringCompleted.

Windows Phone

Topics Windows Phone applications on a

network The role of the Windows Phone

emulator Creating network connections Using asynchronous network

requests

Page 3: 3 4 5 6 7 8 9 WebClient client; 10 // Constructor public MainPage() { InitializeComponent(); client = new WebClient(); client.DownloadStringCompleted.

Windows Phone3

Windows Phone on a network The Windows Phone device is very well connected WIFI 3G GPRS

All these networks are accessed by programs that use the TCP/IP networking protocol

Your program does not have to worry about the underlying communication technology

Page 4: 3 4 5 6 7 8 9 WebClient client; 10 // Constructor public MainPage() { InitializeComponent(); client = new WebClient(); client.DownloadStringCompleted.

Windows Phone4

Phone limitations A mobile phone does not have a

working connection at all times Mobile applications that use the

network must either “fail gracefully” or provide offline modes for their users

Any network request must provide progress information and use timeouts to ensure that a failed network doesn’t cause them to get stuck

Page 5: 3 4 5 6 7 8 9 WebClient client; 10 // Constructor public MainPage() { InitializeComponent(); client = new WebClient(); client.DownloadStringCompleted.

Windows Phone5

Windows Phone networking Windows Phone Silverlight

applications and XNA games can use the network But they are not allowed to make

“socket” based connections in Version 1.0 of the operating system

All network requests must be calls to services that will deliver a result asynchronously

The network calls are exactly the same as those provided in the desktop .NET libraries

Page 6: 3 4 5 6 7 8 9 WebClient client; 10 // Constructor public MainPage() { InitializeComponent(); client = new WebClient(); client.DownloadStringCompleted.

Windows Phone6

Asynchronous connections A program will never make a call to

the network and then wait for the result This is called synchronous operation,

and is not supported by any network calls on the phone

Instead a program will make a call to the network to initiate an operation

The network object will then call back later with the result of the operation

Page 7: 3 4 5 6 7 8 9 WebClient client; 10 // Constructor public MainPage() { InitializeComponent(); client = new WebClient(); client.DownloadStringCompleted.

Windows Phone7

Asynchronous operation This form of event based operation is

exactly the same as the model used by Silverlight to accept user input

Silverlight elements can generate program events Button pressed, Text Changed etc

A program can bind to these events so that a method is called when an event occurs

Page 8: 3 4 5 6 7 8 9 WebClient client; 10 // Constructor public MainPage() { InitializeComponent(); client = new WebClient(); client.DownloadStringCompleted.

Windows Phone8

Downloading Web Pages To get started we could

look at an application to “scrape” text off a web page

This can be used to obtain data for display

We can also use it to read structured data from the web, of which more later

Page 9: 3 4 5 6 7 8 9 WebClient client; 10 // Constructor public MainPage() { InitializeComponent(); client = new WebClient(); client.DownloadStringCompleted.

Windows Phone9

The WebClient class

To start we need something that will do the work for us

The .NET WebClient class can be used to fetch information from the internet An instance of the WebClient class

represents a single web request We can use it to read HTML from a

given web page

WebClient client;

Page 10: 3 4 5 6 7 8 9 WebClient client; 10 // Constructor public MainPage() { InitializeComponent(); client = new WebClient(); client.DownloadStringCompleted.

Windows Phone10

Creating a WebClient

We can create a WebClient instance in the constructor for our page

// Constructorpublic MainPage(){ InitializeComponent();

client = new WebClient(); client.DownloadStringCompleted += new DownloadStringCompletedEventHandler( client_DownloadStringCompleted);}

Page 11: 3 4 5 6 7 8 9 WebClient client; 10 // Constructor public MainPage() { InitializeComponent(); client = new WebClient(); client.DownloadStringCompleted.

Windows Phone11

Binding to the loaded event

This statement binds a method to the completed event

The method runs after the page is fetched

// Constructorpublic MainPage(){ InitializeComponent();

client = new WebClient(); client.DownloadStringCompleted += new DownloadStringCompletedEventHandler( client_DownloadStringCompleted);}

Page 12: 3 4 5 6 7 8 9 WebClient client; 10 // Constructor public MainPage() { InitializeComponent(); client = new WebClient(); client.DownloadStringCompleted.

Windows Phone12

Displaying the result

The client_DownloadStringCompleted method runs when the page text has arrived

It checks for an error and displays the text if there was none

void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e){ if (e.Error == null) { pageTextBlock.Text = e.Result; }}

Page 13: 3 4 5 6 7 8 9 WebClient client; 10 // Constructor public MainPage() { InitializeComponent(); client = new WebClient(); client.DownloadStringCompleted.

Windows Phone13

Sending the request

This is the event handler for the button that is pressed to start the transaction

It calls the DownloadStringAsync method on the WebClient to start the download

It is given a Uri to download from

private void loadButton_Click(object sender, RoutedEventArgs e){ client.DownloadStringAsync(new Uri(urlTextBox.Text));}

Page 14: 3 4 5 6 7 8 9 WebClient client; 10 // Constructor public MainPage() { InitializeComponent(); client = new WebClient(); client.DownloadStringCompleted.

Windows Phone14

The Uri

A Uri instance gives the address of a resource

It can be created from a string of text In this program the Uri is created

from a TextBox on the user interface

private void loadButton_Click(object sender, RoutedEventArgs e){ client.DownloadStringAsync(new Uri(urlTextBox.Text));}

Page 15: 3 4 5 6 7 8 9 WebClient client; 10 // Constructor public MainPage() { InitializeComponent(); client = new WebClient(); client.DownloadStringCompleted.

Windows Phone15

Demo 1: Web Scraping

Demo

Page 16: 3 4 5 6 7 8 9 WebClient client; 10 // Constructor public MainPage() { InitializeComponent(); client = new WebClient(); client.DownloadStringCompleted.

Windows Phone16

Review Windows phones can make

connections to services over the network

All network requests are asynchronous The result of a network request is

delivered by an event generated in the client

The WebClient class provides a very easy way of loading the content of urls