WelcomeUser Guide
ToSPrivacyCanary
DonateBugsLicense

©2024 Poal.co

723

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)

[–] 2 pts

I ran this code in my VM and it started mining bitcoin for the FBI. WTF man!?!?!

[–] 0 pt

If you're curious about what happened, I finally discovered that Microsoft didn't originally support automatic decompression in the portable .Net 6 environment. They later added support for it, but you had to implement it first. Apparently, some websites serve compressed pages even when you don't ask for it.