生成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; }
时间: 2024-11-13 09:01:22