C
C#2y ago
rallez

✅ Basic http client get request

Hi why my code won't return any value
static async Task<int> GetServerTime()
{
using (HttpClient client = new HttpClient())
{
try
{
HttpResponseMessage response = await client.GetAsync("http://XXX/");
response.EnsureSuccessStatusCode();

string result = await response.Content.ReadAsStringAsync();
int serverTime = int.Parse(result);
return serverTime;
}
catch (HttpRequestException ex)
{
Console.WriteLine("Błąd: " + ex.Message);
throw;
}
}
}

async void Test()
{
Console.WriteLine(await GetServerTime());
}

Test();
static async Task<int> GetServerTime()
{
using (HttpClient client = new HttpClient())
{
try
{
HttpResponseMessage response = await client.GetAsync("http://XXX/");
response.EnsureSuccessStatusCode();

string result = await response.Content.ReadAsStringAsync();
int serverTime = int.Parse(result);
return serverTime;
}
catch (HttpRequestException ex)
{
Console.WriteLine("Błąd: " + ex.Message);
throw;
}
}
}

async void Test()
{
Console.WriteLine(await GetServerTime());
}

Test();
When i dont use Test method instead just using
Console.WriteLine(await GetServerTime());
Console.WriteLine(await GetServerTime());
it works normally
22 Replies
Angius
Angius2y ago
async void is a no-no Use async Task And await the call, of course
rallez
rallezOP2y ago
okay that's work for me in this simple code, but now when im trying using so the GetServerTime method stays the same how do i send more code than discord allows without nitro? sohuld i use pastebin or something like that?
Angius
Angius2y ago
$paste
MODiX
MODiX2y ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
Angius
Angius2y ago
The rule of thumb, though, is that 1. An async method needs to return a Task or Task<T> 2. An async method has to be awaited 3. A method that awaits something has to be async
rallez
rallezOP2y ago
so my code looks like that https://paste.mod.gg/kfiopaarqyuu/
BlazeBin - kfiopaarqyuu
A tool for sharing your source code with the world!
rallez
rallezOP2y ago
at line 35 i used GetServerTime method
phaseshift
phaseshift2y ago
but do you still have 'async void' somewhere?
rallez
rallezOP2y ago
no voids with async only 2 tasks with async which are in sended code actually
phaseshift
phaseshift2y ago
show what calls Tavern
rallez
rallezOP2y ago
its other function which is void type should i change all methods to Tasks? Tavern is called by other method which is also called by other method ...
phaseshift
phaseshift2y ago
if it's void return type, how can you do await Tavern !? yes, it needs to be Task/async 'all the way down'
rallez
rallezOP2y ago
okay thanks im creating simple console game, is something wrong if all methods will be tasks?
phaseshift
phaseshift2y ago
no, not if you want to be using async. It needs to start being async/Tasl from MAin
rallez
rallezOP2y ago
so going further all method calls has to be awaited right?
phaseshift
phaseshift2y ago
All async ones that aren't fire n forget
rallez
rallezOP2y ago
can i do that this way instead?
int GetServerTime()
{
using var client = new HttpClient();
try
{
const string url = "http://XXX/";
var response = client.GetAsync(url).Result;
response.EnsureSuccessStatusCode();

var result = response.Content.ReadAsStringAsync().Result;
var serverTime = int.Parse(result);
return serverTime;
}
catch (HttpRequestException ex)
{
Console.WriteLine("Error: " + ex.Message);
throw;
}
}

void Test()
{
var time = GetServerTime();
Console.WriteLine(time);
}
Test();
int GetServerTime()
{
using var client = new HttpClient();
try
{
const string url = "http://XXX/";
var response = client.GetAsync(url).Result;
response.EnsureSuccessStatusCode();

var result = response.Content.ReadAsStringAsync().Result;
var serverTime = int.Parse(result);
return serverTime;
}
catch (HttpRequestException ex)
{
Console.WriteLine("Error: " + ex.Message);
throw;
}
}

void Test()
{
var time = GetServerTime();
Console.WriteLine(time);
}
Test();
HimmDawg
HimmDawg2y ago
Avoid .Result because it can lead to deadlocks.
rallez
rallezOP2y ago
what it actually means?
HimmDawg
HimmDawg2y ago
A deadlock is a situation, where two threads wait for each other, but both cannot continue execution until they got their answer from the other thread
rallez
rallezOP2y ago
my app doesnt use multiple threads actually i just need to get time from server im very begginer and just creating smple console game should i worry about it right now?
HimmDawg
HimmDawg2y ago
Yes. I know it sounds tempting to just do it like that if it works, but as Mr. MultipleZ said, there is a rule of thumb you should follow
2. An async method has to be awaited
Want results from more Discord servers?
Add your server