Azure – REST

Get a string

httpClient httpClient = new HttpClient();

httpClient.DefaultRequestHeaders.Accept.TRyParseAdd("application/json");

string responseText = await httpClient.GetStringAsync(new Uri("http://services.odata.og/Northwind/Northwind.svc/Suppliers"));

Tip: Resolve the correct Type

  • var Client = new Windows.Web.Http.HttpClient();

 Get Full Response

try
{
  var client = new HttpClient();
  var uri = new Uri("http://example.com/customers/1");
  var response = await client.GetAsync(uri);
  var statusCode = response.StatusCode;
  
  // EnsureSuccessStatusCode throws Exception if not HTTP 200
  response.EnsureSuccessStatusCode();
  
  var responseText = await response.Content.ReadAsStringAsync();
}
catch(Exception ex)
{
  //...
}

 Reading response Headers

try
{
  var client = new HttpClient();
  var uri = new Uri("http://example.com/customers/1");
  var response = await client.GetAsync(uri);

  foreeach(var header in response.Headers)
  {
    HeadersText.Text += header.Key + " " + header.Value + "\n";
  }
  
  ResultsText.Text = await response.Content.ReadAsStringAsync();
}
catch(Exception ex)
{
  //...
}

Writing Request Headers

var client = new HttpClient();
var headers = client.DefaultRequestHeaders;
headers.Referer = new Uri("http://contoso.com");
var ok = headers.UserAgent.TryParseAdd("testprogramm/1.0");

var response = await client.GetAsync(uri);

var length = response.Content.Headers.ContentLength.Value;
byte[] buffer = new byte[length]

Headers

  • Headers (read/write collection)
  • Accept (read/write collection)
  • AcceptEncoding (read/write collection)
  • AcceptLanguage (read/write collection)
  • Authorization (read/write)
  • CacheControl (read/write collection)
  • Connection (read/write collection)
  • Cookie (read/write collection)
  • Date (read/write)
  • Expect (read/write collection)
  • From (read/write)
  • Host (read/write)
  • IfModifiedSince (read/write)
  • IfUnmodifiedSince (read/write)
  • MaxForwards (read/write)
  • ProxyAuthorization (read/write)
  • Referer (read/write)
  • TransferEncoding (read/write collection)
  • UserAgent (read/write collection)

Sending a Request

try
{
  var client = new HttpClient()
  var request = new HttpRequestMessage(HttpMethod.Delete, uri);
  var response = await client.SendRequestAsync(request);
  Headers.Text = "Status" + response.StatusCode;
}
catch(Exception ex)
{}

HttpMethods

  • Delete
  • Get
  • Head
  • Options
  • Patch
  • Post (Also discrete Method)
  • Put (Also discrete Method)

New: HttpMethod(string) for your own methods.

Less Payload, use JSON REST Zip

HttpBaseProtocolFilter filter = new HttpBaseProtocolFilter();
filter.AutomaticDecompression = true;
HttpClient webClient = new HttpClient(filter);

 Setting Cookies

var uri = new Uri("http://example.com/customers/1");

try
{
  var baseFilter = new HttpBaseProtocolFilter();
  var cookieManager = baseFilter.CookieManager;
  var cookie = new HttpCookie("favoriteColor", ".example.com", "/);
    { Value = "purple" };
  cookieManager.SetCookie(cookie);
  var client = new HttpClient(baseFilter);
  await client.PostAsync(uri, new HttpStringContent("Pete"));
}
catch(Exception ex)
{}

Access Cookies

var baseFilter = new HttpBaseProtocolFilter();
var cookieManager = baseFilter.CookieManager;
var client = new HttpClient(baseFilter);
var cookie = cookieManager.GetCookies(uri);

foreach(var cookie in cookies)
{
  CookieList.Text += cookie.Name + " " + cookie.Value + "\n";
}

Using HttpBaseProtocolFilter for Compression and Credentials

HttpBaseProtocolFilter filter = new HttpBaseProtocolFilter();
filter.AutomaticDecompression = true;

PasswordCredential creds = new PasswordCredential("jump", username, password);
filter.ServerCredentials = creds;
filter.ProxyCredentials = creds;
HttpClient httpClient = new HttpClient(filter);

string response = await httpClient.GetStringAsync(uri),

Use Credential Locker to store Account Information

  • Windows.Security.Credentials

Web Account Manager for OAuth (SSO)