C # GetResponseAsync (): how to use correctly
I am developing an application using Twitch API and I tried using asynchronous request.
I am testing earlier with a synchronous request, for example:
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 it's not asynchronous.
I tried with the GetResponseAsync()
following:
using (var response2 = await request.GetResponseAsync())
{
using (var reader = new StreamReader(response2.GetResponseStream()))
{
string data = await reader.ReadToEndAsync();
return data;
}
}
However, it didn't go through the first using
one and I got these exceptions:
Exception: "System.Net.WebException" in mscorlib.dll
Exception: "System.Exception" in LivestreamerTwitchViewer.exe
Exception levΓ©e: "System.Exception" in mscorlib.dll
So what did I do wrong?
How to use it correctly GetResponseAsync()
?
Thanks in advance for your help.
source to share
No one has answered this question yet
Check out similar questions: