HTTP classes

Package: MyToolkit.Http
Platforms: All (PCL)

The HTTP classes provide a simple API with additional HTTP featues.

Features:
  • GZIP/deflate supported for ALL platforms (also WP8) which is enabled by default
  • File upload (multipart/form-data)
  • Custom connection timeout if needed
  • IProgress support to access the download progress (for uploading not implemented yet)
  • Support for cancellation tokens
  • Portable class library for WP, SL, WinRT, WPF

HTTP GET

Simple HTTP GET call:

try
{
	var response = await Http.GetAsync("http://www.server.com");
	var html = response.Resonse;
}
catch (OperationCanceledException e)
{
	// TODO add your cancellation logic
}
catch (Exception e)
{
	// TODO add your exception handling logic
}

Request with more parameters:

try
{
	var request = new HttpGetRequest("http://www.server.com");
	request.RequestGZIP = false; // default is true
	request.Query.Add("name", "value"); 
	request.Credentials = new NetworkCredential("username", "password");
	
	// the url is now: "http://www.server.com?name=value"
	var response = await Http.GetAsync(request);
	var html = response.Resonse;
}
catch (OperationCanceledException e)
{
	// TODO add your cancellation logic
}
catch (Exception e)
{
	// TODO add your exception handling logic
}

HTTP POST

HTTP POST call with POST data and attached files:

var request = new HttpPostRequest("http://www.server.com");
request.Data.Add("name", "value"); // POST data
request.Files.Add(new HttpPostFile("name", "file.jpg", "path/to/file.jpg")); // POST files
var response = await Http.PostAsync(request);

Exceptions

An exception is only thrown if there is a network or HTTP problem. If HttpStatusCode != 200, the Exception property returns a HttpStatusException object or - when using async methods - the HttpStatusException exception is thrown.

In most scenarios the OperationCanceledException exception should be catched but thrown away (no reaction) - see the example below.

Handle HTTP status codes

The following code shows how to handle HTTP status codes which are != 200 (not OK).

try
{
    var result = await Http.PostAsync("url");
    Debug.WriteLine(result.Response);
}
catch (HttpStatusException e)
{
    // your status != 200 handler
    Debug.WriteLine(e.Result.HttpStatusCode);
    Debug.WriteLine(e.Result.Response); // the response can still be read
}
catch (OperationCanceledException e)
{
    // TODO add your cancellation logic
}
catch (Exception e)
{
    // TODO add your exception handling logic
}

Progress handling

The following code shows how to display the progress of a HTTP call using a progress control.

var prog = new Progress<HttpProgress>();
prog.ProgressChanged += (o, p) =>
{
	progress.Minimum = 0; 
	progress.Value = p.ReadBytes;
	progress.Maximum = p.TotalBytes;
};

var request = new HttpGetRequest(
	new Uri("http://server.com/largefile.zip", UriKind.Absolute));

var response = await Http.GetAsync(request, CancellationToken.None, prog);

Abort runnning requests

To prevent showing of error messages for example after navigating from a page, call Http.AbortAllRequests(); in the method OnNavigatedFrom. This will abort all currently running HTTP requests.

Last edited Mar 23 at 11:23 PM by rsuter, version 68

Comments

No comments yet.