熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

C# 獲取網頁html源文件

2013-11-13 10:45:56  來源: .NET編程 

  /// <summary>
/// 獲取網頁html源文件
/// </summary>
/// <param name="url">網頁地址</param>
/// <param name="encodingStr">網頁文件編碼字符串</param>
/// <returns>html源文件</returns>
#region GetPageSource
public static string GetPageSource(string url string encodingStr)
{
HttpWebResponse res = null;
string strResult = "";
try
{
HttpWebRequest req = (HttpWebRequest)WebRequestCreate(url);
//reqMethod = "POST";
reqKeepAlive = true;
reqContentType = "application/xwwwformurlencoded";
reqAccept = "text/Htmlapplication/xhtml+XMLapplication/xml;q=*/*;q=";
reqUserAgent = "Mozilla/ (Windows; U; Windows NT ; zhCN; rv:) Gecko/ Firefox/";
res = (HttpWebResponse)reqGetResponse();
StreamReader reader = new StreamReader(resGetResponseStream() EncodingGetEncoding(encodingStr));
strResult = readerReadToEnd();
readerClose();
}
catch
{

  }
finally
{
if (res != null)
{
resClose();
}
}
return strResult;
}


From:http://tw.wingwit.com/Article/program/net/201311/14461.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.