Using windows phone web client is pretty common but the thing is you can not use it using Async -> Await mechanism so i used threading to create an async functionality for The Download string and upload string methods here is the code below
// Comment
public class AsyncWebClient
{
public Task DownloadString(Uri uri)
{
var task = new TaskCompletionSource();
try
{
var client = new WebClient();
client.DownloadStringCompleted += (s, e) =>
{
task.SetResult(e.Result);
};
client.DownloadStringAsync(uri);
}
catch (Exception ex)
{
task.SetException(ex);
}
return task.Task;
}
public Task UploadString(Uri uri, string content)
{
var task = new TaskCompletionSource();
try
{
var client = new WebClient();
client.UploadStringCompleted += (s, e) =>
{
task.SetResult(e.Result);
};
client.UploadStringAsync(uri,content);
}
catch (Exception ex)
{
task.SetException(ex);
}
return task.Task;
}
}
Comments
Post a Comment