简体   繁体   中英

HttpWebRequest returns empty text

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!

我遇到了同样的问题,原因是我以前将方法设置为HEAD而在以后的版本中则需要解析正文。

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM