Getting Started

Add the namespace reference

C#
using CI.HttpClient;

Make a GET request

Make a simple get request, the callback is raised once the download completes

C#
var client = new HttpClient(); // Send the GET request to the specified uri client.Get(new System.Uri("http://httpbin.org/get"), HttpCompletionOption.AllResponseContent, r => { // This callback is raised when the request completes if (r.IsSuccessStatusCode) { // Read the response content as a string string responseData = r.ReadAsString(); // Or if your api returns a json response you can easily deserialise it to a specific type MyObject myObject = r.ReadAsJson<MyObject>(); } });

Send a FormUrlEncoded message

C#
var client = new HttpClient(); var keyValuePairs = new Dictionary<string, string>() { { "Key1", "Value1" }, { "Key2", "Value2" }, }; var content = new FormUrlEncodedContent(keyValuePairs); client.Post(new Uri(url), content, HttpCompletionOption.AllResponseContent, r => { // Handle the response });

Send a JSON message

C#
var client = new HttpClient(); MyObject myObject = new MyObject(); // Automatically serialise your object to json var content = StringContent.FromObject(myObject); client.Post(new Uri(url), content, HttpCompletionOption.AllResponseContent, r => { // Handle the response });

Upload a file with a filename

Serialise your file to a byte array

C#
var client = new HttpClient(); var multipartFormDataContent = new MultipartFormDataContent(); multipartFormDataContent.Add(new ByteArrayContent(fileBytes, "application/octet-stream"), "MyFile", "MyFile.txt"); client.Post(new Uri(url), multipartFormDataContent, HttpCompletionOption.AllResponseContent, r => { // Handle the response }, u => { // Optional upload status callback });

Or open a stream on the file

C#
var client = new HttpClient(); var multipartFormDataContent = new MultipartFormDataContent(); var fileStream = new FileStream("MyFile.txt", FileMode.Open); multipartFormDataContent.Add(new StreamContent(fileStream, "application/octet-stream"), "MyFile", "MyFile.txt"); client.Post(new Uri(url), multipartFormDataContent, HttpCompletionOption.AllResponseContent, r => { // Handle the response }, u => { // Optional upload status callback });

Progress reporting

You can get the progress of a download by setting the HttpCompletionOption to StreamResponseContent. This downloads the resource in chunks which you need to handle

C#
var client = new HttpClient(); byte[] responseData = null; client.Get(new Uri(url), HttpCompletionOption.StreamResponseContent, r => { if (responseData == null) { // Create an array that can hold the full response responseData = new byte[r.ContentLength]; } // Get the percentage complete ProgressSlider.value = r.PercentageComplete; // Copy the data read this round to our array r.ReadAsByteArray().CopyTo(responseData, r.TotalContentRead - r.ContentReadThisRound); });

If you are uploading data in the request then you can also get progress reporting by attaching an upload handler

C#
client.Post(new Uri(url), null, HttpCompletionOption.StreamResponseContent, r => { }, u => { // Get the percentage complete ProgressSlider.value = u.PercentageComplete; });

Adding headers

Headers can either be added to all requests or to individual requests

C#
var client = new HttpClient(); // These headers are included with all requests from this instance client.Headers.Add("Accept", "application/json"); client.Headers.Add("MyCustomHeader", "SomeData");

You can also include headers at the request level when using Send

Compression

Assuming the server supports compression you can request either gzip or deflate content by setting the Accept-Encoding header

C#
var client = new HttpClient(); client.Headers.Add("Accept-Encoding", "gzip, deflate");

You can have HttpClient automatically decompress the response by setting the AutomaticDecompression property

C#
// Both are set here as an example but either can be specified on their own client.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;