Http class
HTTP GET
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"
Http.Get(request, OnRequestFinished);
The event handler method:
private void OnRequestFinished(HttpResponse response)
{
if (response.Successful)
{
// TODO: process response outside UI thread
Deployment.Current.Dispatcher.BeginInvoke(() => {
var text = response.Response;
// TODO: set your data to view model or control
// (this code is called in the UI thread)
});
}
else
{
if (!response.Canceled)
{
// display exception
MessageBox.Show(response.Exception.Message);
}
}
}
HTTP POST
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
Http.Post(request, OnRequestFinished);
ExceptionsAn exception is only thrown if there is a network or http problem. If HttpStatusCode != 200 the Exception property is set to a HttpStatusException instance or HttpStatusException is thrown in async operations.
Handle wrong HTTP status codes
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);
}
catch (OperationCanceledException e)
{
// TODO add your cancellation logic
}
catch (Exception e)
{
// TODO add your exception handling logic
}
Http.Post("url", result =>
{
if (result.Successful)
{
Debug.WriteLine(result.Response);
}
else if (!result.Canceled)
{
if (result.Exception is HttpStatusException)
{
var e = (HttpStatusException)result.Exception;
// your status != 200 handler
Debug.WriteLine(e.Result.HttpStatusCode);
Debug.WriteLine(e.Result.Response);
}
else
Debug.WriteLine(result.Exception.Message); // url not found, etc...
}
});
Windows Phone
To prevent from popping up of error messages outside the page, call
Http.AbortAllRequests(); in the method
OnNavigatedFrom. This will abort all HTTP requests.
To prevent from popping up of error messages outside the page, call
Http.AbortAll(this); (this is the current page) in the method
OnNavigatedFrom. This will abort all HTTP request made on the current page.
WinRT / WinPRT (WP8)
Now with await/async support:
try
{
var response = await Http.GetAsync("http://www.domain.com");
}
catch (OperationCanceledException e)
{
// TODO add your cancellation logic
}
catch (Exception e)
{
// TODO add your exception handling logic
}
Classes
If the
Http class is not built with
USE_GZIP debug symbols only the Http* classes (see list below) are needed. The idea is to have a simpler standalone use of the http classes: If the classes are compiled outside the MyToolkit library the GZIP classes are no longer needed (found in "/Libraries" directory) and GZIP is no longer supported.