动态生成静态网页

public void MakeHtml()
{
try
        {

//动态页面网址
 string strUrl = "https://www.psacloud.com/product/Detail/Default.aspx?ContentID=123";
//要存放静态网页的路径
 string filePath = "/product/";
//物理完整路径                    
            string toFileFullPath = HttpContext.Current.Server.MapPath(filePath);
            //检查是否有该路径没有就创建
            if (!System.IO.Directory.Exists(toFileFullPath))
            {
                System.IO.Directory.CreateDirectory(toFileFullPath);
            }
            filePath = filePath + "index.html";
            if (File.Exists(System.Web.HttpContext.Current.Server.MapPath(filePath)))
            {
                File.Delete(System.Web.HttpContext.Current.Server.MapPath(filePath));
            }
            //创建请求
            //如果访问的地址是带证书的https,需要加上这句话,证书认证
            ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
            //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
            HttpWebRequest Dd_gUrl = (HttpWebRequest)WebRequest.Create(strUrl);
            Dd_gUrl.UseDefaultCredentials = false;
            HttpWebResponse WebRe = (HttpWebResponse)Dd_gUrl.GetResponse();
            Stream strhtml = WebRe.GetResponseStream();
            StreamReader stRCmcnHtml = new StreamReader(strhtml, Encoding.GetEncoding("utf-8"));
            StreamWriter Sw = new StreamWriter(System.Web.HttpContext.Current.Server.MapPath(filePath), true, Encoding.GetEncoding("utf-8")); ;
            Sw.Write("<meta charset='utf-8'/>" + stRCmcnHtml.ReadToEnd());//<meta charset="utf-8"/>百度收录不乱码
            Sw.Flush();
            Sw.Close();
            strhtml.Close();
            WebRe.Close();
            stRCmcnHtml.Close();
            //return true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
       
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值