生成静态网页

爷,独闯天下 提交于 2020-01-01 22:28:37
/// <summary>
    /// 生成静态网页
    /// </summary>
    /// <param name="str_Url">htm文件的物理地址 要完整的</param>
    /// <param name="str_U">网址要完整的</param>
    public void jtwy(string str_U, string str_Url)
    {
        try
        {
            if (File.Exists(str_Url) == true)
            {
                File.Delete(str_Url);
            }
            //   ===建立流文件===  
            System.IO.StreamWriter s = new StreamWriter((System.IO.Stream)File.OpenWrite(str_Url), System.Text.Encoding.GetEncoding("GB2312"));

            string cFetchStr = "";
            string cException = "";
            HttpWebRequest httpRequest;
            httpRequest = (HttpWebRequest)WebRequest.Create(str_U);
            httpRequest.Accept = "*/*";
            HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
            System.Text.Encoding strCoding = Encoding.Default;  //获取当前页的编码

            StreamReader sResponseStream = new StreamReader(httpResponse.GetResponseStream(), strCoding);

            cFetchStr = sResponseStream.ReadToEnd();

            httpResponse.Close();
            sResponseStream.Close();
            s.WriteLine(cFetchStr);   //   生成文件  
            s.Flush();
            s.Close();
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
    }

public void FetchHtml(string str_U, string str_Url)
    {
        //str_Url  htm文件的物理地址
        //str_U  aspx文件的路京

        if (File.Exists(str_Url) == true)
        {
            File.Delete(str_Url);
        }
        //   ===建立流文件===  
        System.IO.StreamWriter s = new StreamWriter((System.IO.Stream)File.OpenWrite(str_Url), System.Text.Encoding.GetEncoding("GB2312"));

        string cFetchStr = "";
        string cException = "";
        HttpWebRequest httpRequest;
        try
        {

            httpRequest = (HttpWebRequest)WebRequest.Create(str_U);
            httpRequest.Accept = "*/*";
            HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
            System.Text.Encoding strCoding = Encoding.Default;  //获取当前页的编码

            StreamReader sResponseStream = new StreamReader(httpResponse.GetResponseStream(), strCoding);

            cFetchStr = sResponseStream.ReadToEnd();

            httpResponse.Close();
            sResponseStream.Close();


        }
        catch (Exception ee)
        {

        }

        s.WriteLine(cFetchStr);   //   生成文件  
        s.Flush();
        s.Close();
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        FetchHtml("http://whseopm.sh09.host.35.com/default1.aspx", MapPath("../index.html"));
        ClientScript.RegisterStartupScript(this.GetType(), "", string.Format("<script>alert('生成成功')</script>"));
    }

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!