Closed hahyes closed 1 year ago
Sorry, I don't believe there's any bug here and this is likely a programming question that I don't know the answer to. I can confidently say Flurl doesn't simply receive bytes from the server and convert them to jibberish, and what exactly RestSharp is doing differently in the case of this web site I can't say. I would suggest asking on Stack Overflow where you'll reach a larger audience.
@tmenier Problem is, bytes are unrecognizable too. Returned bytes cannot be understand by any converter or encoding. This is weird behaviour which with I can't do anything as I simply call library to return me bytes. I even put these bytes into HxD, bytes are untranslable into proper text, there is no way Flurl.Http
returned me a proper data.
Can't call it "programming question" when I have no influence over it by simply calling .GetBytesAsync()
.
I think it's caused by the br
encoding in this line: Accept_Encoding = "gzip, deflate, br"
.
Can you remove it?
If you cannot remove it, then you'll need to decompress the response before using it: https://stackoverflow.com/questions/57038748/decompress-brotli-httpresponse-in-a-httpclient
I don't know if/why RestSharp is doing it automatically.
@tranb3r I removed it and it works now? It is... weird? Like, it is not that I got this header from nowhere. I literally copied it from browser making the request. What's more weird, Postman had the same header and it worked.
So, probably browser and Postman are dealing automatically with it - RestSharp most likely too.
Anyway thank you for answer!
@tmenier So it was a programming question after all. Well, couldn't predict browser/Postman/RestSharp are doing something uncommon in background. Still, little embarrasing. Sorry for wasting time, have a nice day!
No worries. Glad someone was able to help, community at its finest!
Here's my solution for gzip and deflate using AutomaticDecompression
string url = "https://my.example.com";
/// Create an instance of HttpClientHandler
using HttpClientHandler handler = new HttpClientHandler();
/// Set the AutomaticDecompression property to DecompressionMethods.GZip
handler.AutomaticDecompression = DecompressionMethods.GZip;
// handler.AutomaticDecompression = DecompressionMethods.Deflate;
// handler.AutomaticDecompression = DecompressionMethods.Brotli;
// handler.AutomaticDecompression = DecompressionMethods.All;
/// Create an instance of HttpClient with the handler
using HttpClient client = new HttpClient(handler);
/// Send a GET request to a URL that returns compressed content
using HttpResponseMessage response = await client.GetAsync(url);
/// Check if the response is successful
if (response.IsSuccessStatusCode)
{
// Read the response content as a string
// The content will be automatically decompressed by the handler
string content = await response.Content.ReadAsStringAsync();
Console.WriteLine(content);
}
else
{
/// Handle the error
_logger.LogError($"Error accessing the URL. Is the target server down? Status: {response.StatusCode}");
}
Holy I'm definitely necro bumping this, I was just trying to find the solution to this SAME EXACT problem I had with Flurl for the past few hours now... Thank GOD google brought me here, please keep this up because this was such a PITA... Just removing the "br" line fixed it. Bless you all.
Hi there,
I encountered problem with
GetStringAsync
called from the response.Code:
As you can see, code is rather simple. Result is coded in unknown encoding. I tried UTF8, UTF7, UTF32, UTF16, Unicode, ASCII... nothing works. I tried to detect enconding with
GetBytesAsync
and library calledUTF.Unknown
, yet it couldn't even detect encoding of string.I tried the same thing with
RestSharp
and I got HTML source in proper enconding without any problems..NET 7.0 and Flurl.Http 3.2.4 VS2022 / Windows 11