C
C#5mo ago
zorous

✅ getting error using http client

private async Task getGameStatus()
{
using (HttpClient client = new HttpClient())
{
try
{
HttpResponseMessage response = await client.GetAsync("https://game-website/activePlayers");
if (response.IsSuccessStatusCode)
{
string responseContent = await response.Content.ReadAsStringAsync();
DateTime currentTime = DateTime.Now;
string dayOfWeek = currentTime.ToString("dddd", CultureInfo.InvariantCulture);
int hour = currentTime.Hour;

if (dayOfWeek == "Friday" && hour >= 17)
{
GameStatus = "Updating";
}
else
{
GameStatus = "Online";
}
}
else
{
GameStatus = "Offline";
}
}
catch
{
GameStatus = "Offline";
}
}
}
private async Task getGameStatus()
{
using (HttpClient client = new HttpClient())
{
try
{
HttpResponseMessage response = await client.GetAsync("https://game-website/activePlayers");
if (response.IsSuccessStatusCode)
{
string responseContent = await response.Content.ReadAsStringAsync();
DateTime currentTime = DateTime.Now;
string dayOfWeek = currentTime.ToString("dddd", CultureInfo.InvariantCulture);
int hour = currentTime.Hour;

if (dayOfWeek == "Friday" && hour >= 17)
{
GameStatus = "Updating";
}
else
{
GameStatus = "Online";
}
}
else
{
GameStatus = "Offline";
}
}
catch
{
GameStatus = "Offline";
}
}
}
with this im getting this error:
Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.dll
Error: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. (timeapi.io:443)
The program '[34060] Blade Guns.exe' has exited with code 4294967295 (0xffffffff).
Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.dll
Error: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. (timeapi.io:443)
The program '[34060] Blade Guns.exe' has exited with code 4294967295 (0xffffffff).
46 Replies
Jimmacle
Jimmacle5mo ago
seems pretty clear whatever site you're making a request to isn't responding
zorous
zorousOP5mo ago
the site im fetching from just returns a integer and nothing else. no format or anything
Buddy
Buddy5mo ago
Try to specify a useragent
Angius
Angius5mo ago
Best read the docs of that API, see what it needs
zorous
zorousOP5mo ago
lol no docs
Angius
Angius5mo ago
Also, is https://game-website/activePlayers the actual URL you have there?
zorous
zorousOP5mo ago
is it possible for them to block my connection and therefore be getting this response type? https://helmet-heroes-api.com/TotalOnline
Angius
Angius5mo ago
No description
Angius
Angius5mo ago
Seems it can't even be pinged
Buddy
Buddy5mo ago
Is that your game?
zorous
zorousOP5mo ago
not mine, im just a player
Buddy
Buddy5mo ago
That site is crazy bad lol. It thinks I run in 4K when my monitor is just in 4K lol, I play in windowed 1/4th of the screen :kekw:
Angius
Angius5mo ago
Curling works
No description
zorous
zorousOP5mo ago
anyway to put that into code?
Angius
Angius5mo ago
No description
Angius
Angius5mo ago
HttpClient seems to work fine
Buddy
Buddy5mo ago
Same for me
Buddy
Buddy5mo ago
No description
zorous
zorousOP5mo ago
will prob use "getfromjsonasync"
Angius
Angius5mo ago
Yeah, it'll make things easier for sure
zorous
zorousOP5mo ago
i've been using "ReadAsStringAsync"
Buddy
Buddy5mo ago
If the format is JSON, sure. Definitely easier.
Angius
Angius5mo ago
A plain number is valid JSON, so yeah
zorous
zorousOP5mo ago
alr im still getting this:
Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.dll
Error: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. (timeapi.io:443)
The program '[24028] HHR Essentials.exe' has exited with code 0 (0x0).
Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.dll
Error: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. (timeapi.io:443)
The program '[24028] HHR Essentials.exe' has exited with code 0 (0x0).
Angius
Angius5mo ago
Your new code?
zorous
zorousOP5mo ago
private async Task getGameStatus()
{
using (HttpClient client = new HttpClient())
{
try
{
string responseContent = await client.GetStringAsync("https://helmet-heroes-api.com/TotalOnline");
DateTime currentTime = DateTime.Now;
string dayOfWeek = currentTime.ToString("dddd", CultureInfo.InvariantCulture);
int hour = currentTime.Hour;

if (dayOfWeek == "Friday" && hour >= 17)
{
GameStatus = "Updating";
}
else
{
GameStatus = "Online";
}
}
catch
{
GameStatus = "Offline";
}
}
}
private async Task getGameStatus()
{
using (HttpClient client = new HttpClient())
{
try
{
string responseContent = await client.GetStringAsync("https://helmet-heroes-api.com/TotalOnline");
DateTime currentTime = DateTime.Now;
string dayOfWeek = currentTime.ToString("dddd", CultureInfo.InvariantCulture);
int hour = currentTime.Hour;

if (dayOfWeek == "Friday" && hour >= 17)
{
GameStatus = "Updating";
}
else
{
GameStatus = "Online";
}
}
catch
{
GameStatus = "Offline";
}
}
}
Angius
Angius5mo ago
Idk what to say, it works perfectly for me
Angius
Angius5mo ago
No description
zorous
zorousOP5mo ago
i have another call made for http client
Angius
Angius5mo ago
Does that one work?
zorous
zorousOP5mo ago
i tried putting debug statements before but they didnt even show up instead i got the error i just relaized the error is going on the api "timeapi.io" and not the TotalOnline is it possible for it to be blocking it?
Angius
Angius5mo ago
Ah, damn, true lol
No description
zorous
zorousOP5mo ago
know any libs that has a clock that shows time for UTC and PH?
Angius
Angius5mo ago
NodaTime should be able to easily convert time between timezones https://nodatime.org/
zorous
zorousOP5mo ago
NodaTime 3.1.11
Noda Time is a date and time API acting as an alternative to the built-in DateTime/DateTimeOffset etc types in .NET.
Angius
Angius5mo ago
It's even in the sample code on their website lmao
No description
Angius
Angius5mo ago
Yeah
zorous
zorousOP5mo ago
alr let me rewrite my other .cs file and see if it still causes an issue
zorous
zorousOP5mo ago
now its returning this:
The thread '[Thread Destroyed]' (25524) has exited with code 0 (0x0).
The thread '[Thread Destroyed]' (33000) has exited with code 0 (0x0).
The thread '[Thread Destroyed]' (25524) has exited with code 0 (0x0).
The thread '[Thread Destroyed]' (33000) has exited with code 0 (0x0).
still trying to figure out why the text isnt showing up
No description
zorous
zorousOP5mo ago
should be like this
No description
Angius
Angius5mo ago
Ah, it's, like, WPF or something?
zorous
zorousOP5mo ago
yes
Angius
Angius5mo ago
Async code with UI frameworks can be... iffy
zorous
zorousOP5mo ago
any suggestions?
Angius
Angius5mo ago
Chances are you have an async void or something somewhere that's causing this issue 'Fraid not, I don't have much experience with making UI apps
zorous
zorousOP5mo ago
rip i solved the issue i forgot to bind it in the file.xaml.cs
Want results from more Discord servers?
Add your server