C
C#13mo 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
Angius13mo ago
async void is a no-no Use async Task And await the call, of course
rallez
rallez13mo 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
Angius13mo ago
$paste
MODiX
MODiX13mo 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
Angius13mo 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
rallez13mo 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
rallez13mo ago
at line 35 i used GetServerTime method
phaseshift
phaseshift13mo ago
but do you still have 'async void' somewhere?
rallez
rallez13mo ago
no voids with async only 2 tasks with async which are in sended code actually
phaseshift
phaseshift13mo ago
show what calls Tavern
rallez
rallez13mo 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
phaseshift13mo 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
rallez13mo ago
okay thanks im creating simple console game, is something wrong if all methods will be tasks?
phaseshift
phaseshift13mo ago
no, not if you want to be using async. It needs to start being async/Tasl from MAin
rallez
rallez13mo ago
so going further all method calls has to be awaited right?
phaseshift
phaseshift13mo ago
All async ones that aren't fire n forget
rallez
rallez13mo 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
HimmDawg13mo ago
Avoid .Result because it can lead to deadlocks.
rallez
rallez13mo ago
what it actually means?
HimmDawg
HimmDawg13mo 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
rallez13mo 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
HimmDawg13mo 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
More Posts