通过 WebClient.DownloadData 自动解压缩 gzip 响应

发布于 2024-09-04 08:48:05 字数 388 浏览 3 评论 0原文

我希望自动解压缩 GZiped 响应。 我正在使用以下代码片段:

mywebclient.Headers[HttpRequestHeader.AcceptEncoding] = "gzip";
mywebclient.Encoding = Encoding.UTF8;

try
{
    var resp = mywebclient.DownloadData(someUrl);
}

我已检查 HttpRequestHeader enum,并且没有选项可以通过 Headers 执行此操作

我如何自动解压缩响应?或者我应该使用另一个函数来代替 mywebclient.DownloadData

I wish to automatically uncompress GZiped response.
I am using the following snippet:

mywebclient.Headers[HttpRequestHeader.AcceptEncoding] = "gzip";
mywebclient.Encoding = Encoding.UTF8;

try
{
    var resp = mywebclient.DownloadData(someUrl);
}

I have checked HttpRequestHeader enum, and there is no option to do this via the Headers

How can I automatically decompress the resp? or Is there another function I should use instead of mywebclient.DownloadData ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲凉≈ 2024-09-11 08:48:05

WebClient 在幕后使用 HttpWebRequest。并且HttpWebRequest支持gzip/deflate解压。请参阅 HttpWebRequest 自动解压缩属性

但是,WebClient 类不会直接公开此属性。因此,您必须从它派生来设置底层 HttpWebRequest 的属性。

class MyWebClient : WebClient
{
    protected override WebRequest GetWebRequest(Uri address)
    {
        HttpWebRequest request = base.GetWebRequest(address) as HttpWebRequest;
        request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
        return request;
    }
}

WebClient uses HttpWebRequest under the covers. And HttpWebRequest supports gzip/deflate decompression. See HttpWebRequest AutomaticDecompression property

However, WebClient class does not expose this property directly. So you will have to derive from it to set the property on the underlying HttpWebRequest.

class MyWebClient : WebClient
{
    protected override WebRequest GetWebRequest(Uri address)
    {
        HttpWebRequest request = base.GetWebRequest(address) as HttpWebRequest;
        request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
        return request;
    }
}
執念 2024-09-11 08:48:05

根据您的情况,自己减压可能会更简单。

using System.IO.Compression;
using System.Net;

try
{
    var client = new WebClient();
    client.Headers[HttpRequestHeader.AcceptEncoding] = "gzip";
    var responseStream = new GZipStream(client.OpenRead(myUrl), CompressionMode.Decompress);
    var reader = new StreamReader(responseStream);
    var textResponse = reader.ReadToEnd();

    // do stuff

}

为了清楚起见,我创建了所有临时变量。这一切都可以扁平化为仅 clienttextResponse

或者,如果简单性是目标,您甚至可以使用 Demis Bellot 的 ServiceStack.Text 来完成此操作:(

using ServiceStack.Text;

var resp = "some url".GetJsonFromUrl();

有其他 .Get*FromUrl 扩展方法)

Depending on your situation, it may be simpler to do the decompression yourself.

using System.IO.Compression;
using System.Net;

try
{
    var client = new WebClient();
    client.Headers[HttpRequestHeader.AcceptEncoding] = "gzip";
    var responseStream = new GZipStream(client.OpenRead(myUrl), CompressionMode.Decompress);
    var reader = new StreamReader(responseStream);
    var textResponse = reader.ReadToEnd();

    // do stuff

}

I created all the temporary variables for clarity. This can all be flattened to only client and textResponse.

Or, if simplicity is the goal, you could even do this using ServiceStack.Text by Demis Bellot:

using ServiceStack.Text;

var resp = "some url".GetJsonFromUrl();

(There are other .Get*FromUrl extension methods)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文