读取Internet网站上的某些内容
当需要用到某个网站上的一些内容时,可以使用下面的方法,获取整个网页的内容,然后在截取自己想要的那部分
private string GetWebContent(string Url)
{
string strResult = "";
try
{
//声明一个HttpWebRequest请求
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
//设置连接超时时间
request.Timeout = 30000;
request.Headers.Set("Pragma", "no-cache");
//这个一定要加上,在某些网站没有会发生"远程服务器返回错误: (403) 已禁止。"错误
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; QQWubi 133; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; CIBA; InfoPath.2)";
//rq.Accept = "*/*";
//rq.Accept = "image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*";
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream streamReceive = response.GetResponseStream();
Encoding encoding = Encoding.GetEncoding("GB2312");
StreamReader streamReader = new StreamReader(streamReceive, encoding);
strResult = streamReader.ReadToEnd();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
return strResult;
}