I'm developing an application using the Twitch API, and I tried to use asynchronous request.
I test before with a synchronous request, like this :
private async static Task<string> generalRequest(string url, string method, object payload = null, string accessToken = null, API api = API.V5, string clientId = null)
{
url = appendClientId(url, ClientId);
var request = WebRequest.CreateHttp(url);
request.Method = method;
request.ContentType = "application/json";
var response = request.GetResponse();
using (var reader = new StreamReader(response.GetResponseStream()))
{
string data = await reader.ReadToEndAsync();
return data;
}
}
It works great but this is not asynchronous.
I tried with GetResponseAsync() like this :
using (var response2 = await request.GetResponseAsync())
{
using (var reader = new StreamReader(response2.GetResponseStream()))
{
string data = await reader.ReadToEndAsync();
return data;
}
}
However, it never got through the first using, and I got these exceptions :
Exception levée : 'System.Net.WebException' in mscorlib.dll
Exception levée : 'System.Exception' in LivestreamerTwitchViewer.exe
Exception levée : 'System.Exception' in mscorlib.dll
So what I did wrong?
How do I use GetResponseAsync() correctly?
Thanks in advance for your help.