1. using System; 2. using System.Collections.Generic; 3. using System.Linq; 4. using System.Web; 5. using System.Net; 6. using System.IO; 7. using System.Text; 8. using System.Web.UI.HtmlControls; 9. using System.Text.RegularExpressions; 10. 11. /// <summary> 12. ///CreateHtml 的摘要说明 13. /// </summary> 14. public class CreateHtml:System.Web.UI.Page 15. { 16. public CreateHtml() 17. { 18. // 19. //TODO: 在此处添加构造函数逻辑 20. // 21. } 22. /// <summary> 23. /// 生成静态页面,生成位置是本项目下 24. /// </summary> 25. /// <param name="strURL">请求的URL</param> 26. /// <param name="strRelPath">创建的路径及文件名,路径为相对路径</param> 27. public bool Nei_Create(string strURL, string strRelPath) 28. { 29. string strFilePage; 30. 31. strFilePage = HttpContext.Current.Server.MapPath(strRelPath); 32. StreamWriter sw = null; 33. //获得aspx的静态html 34. try 35. { 36. 37. if (File.Exists(strFilePage)) 38. { 39. File.Delete(strFilePage); 40. } 41. sw = new StreamWriter(strFilePage, false, System.Text.Encoding.GetEncoding("gb2312")); 42. System.Net.WebRequest wReq = System.Net.WebRequest.Create(strURL); 43. System.Net.WebResponse wResp = wReq.GetResponse(); 44. System.IO.Stream respStream = wResp.GetResponseStream(); 45. System.IO.StreamReader reader = new System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding("gb2312")); 46. string strTemp = reader.ReadToEnd(); 47. 48. Regex r1 = new Regex("<input type=/"hidden/" name=/"__EVENTTARGET/".*/>", RegexOptions.IgnoreCase); 49. Regex r2 = new Regex("<input type=/"hidden/" name=/"__EVENTARGUMENT/".*/>", RegexOptions.IgnoreCase); 50. Regex r3 = new Regex("<input type=/"hidden/" name=/"__VIEWSTATE/".*/>", RegexOptions.IgnoreCase); 51. 52. Regex r4 = new Regex("<form .*id=/"form1/">", RegexOptions.IgnoreCase); 53. Regex r5 = new Regex("</form>"); 54. 55. Regex r6 = new Regex("<input type=/"hidden/" name=/"__EVENTVALIDATION/".*/>", RegexOptions.IgnoreCase); 56. strTemp = r1.Replace(strTemp, ""); 57. strTemp = r2.Replace(strTemp, ""); 58. strTemp = r3.Replace(strTemp, ""); 59. strTemp = r4.Replace(strTemp, ""); 60. strTemp = r5.Replace(strTemp, ""); 61. strTemp = r6.Replace(strTemp, ""); 62. 63. sw.Write(strTemp); 64. } 65. catch (Exception ex) 66. { 67. HttpContext.Current.Response.Write(ex.Message); 68. HttpContext.Current.Response.End(); 69. return false;//生成到出错 70. } 71. finally 72. { 73. sw.Flush(); 74. sw.Close(); 75. sw = null; 76. } 77. 78. return true; 79. } 80. /// <summary> 81. /// 生成静态页面,生成位置不在本项目下 82. /// </summary> 83. /// <param name="strURL">请求的URL</param> 84. /// <param name="strRelPath">创建的路径及文件名,路径为绝对路径</param> 85. public bool Wai_Create(string strURL, string strRelPath,string filename) 86. { 87. string strFilePage; 88. strFilePage = strRelPath + "//" + filename; 89. StreamWriter sw = null; 90. //获得aspx的静态html 91. try 92. { 93. if (!Directory.Exists(strRelPath)) 94. { 95. Directory.CreateDirectory(strRelPath); 96. } 97. if (File.Exists(strFilePage)) 98. { 99. File.Delete(strFilePage); 100. } 101. sw = new StreamWriter(strFilePage, false, System.Text.Encoding.GetEncoding("gb2312")); 102. System.Net.WebRequest wReq = System.Net.WebRequest.Create(strURL); 103. System.Net.WebResponse wResp = wReq.GetResponse(); 104. System.IO.Stream respStream = wResp.GetResponseStream(); 105. System.IO.StreamReader reader = new System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding("gb2312")); 106. string strTemp = reader.ReadToEnd(); 107. 108. Regex r1 = new Regex("<input type=/"hidden/" name=/"__EVENTTARGET/".*/>", RegexOptions.IgnoreCase); 109. Regex r2 = new Regex("<input type=/"hidden/" name=/"__EVENTARGUMENT/".*/>", RegexOptions.IgnoreCase); 110. Regex r3 = new Regex("<input type=/"hidden/" name=/"__VIEWSTATE/".*/>", RegexOptions.IgnoreCase); 111. 112. Regex r4 = new Regex("<form .*id=/"form1/">", RegexOptions.IgnoreCase); 113. Regex r5 = new Regex("</form>"); 114. 115. Regex r6 = new Regex("<input type=/"hidden/" name=/"__EVENTVALIDATION/".*/>", RegexOptions.IgnoreCase); 116. strTemp = r1.Replace(strTemp, ""); 117. strTemp = r2.Replace(strTemp, ""); 118. strTemp = r3.Replace(strTemp, ""); 119. strTemp = r4.Replace(strTemp, ""); 120. strTemp = r5.Replace(strTemp, ""); 121. strTemp = r6.Replace(strTemp, ""); 122. 123. sw.Write(strTemp); 124. } 125. catch (Exception ex) 126. { 127. HttpContext.Current.Response.Write(ex.Message); 128. HttpContext.Current.Response.End(); 129. return false;//生成到出错 130. } 131. finally 132. { 133. sw.Flush(); 134. sw.Close(); 135. sw = null; 136. } 137. 138. return true; 139. 140. } 141. public void FilePicDelete(string path) 142. { 143. System.IO.FileInfo file = new System.IO.FileInfo(path); 144. if (file.Exists) 145. file.Delete(); 146. } 147. } 用法: new CreateHtml().Nei_Create("http://localhost:4032/new5mdn/default.aspx", "default.htm");