简体   繁体   English

使用 C# HttpClient 下载的 Skype Emoticon 与使用浏览器下载的字节 stream 不同

[英]Skype Emoticon downloaded with C# HttpClient is a different byte stream than downloaded with browser

This is the most bizarre thing.这是最离奇的事情。

I'm trying to download this file using the C# HttpClient: https://statics.teams.microsoft.com/evergreen-assets/skype/v2/smile/50.png我正在尝试使用 C# HttpClient 下载此文件: https://statics.teams.microsoft.com/evergreen-assets/skype/v2/smile/50.png

It happens to be one of the Microsoft Teams emoticon image files.它恰好是 Microsoft Teams 表情图像文件之一。

笑脸.png

(Incidentally, I had to download this image locally to my machine with Chrome, then upload it from there, as the StackOverflow image upload couldn't handle the URL either...) (顺便说一句,我必须使用 Chrome 将此图像本地下载到我的机器,然后从那里上传,因为 StackOverflow 图像上传也无法处理 URL ......)

I've tried a number of different cracks at the code to download this file - the most straightforward and usual way of doing so, stripped down to essentials, is:我已经尝试了许多不同的破解代码来下载这个文件 - 最直接和最常用的方法是:

var client = new HttpClient();
var webStream = await client.GetStreamAsync("https://statics.teams.microsoft.com/evergreen-assets/skype/v2/smile/50.png");
using (var fileStream = new FileStream("smilely.png", FileMode.OpenOrCreate)) {
    webStream.CopyTo(fileStream);
}

I've tried several different ways using HttpClient, using the System.Net.WebClient, and raw WebRequests as well, with the same results.我尝试了几种使用 HttpClient、System.Net.WebClient 和原始 WebRequest 的不同方法,结果相同。

If I put the URL into my browser and go to it, I see the image as expected, but if I am downloading the file from C#, I get a corrupted image that won't open.如果我将 URL 放入我的浏览器并将 go 放入其中,我会按预期看到图像,但如果我从 C# 下载文件,我会得到一个无法打开的损坏图像。

The proper file, downloaded using the browser is 2127 bytes, starting with the proper PNG header bytes, like so:使用浏览器下载的正确文件为 2127 字节,从正确的 PNG header 字节开始,如下所示:

89 50 4E 47 0D 0A 1A 0A

The file that I download programmatically is screwed up, with an entirely different byte stream, which is only 2093 bytes, and starts:我以编程方式下载的文件被搞砸了,一个完全不同的字节 stream,只有 2093 个字节,然后开始:

1F 8B 08 00 00 00 00 00

I don't have this problem downloading other emoticon images from the same set, like https://statics.teams.microsoft.com/evergreen-assets/skype/v2/laugh/50.png我从同一组下载其他表情图像没有这个问题,比如https://statics.teams.microsoft.com/evergreen-assets/skype/v2/laugh/50.png

在此处输入图像描述

What in the world could possibly be going on with this?这到底是怎么回事?

1F 8B is the magic number for GZIP. 1F 8B是 GZIP 的神奇数字

If we look at the response content headers with:如果我们查看响应内容标头:

var client = new HttpClient();
var response = await client.GetAsync("https://statics.teams.microsoft.com/evergreen-assets/skype/v2/smile/50.png");
var contentHeaders = response.Content.Headers;

We can see that ContentEncoding is gzip .我们可以看到ContentEncodinggzip

So it looks like something's up with the server configuration.所以看起来服务器配置出了点问题。 Normally a server will only give you something with content-encoding if you explicitly state that you accept encoded responses with the Accept-Encoding header, but it looks like this particular server isn't playing by the rules for this file.通常,如果您明确 state 接受带有Accept-Encoding header 的编码响应,则服务器只会为您提供内容编码的内容,但看起来这个特定的服务器没有按照这个文件的规则播放。

Your browser did say that it accepted gzip-encoded files, so doesn't blink when it gets a gzip-encoded response.您的浏览器确实说它接受了 gzip 编码的文件,因此当它收到 gzip 编码的响应时不会闪烁。 Your C# code wasn't expecting that.您的 C# 代码没想到会这样。

You can get HttpClient to automatically decompress gzip-encoded content with:您可以使用以下方法让HttpClient自动解压缩 gzip 编码的内容:

var handler = new HttpClientHandler()
{
    AutomaticDecompression = System.Net.DecompressionMethods.GZip
};
var client = new HttpClient(handler);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM