A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/MyToolkit/Core/wiki/Http below:

Http · RicoSuter/MyToolkit Wiki · GitHub

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

Features:

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);

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.

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
}

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);

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.

In Silverlight you have to create an HttpRequest object and set AutomaticDecompression to None - otherwise an exception is thrown.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4