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

Did you find this page helpful?