曹永思

导航

.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

 

posted on 2015-01-27 16:53  曹永思  阅读(147)  评论(0编辑  收藏  举报