HttpWebRequest 返回空文本
当我尝试获取此 URL 的内容时 http://www.yellowpages.com.au/qld/gatton /a-12000029-listing.html
using System.Net;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(link);
request.AllowAutoRedirect = true;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream resStream = response.GetResponseStream();
StreamReader objSR;
objSR = new StreamReader(resStream, System.Text.Encoding.GetEncoding("utf-8"));
string sResponse = objSR.ReadToEnd();
我没有收到服务器的任何响应。请帮我找出为什么会发生这种情况。
提前致谢!
When I try to fetch the content of this URL
http://www.yellowpages.com.au/qld/gatton/a-12000029-listing.html
using System.Net;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(link);
request.AllowAutoRedirect = true;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream resStream = response.GetResponseStream();
StreamReader objSR;
objSR = new StreamReader(resStream, System.Text.Encoding.GetEncoding("utf-8"));
string sResponse = objSR.ReadToEnd();
I don't get any response from the server. Please help me find out why this happens.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它很可能正在查看用户代理并拒绝向无法识别自身身份的客户端提供内容。尝试在请求对象上设置 UserAgent 属性。
It may well be looking at the user agent and refusing to serve content to a client that doesn't identify itself. Try setting the UserAgent property on your request object.
在我看来,该网站正在检查引用网址,如果指定了无效的引用网址,则可能会提供空内容。
尝试设置
request.Referer = "http://www.google.com";
。对引荐来源网址进行实验,看看这是否会改变响应。我还会按照 Matthew 的建议尝试 UserAgent 属性。Looks to me like that site is checking the referrer url and may be serving up empty content if an invalid referrer is specified.
Try setting
request.Referer = "http://www.google.com";
. Experiment with the referrer to see if that changes the response. I'd also try the UserAgent property as Matthew suggested.我也遇到了同样的问题,原因是我之前将方法设置为
HEAD
并且在后来的修订中需要解析正文。I had the same problem and the cause was that I previously had set the method to
HEAD
and in later revisions had the need to parse the body.