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

Did you find this page helpful?