根据链接获取网站的源码

    #region 根据链接获取网站源码并返回   

  

    /// <summary>

    /// 根据链接获取网站源码并返回

    /// </summary>

    /// <param name="url"></param>

    /// <returns></returns>

    private static string GetPage(string url)

    {

        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);

        HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();

        Stream stream = webResponse.GetResponseStream();

       // StreamReader reader = new StreamReader(stream, System.Text.Encoding.GetEncoding("GB2312"));

        StreamReader reader = new StreamReader(stream, System.Text.Encoding.GetEncoding("utf-8"));

        //整个页面内容

        string content = reader.ReadToEnd();

        reader.Close();

        webResponse.Close();

        return content.Replace("<script", "<script");

    }

    #endregion

  

你可能感兴趣的:(源码)