使用 WebProxy 时 HttpWebRequest 的奇怪行为
我有一个带有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
既然您通过了,
我会说您的代理在将流发送回给您之前会对其进行压缩。
检查响应的标头以检查编码。
Since you are passing
I would say your proxy compress the stream before sending it back to you.
Inspect the headers of the Response to check the encoding.
只需使用Gzip解压即可。
Just use Gzip to decompress it.