Hello good, I have this code that is to check if the streamer is online or not, but it always returns false, can you help me please.
static HttpClientHandler hcHandle = new HttpClientHandler();
static async Task<bool> IsOnline(string channel)
{
using (var hc = new HttpClient(hcHandle, false))
// false here prevents disposing the handler, which should live for the duration of the program and be shared by all requests that use the same handler properties
{
hc.DefaultRequestHeaders.Add("Client-ID", "dagsugbfi4u9ytbq6cn2kj681qvtzu");
//hc.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", "your oauth token, should you have one (you should, but not required)");
//hc.DefaultRequestHeaders.UserAgent.ParseAdd("this would be good practice to set to your own");
hc.Timeout = TimeSpan.FromSeconds(15); // good idea to set to something reasonable
using (var response = await hc.GetAsync($"https://api.twitch.tv/helix/streams?user_login={channel}"))
{
response.EnsureSuccessStatusCode(); // throws, if fails, can check response.StatusCode yourself if you prefer
string jsonString = await response.Content.ReadAsStringAsync();
// TODO: parse json and return true, if the returned array contains the stream
}
}
return false;
}
public async void button2_Click(object sender, EventArgs e)
{
bool is_valid = await IsOnline("thead");
if(is_valid)
{
this.Alert("thead is online", FormCoscu.enmType.coscu);
}
else
{
MessageBox.Show("stream is offline");
}
}
Is this the actual code you’re using? Because it’s incomplete and even has a commented TODO section that the actual logic of checking if the stream exists or not still has to be written, so if you haven’t finished writing the code then of course it would return false.
If it’s not the actual code you’re using, could you please show us what the actual request you’re making is.
For my thing, I dont believe a webhook is necessary. Although I will post the finished code for anyone who would like it. (Lua)
local CheckIfOnline = function()
local Data = {
Url = “https://api.twitch.tv/helix/streams?user_login=12gaugenick”,
Method = “GET”,
Headers = {
[“Client-ID”] = “p68bczp3bnsopaf6378auzjk4nu8wl"”
},
}
local Request = Http:RequestAsync(Data)
if Request.StatusCode == 200 then
local TwitchData = Http:JSONDecode(Request.Body).data[1]
if TwitchData.type == “live” then
return {
Viewers = TwitchData.viewer_count,
Title = TwitchData.title,
IsLive = TwitchData.type
}
else
return false
end
else
warn(“API Error”)
end
return false
end