Web响应未返回实际网页源

时间:2013-02-03 08:03:41

标签: c# httpwebrequest httpwebresponse

几天前我正在成功地从http://www.kse.com.pk/phps/mktSummary.php链接中提取网页来源,现在当我尝试做同样的事情时,它会返回以下回复

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><meta http-equiv="Content-Script-Type" content="text/javascript"><script type="text/javascript">function setCookie(c_name, value, expiredays) { var exdate = new Date(); exdate.setDate(exdate.getDate()+expiredays); document.cookie = c_name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires=" + exdate.toGMTString()) + ";path=/"; } function getHostUri() { var loc = document.location; return loc.toString(); } setCookie('YPF8827340282Jdskjhfiw_928937459182JAX666', 'xxx.xxx.xxx.xxx', 10); setCookie('DOAReferrer', document.referrer, 10); location.href = getHostUri();</script></head><body><noscript>This site requires JavaScript and Cookies to be enabled. Please change your browser settings or upgrade your browser.</noscript></body></html>

以上链接在本网站上用作框架:http://www.kse.com.pk 我正在使用此代码

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.kse.com.pk/phps/mktSummary.php");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream resStream = response.GetResponseStream();
source = new StreamReader(resStream).ReadToEnd();

2 个答案:

答案 0 :(得分:0)

试试这个:

string webPage = "";
using (var wc = new WebClient())
{
    webPage = wc.DownloadString("http://www.kse.com.pk/phps/mktSummary.php");
}

答案 1 :(得分:0)

在WebRequest中添加这些行解决了我的问题

request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";
request.Headers.Add("Cookie: YPF8827340282Jdskjhfiw_928937459182JAX666=xxx.xxx.xxx.xxx;");

xxx.xxx.xxx.xxx替换为您的IP地址