I made a function like this:
public void GetData(string dataToPost)
{
var url = "some URL";
using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "application/json";
client.UploadStringCompleted += (s, e) =>
{
Console.WriteLine("Result is here");
Console.WriteLine(e.Result);
};
client.UploadStringAsync(new System.Uri(url), "POST", dataToPost);
}
}
The problem here is the fact that I want to get a value returned from the server (HTTP response).
I want my function to be asynchronous, so for example I'd like to be able to call it 5 times in a loop, without waiting for each call to return something before next call.
I have no idea how to achieve it. I tried to create some Task object to await it, but Task requires a delegate and I don't know what delegate i could give it.
Te function above uses an event, which is fired when the result comes - so I need to return that result.
How can I do that?