用asp.net c# HttpWebRequest獲取網(wǎng)頁源代碼

字號(hào):


    該方法需要傳遞目標(biāo)網(wǎng)頁的編碼方式,比如System.Text.Encoding.Default或者System.Text.Encoding.UTF8
    如果哪位高手知道如何自動(dòng)判斷目標(biāo)頁面的編碼格式,請(qǐng)?jiān)谠u(píng)論中告知。謝謝!
    ///
    /// 獲取源代碼
    ///
    ///
    ///
    ///
    public static string GetPage(string url, Encoding encoding)
    {
    HttpWebRequest request = null;
    HttpWebResponse response = null;
    StreamReader reader = null;
    try
    {
    request = (HttpWebRequest)WebRequest.Create(url);
    request.UserAgent = "";
    request.Timeout = 20000;
    request.AllowAutoRedirect = false;
    response = (HttpWebResponse)request.GetResponse();
    if (response.StatusCode == HttpStatusCode.OK && response.ContentLength < 1024 * 1024)
    {
    reader = new StreamReader(response.GetResponseStream(), encoding);
    string html = reader.ReadToEnd();
    return html;
    }
    }
    catch
    {
    }
    finally
    {
    if (response != null)
    {
    response.Close();
    response = null;
    }
    if (reader != null)
    reader.Close();
    if (request != null)
    request = null;
    }
    return string.Empty;