.net 根据网址生成静态页

生成HTML页面代码

技术分享
    public int Htmls(int id)
        {
            int result = 0;

            string strHtmlContent = "";
            HttpWebRequest request;
            Encoding encoding = Encoding.GetEncoding("utf-8");
            StreamReader sr = null;
            StreamWriter sw = null;

            string urls = "http://www.baidu.com/";

            try
            { //声明一个HttpWebRequest请求
                request = (HttpWebRequest)WebRequest.Create(urls);
                //连接超时时间
                request.Timeout = 20000;
                request.Headers.Set("Pragma", "no-cache");
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream streamHtmlCode = response.GetResponseStream();

                sr = new StreamReader(streamHtmlCode, encoding);
                strHtmlContent = sr.ReadToEnd();

            }
            catch (Exception ex)
            {
                result = 2;
                Log.Write(urls + "=Message=" + ex.Message + " StackTrace=" + ex.StackTrace);
                // System.Threading.Thread.Sleep(20000);
            }
            finally
            {//
                //  sr.Close();
                request = null;
            }
            string fileName = id + ".html";

            //写入
            try
            {
                sw = new StreamWriter(HttpContext.Current.Server.MapPath("/shengxiao/jm/") + fileName, false, encoding);
                sw.Write(strHtmlContent);
                sw.Flush();

                result = 1;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (sw != null)
                {
                    sw.Close();
                }
                // Response.Write(urls);
            }

            return result;
        }
View Code

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。