技术文章 > C#通过Url抓取网页内容(2)

C#通过Url抓取网页内容(2)

2018-07-17 23:22

文档管理软件,文档管理系统,知识管理系统,档案管理系统的技术资料:
[code]
csharp
private static string getContent(string Url)
{
string strResult = "";
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
//声明一个HttpWebRequest请求
request.Timeout = 30000;
//设置连接超时时间
request.Headers.Set("Pragma", "no-cache");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream streamReceive = response.GetResponseStream();
Encoding encoding = Encoding.GetEncoding("GB2312");
StreamReader streamReader = new StreamReader(streamReceive, encoding);
strResult = streamReader.ReadToEnd();
streamReader.Close();
}
catch
{
throw;
}
return strResult;
}
[/code]