Archiv der Kategorie: Azure

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)

 

 

Azure – SQL Offline Support

  1. Add NuGet Package WindowsAzure.MobileService.SQLiteStore
    Search for „WindowsAzure.MobileService“
  2. Change Reference from „SQLite for … (Windows 8.1)“ to „SQLite for Universal App Platform“
  3. Insert usings for
    • Microsoft.WindowsAzure.MobileServices.SQLiteStore
    • Microsoft.WindowsAzure.MobileServices.Sync
  4. Change the „GetTable“-Function to „GetSyncTable“-Function
  5. Add „await InitLocalStoreAsync()“-Function in „OnNavigatedTo“-Function
  6. Add „await SyncAsync()“-Function after „InsertAsync“-Function
  7. Add „await SyncAsync()“-Function after „UpdateAsync“-Function
  8. Add „await SyncAsync()“-Function after „DeleteAsync“-Function
  9. Add „await SyncAsync()“-Function before „Refresh“-Function
  10. Add following Code:
private async Task RefreshItemsAsync()
{
  MobileServiceInvalidOperationException exception = null;
  try
  {
    items = await todoTable.where(...).ToCollectionAsync();
  }
  catch(MobileServiceInvalidOperationException e)
  {
    exception = e;
  }

  if(exception !=  null)
  {
    await new MessageDialog(...).ShowAsync();
  }
  else
  {
    ListItems.ItemsSource = items;
    this.ButtonSave.IsEnabled = true;
  }
}

private async Task InitLocalStoreAsync()
{
  if(!App.MobileService.SyncContext.IsInitialized)
  {
    var store = new MobileServiceSQLiteStore("localstore.db");
    store.DefineTable<TodoItem>();
    await App.MobileService.SyncContext.InitializeAsync(store);
  }

  await SyncAsync();
}

private async Task SyncAsync()
{
  string errorString = null;

  try
  {
    await App.MobileService.SyncContext.PushAsync();
    await todoTable.PullAsync("todoItems", todoTable.CreateQuery());    
  }
  catch(MobileServicePushFailedException ex)
  {
    errorString = "Errors: " + ex.PushResult.Errors.Count + " " + ex.Message;
  }
  catch(Exception ex)
  {
    errorString = "Error: " + ex.Message;
  }

  if(errorString != null)
  {
    MessagebDialog d = new MessageDialog(errorString);
    await d.ShowAsync();
  }
}

 

Azure – Mobile Service

Insert Item to Azure – Table

private async System.Threading.Tasks.Task InsertTodoItem()
{
  IMobileServiceTable<TodoItem> todoTable = App.TaskMasterDemoClient.GetTable<TodoItem>();
  
  TodoItem t = new TodoItem();
  t.Title = titleTextBox.Text;
  t.Description = descriptionTextBox.Text;
  t.DueDAte = dueDatePicker.Date.ToString();
  t.AssignedTo = assignedToTextBox.Text;

  try
  {
    await todoTable.InsertAsync(t);
  }
  catch(Exception)
  {
    // Handle exception
  }
}