适合C#asp.net 生成静态的简易代码 高效率

    protected void Button2_Click(object sender, EventArgs e)
    {

     
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sql"].ConnectionString);
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from 文章列表", con);
        SqlDataReader sdr = cmd.ExecuteReader();
        for (int i = 0; i <= 100; i++)//  Convert.ToInt32(zong.Text)
        {


            if (sdr.Read())
            {

            string url = "http://www.域名.com/news.aspx?=" + sdr["id"].ToString();

            string Html = string.Empty;//初始化新的webRequst
            HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(url);

            Request.KeepAlive = true;
            Request.ProtocolVersion = HttpVersion.Version11;
            Request.Method = "GET";
            Request.Accept = "*/* ";
            Request.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5";
            Request.Referer = url;

            HttpWebResponse htmlResponse = (HttpWebResponse)Request.GetResponse();
            //从Internet资源返回数据流
            Stream htmlStream = htmlResponse.GetResponseStream();
            //读取数据流
            StreamReader weatherStreamReader = new StreamReader(htmlStream, Encoding.UTF8);
            //读取数据
            Html = weatherStreamReader.ReadToEnd();
            weatherStreamReader.Close();
            htmlStream.Close();
            htmlResponse.Close();
            //针对不同的网站查看html源文件



            StreamWriter FileWriter = new StreamWriter(Server.MapPath(@"~/newss/") + sdr["id"].ToString() + ".html", true); //写文件
            FileWriter.Write(Html);//将字符串写入

            FileWriter.Flush();

            FileWriter.Close(); //关闭StreamWriter对象






            }
        }



        sdr.Close();
        con.Close();
    }
}








再说一下 ,网上有人用  myPage.Server.Execute("./news.aspx?=" , strHTML); 这个模式来获取

,反正我用了 就会出问题,总是获取到第一个页面的HTML源码,那个是不行的,我们换个思路,直接获取客户端源码,然后把他保存为HTML文件




webform
桂ICP备16004754号