C#自动批量生成静态网页ASP.NET

C#自动批量生成静态网页ASP.NET


C#自动批量生成静态网页ASP.NET


直接上代码:



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>





        <asp:DropDownList ID="DropDownList1" runat="server" style="display:none" >
        </asp:DropDownList>



    <asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel3" runat="server">
<ContentTemplate>

<asp:Timer ID="Timer1" runat="server" Interval="1" OnTick="Timer1_Tick" 
        Enabled="False">
</asp:Timer>


操作时间:<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label><br />

<asp:Label ID="Label8" runat="server" Text="0"  style="display:none" ></asp:Label><br />


提示:<asp:Label ID="Label1" runat="server" Text="Label" ></asp:Label>

生成进度:<asp:Label ID="百分比数" runat="server" Text="Label"></asp:Label>

</ContentTemplate>
</asp:UpdatePanel>




    </div>
    <asp:Button ID="Button1" runat="server" Height="46px" onclick="Button1_Click" 
        Text="批量生成静态HTML" Width="138px" />
    </form>
</body>
</html>



后台代码:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Net;
using System.IO;
using System.Text;

public partial class _Default : System.Web.UI.Page
{

    protected void Timer1_Tick(object sender, EventArgs e)
    {


        Label3.Text =  DateTime.Now.ToString();

        //////////////////////////////////////////////////////

        int mm = int.Parse(Label8.Text);
        mm++;

        Label8.Text = mm.ToString();   ///  这里的作用是  递增的去选中 DropDownList1 的值


        //////////////////////////////////////////////////////////////

        try
        {




            DropDownList1.SelectedIndex = mm;
        }
        catch
        {
            Timer1.Enabled = false;

            ///任务结束后  停止,其实就是靠报错来停止的
        }



        string url = "http://localhost:30695/%E5%AE%98%E7%BD%91/news.aspx?=" + DropDownList1.Text ;

        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源文件


        Label1.Text = Html;

        string result = Html;


        try
        {



            // 生成的静态页的保存路径
            string savePathForStaticPage = Server.MapPath("~/news/"+"test-" + DropDownList1.Text + ".html");
            StreamWriter writer = new StreamWriter(savePathForStaticPage, false, Encoding.UTF8);
            writer.Write(result);
            writer.Close();



            Label1.Text = "test-" + DropDownList1.Text + ".html 文件生成成功!";
        }
        catch(Exception ex)
        {
            Label1.Text = ex.Message ;
        }



        string 总行数 = DropDownList1.Items.Count.ToString(); //显示行数  计算 百分比
        double a = Convert.ToDouble(总行数);
        double b = Convert.ToDouble(Label8.Text);
        double d = Math.Round(b / a * 100, 3);

        百分比数.Text = d.ToString();



    }



    protected void Page_Load(object sender, EventArgs e)
    {
        /////  这里是加载数据库 文章的ID  

        System.Data.SqlClient.SqlConnection sqlconn = new System.Data.SqlClient.SqlConnection();
        sqlconn.ConnectionString = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;  ///  这里是连接数据库的代码 (我写到web配置文件里面了)
        sqlconn.Open();
        System.Data.SqlClient.SqlDataAdapter sqldar = new System.Data.SqlClient.SqlDataAdapter("select * from 文章列表", sqlconn);

        System.Data.DataSet DataSet1 = new System.Data.DataSet();
        sqldar.Fill(DataSet1, "id");
        DropDownList1.DataSource = DataSet1.Tables["id"].DefaultView;
        DropDownList1.DataTextField = "id";
        DropDownList1.DataBind();
        sqlconn.Close();

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Timer1.Enabled = true;
    }
}


webform
桂ICP备16004754号