.NET:是否有可能得到的HttpWebRequest自动DECOM preSS gzip压缩的反应?(.NET: Is it possible to get HttpWebRequest to automatically decompress gzip'd responses)

   IT问题网   2019-08-13 00:00:00

问 题

在<一个href="http://stackoverflow.com/questions/2813989/how-do-i-read-a-secure-rss-feed-into-a-syndicationfeed-without-providing-credenti/2815624#2815624">this回答,我描述了我是如何使出了httpwebresponse wrappnig各地的响应流的gzipstream,以decom press它。

相关code是这样的:

httpwebrequest的hwr =(httpwebrequest的)webrequest.create(url);
hwr.cookiecontainer =
persistentcookies.getcookiecontainerforurl(url);
hwr.accept ="为text / xml,* / *";
hwr.headers.add(htt的prequestheader.acceptencoding,"gzip,放气");
hwr.headers.add(htt的prequestheader.acceptlanguage,"en-us");
hwr.useragent ="我的特殊应用程序";
hwr.keepalive = true;

使用(var resp =(httpwebresponse)hwr.getresponse())
{
使用(流s = resp.getresponsestream())
{
流s2 =秒;
如果(resp.contentencoding.tolower()。包含("gzip"))
s2 =新gzipstream(s2,com pressionmode.decom press);
否则,如果(resp.contentencoding.tolower()。包含("放气"))
s2 =新deflatestream(s2,com pressionmode.decom press);

...使用s2 ...
}
}
 

有没有办法让httpwebresponse为解-com pressing流,自动?换句话说,这是一种对我来说,以消除上述code以下内容:

 流s2 = s;
如果(resp.contentencoding.tolower()。包含("gzip"))
s2 =新gzipstream(s2,com pressionmode.decom press);
否则,如果(resp.contentencoding.tolower()。包含("放气"))
s2 =新deflatestream(s2,com pressionmode.decom press);
 

感谢。

解决方案

使用的<一个href="http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.automaticdecom$p$pssion.aspx">httpwebrequest.automaticdecom$p$pssion属性如下:

httpwebrequest的hwr =(httpwebrequest的)webrequest.create(url);
hwr.automaticdecom pression = decom pressionmethods.deflate | decom pressionmethods.gzip;
 

这是没有必要手动添加接受编码 http标头;当属性用于它会自动被添加。

(另外,我知道这只是例子code,但 httpwebresponse 对象应放置在使用块,它的正确处置,当你使用它已经完成了。)

标签:是否是不是有可能得到自动压缩紧缩反应反映



分享:

  • 微信
  • QQ好友
  • QQ空间
  • 新浪微博


热门推荐

如何检查是否一个程序正在使用.NET?(How to check if a program is using .NET)

problem can we check if a running application or a progra ...

使用Visual Studio安装项目以自动注册和GAC一个COM互操作DLL(Use Visual Studio Setup Project to automatically register and GAC a COM Interop DLL)

problem i've created a .net assembly for com interop and ...

如何获得MX记录与System.Net.DNS DNS名称?(How to get mx records for a dns name with System.Net.DNS)

problem is there any built in method in the .net library ...

我为什么要preFER单曲“等待Task.WhenAll”在多个等待着?(Why should I prefer single &#39;await Task.WhenAll&#39; over multiple awaits)

problem in case i don't care about the order ...

我该如何分析和转换日期时间的到RFC 822的日期,时间格式?(How do I parse and convert DateTime’s to the RFC 822 date-time format)

problem how do i convert a datetime structure to its equi ...

有什么区别呢.AsNoTracking()做什么呢?(What difference does .AsNoTracking() make)

problem i have a question regarding the .asnotracking() e ...

实体框架/ LINQ到SQL:跳过和放大器;采取(Entity Framework/Linq to SQL: Skip &amp; Take)

problem just curious as to how skip amp; take are suppose ...

StreamWriter.Flush()和StreamWriter.Close()之间的区别是什么?(What is the difference between StreamWriter.Flush() and StreamWriter.Close())

problem what is the difference in functionality between s ...

现净switch语句散列或索引?(Are .Net switch statements hashed or indexed)

problem does .net 4 (or any prior version) perform any so ...

使用LINQ生成没有选择直接更新(Use linq to generate direct update without select)

problem g'day everyone. i'm still learning l ...

是否有可能拦截控制台输出?(Is it possible to intercept Console output)

problem i call a method, say, fizzbuzz(), over which i ha ...

当使用排序列表 TKEY的,TValue在一个SortedDictionary TKEY的,TValue?(When to use a SortedList&lt;TKey, TValue&gt; over a SortedDictionary&lt;TKey, TValue&gt;)

problem this may appear to be a duplicate of this questio ...

如何初始化列表 T给定的尺寸(相对于容量)?(How to initialize a List&lt;T&gt; to a given size (as opposed to capacity))

problem .net offers a generic list container whose perfor ...

如何使用XPath用的XDocument?(how to use XPath with XDocument)

problem there is a similar question, but it seems that th ...

什么是WebConfigurationManager和ConfigurationManager中的区别?(What&#39;s the difference between the WebConfigurationManager and the ConfigurationManager)

problem what's the difference between the webconfiguratio ...

NUnit的测试运行秩序(NUnit Test Run Order)

problem by default nunit tests run alphabetic ...

如何恢复从C#数据库(How to restore a database from C#)

problem i have a sql 2008 db. i am running a form that ba ...

调用堆栈不说&QUOT;你来自哪里&QUOT;但是&QUOT;你要去哪里,下一个&QUOT;?(The call stack does not say &quot;where you came from&quot;, but &quot;where you are going next&quot;)

problem in a previous question (get object ca ...

如何读取和写入ID3标签在C#中的MP3?(How to read and write ID3 tags to an MP3 in C#)

problem is there a library for reading and writing id3 ta ...

如何设置读取权限的X.509证书从.NET私钥文件(How to set read permission on the private key file of X.509 certificate from .NET)

problem here is the code to add a pfx to the cert store. ...