最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】C#中HttpWebRequest使用Proxy后异常

C# crifan 7135浏览 0评论

【问题】

已经给C#的HttpWebRequest支持了Proxy了:

【已解决】C#中HttpWebRequest支持代理

但是接下来发生一个很郁闷的事情:

当用了代理之后,http工作不正常了,现象是:

之前可以去模拟某网站获得alexa rank和page rank

现在都无法获得了。

 

【解决过程】

1.关于加了代理后,工作异常,不工作,参考了很多帖子:

HttpWebRequest and proxy issues

WebRequest through Proxy which does not allow POST

Why does Http Web Request and IWebProxy work at weird times

httpWebRequest through Proxy

Http WebRequest/Response not working with proxy settings

c# – HttpWebRequest one proxy and one not

但是貌似都没啥帮助。

2.经过调试后发现, 使用了本地的gae的代理:

127.0.0.1:8087

的gae代理后,返回的html内容变成乱码了:

use proxy return html wrong

 

看起来,像是未解压的html。

3.所以,看来要去找:

加了代理后,html不能正常解压了。

因为我之前加了代码:

        req.Headers["Accept-Encoding"] = "gzip, deflate";
        req.AutomaticDecompression = DecompressionMethods.GZip;

可以正常自动解压html的。

4.找到一个:

Advanced HttpWebRequest Issue

结果发现,其只是自己去手动解压的。效率很低。

5.后来经过自己调试,发现,在用了gae的代理:

gProxy = new WebProxy("127.0.0.1", 8087);

后,返回的html的header是:

{Content-Encoding: deflate

Content-Length: 2249

Set-Cookie: ax=1; expires=Wed, 10-Jul-2013 08:14:56 GMT; path=/,info2=1373443496%231%231%231373443496; expires=Sat, 05-Jul-2014 08:04:56 GMT; path=/,det2=277496; expires=Sat, 05-Jul-2014 08:04:56 GMT; path=/,huid=4f17624266d684afef6934ec0b130727; expires=Sat, 05-Jul-2014 08:04:56 GMT; path=/,domainOnly=robin.hubpages.com; expires=Wed, 10-Jul-2013 09:04:56 GMT; path=/,domainWWW=robin.hubpages.com; expires=Wed, 10-Jul-2013 09:04:56 GMT; path=/,domainURL=robin.hubpages.com; expires=Wed, 10-Jul-2013 09:04:56 GMT; path=/

Via: HTTP/1.1 GWA

Server: Apache

Connection: close

Date: Wed, 10 Jul 2013 08:04:55 GMT

Content-Type: text/html

}

很明显,对应的压缩方式是deflate:

Content-Encoding: deflate

然后回头再看看自己之前的,支持自动解压缩的代码:

req.AutomaticDecompression = DecompressionMethods.GZip;

就明白了,就类似于之前折腾Python使用gae代理时候遇到的问题一样:

【已解决】Python中使用代理访问网络

也是返回了

Content-Encoding: deflate

而之前只支持:

Content-Encoding: gzip

所以出错的,所以,此处也是:

给C#中,添加,不仅支持gzip,也支持deflate。

5.所以,自然可以猜测到,是不是AutomaticDecompression一次性支持多种压缩方式,通过"|"或的关系传递进去的。

然后就找到了:

Does .NET’s HttpWebResponse uncompress automatically GZiped and Deflated responses?

果然和猜测的是一样的。

所以去改代码为:

        req.Headers["Accept-Encoding"] = "gzip, deflate";
        //req.AutomaticDecompression = DecompressionMethods.GZip;
        req.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

然后再去调试看看,返回的

Content-Encoding: deflate

类型的html,是不是已经可以自动解压缩了。

结果果然是可以的:

not support deflate html decompress

 

【总结】

不论是python还是C#,都是要了解相关的http中的gzip,deflate方面的基本知识,然后才可以去添加对应的html的gzip和deflate方面的解压缩的支持的。

此处,对于C#,核心代码为:

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

......

req.Headers["Accept-Encoding"] = "gzip, deflate";
//req.AutomaticDecompression = DecompressionMethods.GZip;
req.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

之后,当获得的html不论是gzip的还是deflate的

(对应的response的headers中,是:

Content-Encoding: gzip

Content-Encoding: deflate

),其都可以自动地,解压为原始的html(字符串)了。

(否则看起来就是乱码,且是二进制级别的那种乱码)

转载请注明:在路上 » 【已解决】C#中HttpWebRequest使用Proxy后异常

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.223 seconds, using 22.11MB memory