C
C#9mo 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
Sound9mo 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
mtreit9mo ago
As in, it's a UI application and the UI thread freezes? How are you calling the method?
Sound
Sound9mo 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
mtreit9mo ago
.Result There's your problem
Angius
Angius9mo ago
Anything but await is blocking
mtreit
mtreit9mo ago
You need to make it async and use await, yeah.
mtreit
mtreit9mo ago
Asynchronous programming - C#
Learn about the C# language-level asynchronous programming model provided by .NET Core.
Sound
Sound9mo ago
everything is sync
Angius
Angius9mo ago
very sync much wow
Sound
Sound9mo ago
do i have to make everything async?
Angius
Angius9mo ago
Yes
Sound
Sound9mo ago
How can i convert getcontentasync to be sync ?
Angius
Angius9mo 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
mtreit9mo 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
Sound9mo ago
thanks