using System.Net;
using System.Net.Http;
using System.Text;
+using System.Threading;
using System.Threading.Tasks;
namespace RemoteAgents.WindowsPhone.Model
public class CallRemoteProcedure
{
- async public Task<TResult> callRemoteService<TResult>(string uri, string method)
+ async public Task<TResult> CallPostRemoteServiceAsync<TResult>(string uri, string method)
+ {
+ POSTResult<TResult> postResult = null;
+
+ postResult = await this.PostAsync<TResult>(uri, method, CancellationToken.None);
+
+ return postResult.result;
+ }
+
+ async private Task<POSTResult<TResult>> PostAsync<TResult>(string uri, string method, CancellationToken cancellation)
{
var postData = new POST();
postData.id = "2114567586433855105";
};
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);
+ // see: http://stackoverflow.com/questions/1329739/nested-using-statements-in-c-sharp
+ // see: http://stackoverflow.com/questions/5895879/when-do-we-need-to-call-dispose-in-dot-net-c
+ //TODO: Am I really sure I have to call the Dispose method of HttpContent content. In this case shouldn't it be stupid?
+ // for HttpResponseMessage response I am sure I have to do it but I am not for HttpContent content.
+ using (HttpContent content = new StringContent(data, System.Text.Encoding.UTF8, "application/json-rpc"))
+ using (HttpResponseMessage response = await this.PostAsync(uri, content, cancellation))
+ {
+ POSTResult<TResult> postResult = null;
- TResult result = default(TResult);
+ if (response.StatusCode == HttpStatusCode.OK)
+ {
+ byte[] responseBytes = await response.Content.ReadAsByteArrayAsync();
+ string responseString = Encoding.UTF8.GetString(responseBytes, 0, responseBytes.Length);
+ postResult = JsonConvert.DeserializeObject<POSTResult<TResult>>(responseString, jsonSettings);
+ }
- if (response.StatusCode == HttpStatusCode.OK)
- {
- 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, jsonSettings);
- result = postResult.result;
+ return postResult;
}
-
- return result;
}
/// </summary>
/// <param name="uri">The Uri the request is sent to.</param>
/// <param name="content">The HTTP request content sent to the server.</param>
+ /// <param name="System.Threading.CancellationToken">Cancellation token.</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)
+ async private Task<HttpResponseMessage> PostAsync(string uri, HttpContent content, CancellationToken cancellation)
{
using (HttpClient client = new HttpClient() { Timeout = TimeSpan.FromSeconds(5) })
{
- return await client.PostAsync(uri, content);
+ return await client.PostAsync(uri, content, cancellation);
}
}