async private void ButtonRetrieveRemoteData_Click(object sender, RoutedEventArgs e)
{
- string currentDate = await view.getCurrentDate();
- if (currentDate != null)
+ try
{
- this.CurrentDateTextBox.Text = currentDate;
+ string currentDate = await view.getCurrentDate();
+ if (currentDate != null)
+ {
+ this.CurrentDateTextBox.Text = currentDate;
+ }
+ }
+ catch (Exception exception)
+ {
+ //TODO: logger for Windows Phone 8 :(
+ Console.WriteLine("ButtonGetDateClicked. Message: {0} Stacktrace: {1}", exception.Message, exception.StackTrace);
}
}
using Newtonsoft.Json;
+using Newtonsoft.Json.Serialization;
+using System;
using System.Net;
using System.Net.Http;
using System.Text;
postData.id = "2114567586433855105";
postData.jsonrpc = "2.0";
postData.method = method;
+ var jsonSettings = new JsonSerializerSettings
+ {
+ Error = delegate(object sender, ErrorEventArgs args)
+ {
+ //TODO: logger for Windows Phone 8 :(
+ Console.WriteLine(args.ErrorContext.Error.Message);
+ args.ErrorContext.Handled = true;
+ }
+ };
- string data = JsonConvert.SerializeObject(postData);
+ string data = JsonConvert.SerializeObject(postData, jsonSettings);
HttpContent content = new StringContent(data, System.Text.Encoding.UTF8, "application/json-rpc");
HttpResponseMessage response = await this.doCall(uri, content);
{
Task<byte[]> responseBytes = response.Content.ReadAsByteArrayAsync();
string responseString = Encoding.UTF8.GetString(responseBytes.Result, 0, responseBytes.Result.Length);
- POSTResult<TResult> postResult = JsonConvert.DeserializeObject<POSTResult<TResult>>(responseString);
+ POSTResult<TResult> postResult = JsonConvert.DeserializeObject<POSTResult<TResult>>(responseString, jsonSettings);
result = postResult.result;
}
}
+ /// <summary>
+ /// Send a POST request to the specified Uri as an asynchronous operation.
+ /// </summary>
+ /// <param name="uri">The Uri the request is sent to.</param>
+ /// <param name="content">The HTTP request content sent to the server.</param>
+ /// <exception cref="System.InvalidOperationException">When some error.</exception>
+ /// <returns>System.Threading.Tasks.Task<![CDATA[<TResult>]]>.The task object representing the asynchronous operation.</returns>
async private Task<HttpResponseMessage> doCall(string uri, HttpContent content)
{
- using (HttpClient client = new HttpClient())
+ using (HttpClient client = new HttpClient() { Timeout = TimeSpan.FromSeconds(5) })
{
return await client.PostAsync(uri, content);
}