using System;
using System.IO;
using System.Net;
using System.Text;
namespace WebDB.Common.Mail
{
/// <summary>
/// 取得網頁內容類別
/// </summary>
public sealed class PageContent
{
private static PageContent content = null;
/// <summary>
/// 設定編碼為 UTF8
/// </summary>
private Encoding coding = Encoding.UTF8;
/// <summary>
/// 取得指定網頁內容
/// </summary>
/// <param name="strUrl">要擷取內容的網址</param>
/// <returns>網頁內容</returns>
public string GetHtml(string strUrl)
{
StringBuilder sbContent = new StringBuilder();
WebResponse resp = null;
StreamReader sr = null;
try
{
Uri uri = new Uri(strUrl);
WebRequest req = WebRequest.Create(uri);
//req.PreAuthenticate = true;
// 使用預設認證機制(Windows 憑證)
req.Credentials = CredentialCache.DefaultCredentials;
req.Proxy = WebRequest.DefaultWebProxy;
resp = req.GetResponse();
Stream stream = resp.GetResponseStream();
sr = new StreamReader(stream, coding);
string strTmp = "";
while ((strTmp = sr.ReadLine()) != null)
{
sbContent.Append(strTmp.Trim());
}
}
catch (Exception e)
{
throw new ArgumentException("取得網頁內容有誤!", e);
}
finally
{
if (sr != null)
{
sr.Close();
}
if (resp != null)
{
resp.Close();
}
}
return sbContent.ToString();
}