C
C#15mo ago
Sound

✅ HttpClient Get Page Content

I need a method RequestHelper.GetContent(string url) that returns the url's html as string. I also need a method that can get the page content as binary, so i can download files.
15 Replies
Sound
SoundOP15mo ago
public static async Task<string> GetContentAsync(string url)
{
try
{
HttpResponseMessage response = await httpClient.GetAsync(url);
response.EnsureSuccessStatusCode();
if(response.StatusCode != HttpStatusCode.OK)
{
throw new HttpRequestException($"Error while fetching content from {url}");
}

return response.Content.ToString();
}
catch (HttpRequestException ex)
{
throw new HttpRequestException($"Error while fetching content from {url}: {ex.Message}");
}
}
public static async Task<string> GetContentAsync(string url)
{
try
{
HttpResponseMessage response = await httpClient.GetAsync(url);
response.EnsureSuccessStatusCode();
if(response.StatusCode != HttpStatusCode.OK)
{
throw new HttpRequestException($"Error while fetching content from {url}");
}

return response.Content.ToString();
}
catch (HttpRequestException ex)
{
throw new HttpRequestException($"Error while fetching content from {url}: {ex.Message}");
}
}
this is my code so far. the problem is that the whole app freezes when i call GetContentAsync
mtreit
mtreit15mo ago
As in, it's a UI application and the UI thread freezes? How are you calling the method?
Sound
SoundOP15mo ago
public static string GetContent(string url)
{
try
{
return GetContentAsync(url).Result;
}
catch (AggregateException ex)
{
throw ex.InnerException ?? ex;
}
}
public static string GetContent(string url)
{
try
{
return GetContentAsync(url).Result;
}
catch (AggregateException ex)
{
throw ex.InnerException ?? ex;
}
}
private void UpdateConfiguration()
{
string contentProjects = RequestHelper.GetContent("https://api.papermc.io/v2/projects");
ProjectsRecord projects = new();
projects = JsonSerializer.Deserialize<ProjectsRecord>(contentProjects);
comboBoxProject.Items.Clear();
comboBoxProject.Items.AddRange(projects.projects.ToArray());
if (comboBoxProject.Text == "")
{
comboBoxProject.Text = comboBoxProject.Items[0].ToString();
}
}
private void UpdateConfiguration()
{
string contentProjects = RequestHelper.GetContent("https://api.papermc.io/v2/projects");
ProjectsRecord projects = new();
projects = JsonSerializer.Deserialize<ProjectsRecord>(contentProjects);
comboBoxProject.Items.Clear();
comboBoxProject.Items.AddRange(projects.projects.ToArray());
if (comboBoxProject.Text == "")
{
comboBoxProject.Text = comboBoxProject.Items[0].ToString();
}
}
private void MainWindow_Load(object sender, EventArgs e)
{
UpdateConfiguration();
}
private void MainWindow_Load(object sender, EventArgs e)
{
UpdateConfiguration();
}
mtreit
mtreit15mo ago
.Result There's your problem
Angius
Angius15mo ago
Anything but await is blocking
mtreit
mtreit15mo ago
You need to make it async and use await, yeah.
mtreit
mtreit15mo ago
Asynchronous programming - C#
Learn about the C# language-level asynchronous programming model provided by .NET Core.
Sound
SoundOP15mo ago
everything is sync
Angius
Angius15mo ago
very sync much wow
Sound
SoundOP15mo ago
do i have to make everything async?
Angius
Angius15mo ago
Yes
Sound
SoundOP15mo ago
How can i convert getcontentasync to be sync ?
Angius
Angius15mo ago
If it's async, you need to await it If you await in a method, it needs to be async You don't
mtreit
mtreit15mo ago
You should probably make things async. That said, if you want to make a synchronous call you can use the Send method: https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient.send?view=net-7.0
Sound
SoundOP15mo ago
thanks
Want results from more Discord servers?
Add your server