An app for windows phone

What I wanted to create was an app that parsed an html site. This because this specific site doesn’t has an official api, so I needed to work around this :mrgreen:

First of all, I needed to get back “into programming”, the last time I’ve programmed was like ages ago ^^ So, back into c#, jej :mrgreen:

After some googling I learned that silverlight in .net worked with an asynchronous call for internet-activities. This ofcourse because a cellular network connection is slow, and you don’t want to stall the GUI.

The method to do this is by using a class called “WebRequest”.
The method is started by using “BeginGetRequestStream“, which starts the fetch in another thread besides the GUI.
When the data is downloaded a callback function is called, so you need to create this as well…

This resulted in some code like the one below:

private void login(string username, string password) {
//the url where we want to go, here no postdata is specified, because the site only uses the postdata in the url
string url = "" + username + "&password=" + password + "&new_language=english&submit=login";

//the initation of a request to a webpage
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
//something for cookies between different httprequests, later more
//webrequest are handled syncronous, on completion the callback is called
IAsyncResult result = (IAsyncResult)request.BeginGetResponse(new AsyncCallback(GetStatusesCallBack), request);

//callback thread
private void GetStatusesCallBack(IAsyncResult result)
string returnValue;
//get the state of the request
var request = (HttpWebRequest)result.AsyncState;
//if completed, put respons in variable
var response = request.EndGetResponse(result);
using (var stream = response.GetResponseStream())
//a streamreader to put the html in a string
using (var reader = new StreamReader(stream))
returnValue = reader.ReadToEnd();
//a third function to update the gui :-)

For my application I needed a method to store some information (email and password). Appearantly each application is given 1MB of datastorage in a database on your phone. More info on this site:

//calling the default constructor
private IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;
//add a mail of a user to the windows phone database
appSettings.Add("email", "");
//to retrieve data from this db
tbResults.Text = (string)appSettings["email"];

I ran into an issue, because I used 2 different WebRequest because I need to fetch 2 different pages (1 to login, a second to get the information from). But when getting the 2nd page, the login was already gone. Probably a cookie issue. So I ran into this site which solved my problem.

//first we define a cookie container: a context where our cookies are stored:
private CookieContainer cookies = new CookieContainer();
//in the first request we recall this container
//and again in the second request we recall the same container

Then, my second application I wanted to download an image from a website. After some googling found the following method to work:

//just to easy, placing an uri as an image source
Uri uri = new Uri("http://locationofimage.jpg", UriKind.Absolute)
image1.Source = new BitmapImage(uri);

And to create a nice and simple overview I wanted to use the default windows “listbox”, but I didn’t know how to use it. Thankfully this website helped me out:

3 reacties

Geef een reactie

Vul je gegevens in of klik op een icoon om in te loggen. logo

Je reageert onder je account. Log uit / Bijwerken )


Je reageert onder je Twitter account. Log uit / Bijwerken )

Facebook foto

Je reageert onder je Facebook account. Log uit / Bijwerken )

Google+ photo

Je reageert onder je Google+ account. Log uit / Bijwerken )

Verbinden met %s

%d bloggers liken dit: