使用 WebProxy 时 HttpWebRequest 的奇怪行为

发布于 2024-10-13 02:31:21 字数 1754 浏览 4 评论 0原文

我有一个带有 StreamReader 的 HttpWebRequest,它在不使用 WebProxy 的情况下工作得很好。当我使用 WebProxy 时,StreamReader 读取奇怪的字符而不是实际的 html。这是代码。

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://URL");
req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10";
req.Accept = "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
req.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
req.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
req.Headers.Add("Accept-Language", "en-US,en;q=0.8");
req.Method = "GET";
req.CookieContainer = new CookieContainer();

WebProxy proxy = new WebProxy("proxyIP:proxyPort");
proxy.Credentials = new NetworkCredential("proxyUser", "proxyPass");
req.Proxy = this.proxy;
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
StreamReader reader = new StreamReader(res.GetResponseStream());
string html = reader.ReadToEnd();

如果不使用 WebProxy,变量 html 将保存 URL 中预期的 html 字符串。但使用 WebProxy 时,html 保存的值如下:

"�\b\0\0\0\0\0\0��]r����s�Y����\0\tP\"]ki����� �-��X�\0\f���/�!�HU����>Cr���P$%�nR�� z�g��3�t�~q3�ٵş(M� ��14&?\r�d:�ex�j��p������.��Y��o�|��֎u�OO.������\v] ?}�~������E:�b��Lן����6+�l���岳��Y��y'ͧ��~#5ϩ�it�2��5��%�p� �E�L���t&x0:-�2��i�C���$M��_6��zU�tJ�>C-��GY�k�O�R$� P�T�8+�*]HY\"���$Ō�-�r�ʙ�H3\f8Jd��Q(:�G�E��r� ��R≤�������W�<]$����i>8\b�p� �\= 4\f�> �&��$��\v ��C��C�vC��x�p�|\"b9�ʤ�\r%i��w@��\t�r�M�� �����!� G�jP�8.D�k�Xʹt�J��/\v!�r��y\f7<����\",\a�/IK������������ п5�;���}h��+Q��IO]�8��c����n�AG带u2>�

I have an HttpWebRequest with a StreamReader that works very fine without using a WebProxy. When I use WebProxy, the StreamReader reads strange character instead of the actual html. Here is the code.

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://URL");
req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10";
req.Accept = "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
req.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
req.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
req.Headers.Add("Accept-Language", "en-US,en;q=0.8");
req.Method = "GET";
req.CookieContainer = new CookieContainer();

WebProxy proxy = new WebProxy("proxyIP:proxyPort");
proxy.Credentials = new NetworkCredential("proxyUser", "proxyPass");
req.Proxy = this.proxy;
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
StreamReader reader = new StreamReader(res.GetResponseStream());
string html = reader.ReadToEnd();

Without using the WebProxy, the variable html holds the expected html string from the URL. But with a WebProxy, html holds a value like that:

"�\b\0\0\0\0\0\0��]r����s�Y����\0\tP\"]ki���ػ��-��X�\0\f���/�!�HU���>Cr���P$%�nR�� z�g��3�t�~q3�ٵȋ(M���14&?\r�d:�ex�j��p������.��Y��o�|��ӎu�OO.�����\v]?}�~������E:�b��Lן�Ԙ6+�l���岳�Y��y'ͧ��~#5ϩ�it�2��5��%�p��E�L����t&x0:-�2��i�C���$M��_6��zU�t.J�>C-��GY��k�O�R$�P�T��8+�*]HY\"���$Ō�-�r�ʙ�H3\f8Jd���Q(:�G�E���r���Rܔ�ڨ�����W�<]$����i>8\b�p� �\= 4\f�> �&��$��\v��C��C�vC��x�p�|\"b9�ʤ�\r%i��w@��\t�r�M�� �����!�G�jP�8.D�k�Xʹt�J��/\v!�r��y\f7<����\",\a�/IK���ۚ�r�����ҿ5�;���}h��+Q��IO]�8��c����n�AGڟu2>�

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

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

发布评论

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

评论(2

标点 2024-10-20 02:31:21

既然您通过了,

req.Headers.Add("Accept-Encoding", "gzip,deflate,sdch"); 

我会说您的代理在将流发送回给您之前会对其进行压缩。

检查响应的标头以检查编码。

Since you are passing

req.Headers.Add("Accept-Encoding", "gzip,deflate,sdch"); 

I would say your proxy compress the stream before sending it back to you.

Inspect the headers of the Response to check the encoding.

不美如何 2024-10-20 02:31:21

只需使用Gzip解压即可。

Just use Gzip to decompress it.

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