I'm having a problem trying to use C# and retrieving html from various websites. The code below should illustrate my issue. This is a .Net 6 console app. The issue is that some websites return corrupted responses. I've tried various encodings and things with no luck. Perhaps you can see something I'm doing wrong.
Thanks!
/* Start of application */
using System.Text;
HttpClient hclient = new HttpClient();
string endpoint = "http://yahoo.com";
Console.WriteLine(Test1().Result); // Returns corrupted binary data
Console.WriteLine(Test2().Result);// Returns corrupted binary data
Console.WriteLine(Test3().Result);// Returns corrupted binary data
endpoint = "http://poal.co";
Console.WriteLine(Test1().Result); // works as expected.
async Task<string> Test1()
{
HttpResponseMessage? resp = await hclient.GetAsync(endpoint);
return await resp.Content.ReadAsStringAsync();
}
async Task<string> Test2()
{
byte[] resp = await hclient.GetByteArrayAsync(endpoint);
return Encoding.UTF8.GetString(resp, 0, resp.Length - 1);
}
async Task<string> Test3()
{
return await hclient.GetStringAsync(endpoint);
}
/* End of application */
>I'm having a problem trying to use C# and retrieving html from various websites. The code below should illustrate my issue. This is a .Net 6 console app. The issue is that some websites return corrupted responses. I've tried various encodings and things with no luck. Perhaps you can see something I'm doing wrong.
>Thanks!
/* Start of application */
using System.Text;
HttpClient hclient = new HttpClient();
string endpoint = "http://yahoo.com";
Console.WriteLine(Test1().Result); // Returns corrupted binary data
Console.WriteLine(Test2().Result);// Returns corrupted binary data
Console.WriteLine(Test3().Result);// Returns corrupted binary data
endpoint = "http://poal.co";
Console.WriteLine(Test1().Result); // works as expected.
async Task<string> Test1()
{
HttpResponseMessage? resp = await hclient.GetAsync(endpoint);
return await resp.Content.ReadAsStringAsync();
}
async Task<string> Test2()
{
byte[] resp = await hclient.GetByteArrayAsync(endpoint);
return Encoding.UTF8.GetString(resp, 0, resp.Length - 1);
}
async Task<string> Test3()
{
return await hclient.GetStringAsync(endpoint);
}
/* End of application */
(post is archived)