您现在的位置是:首页
>
总结调整和测试静态工作点的方法 .net生成静态页方法总结
. et生成静态页方法总结 第1种方法 用 erver Execute ath A Stri g writer A Sy etem IO TextWriter 方法 这种方法很简单 向服务器放松
.net生成静态页方法总结
第1种方法
用server Execute(path As String writer As Sysetem IO TextWriter) 方法 这种方法很简单 向服务器放松动态网页请求 获取页面的客户端代码 然后把内容写进文件里.这种方法写起来比较简单
Dim swHtml As StringWriter = New StringWriter() Server Execute( //localhost/newsSzhome/manage/newstemplate aspx swHtml) Dim strContent As String = swHtml ToString() Dim filepath As String = d//news// If Not (System IO Directory Exists(System IO Path GetDirectoryName(filepath))) Then System IO Directory CreateDirectory(System IO Path GetDirectoryName(filepath)) End If Dim sw As StreamWriter = New StreamWriter(filepath False System Text Encoding Default) Try sw Write(strContent ) Catch ex As Exception Throw ex Finally sw Flush() sw Close() End Try
这种方法是必须读网页地址 缺点显而易见 速度慢 另外如果请求的动态页面有验证控件的话 返回的页面却无法进行数据验证 如果在同一个项目里的程序用这个还是很好的 但是如果是要把生成程序跟网页程序分开(如写成webservice)的话 用这个方法就相当与去打开一个外网网页 效率肯定会大打折扣(而且我在webservice上用这方法根本运行不了 程序出异常!具体原因没有去探索 估计应该是权限的问题). 第2种方法

这个方法跟第1种方法相似(也是需要读取网页内容) 用System Net WebRequest Create(path As String)方法建里一个需要读取的网页的webRequest 再获得它的WebResponse 再以流的形式写入文件.
System Net WebRequest 代码实例
Dim wReq As System Net WebRequest = System Net WebRequest Create( //localhost/newsSzhome/manage/newstemplate aspx Dim wResp As System Net WebResponse = wReq GetResponse Dim srs As System IO Stream = wResp GetResponseStream Dim sr As System IO StreamReader = New System IO StreamReader(srs System Text Encoding Default) GetEncoding( gb )) Dim strContent As String = sr ReadToEnd() Dim filepath As String = d://news// If Not (System IO Directory Exists(System IO Path GetDirectoryName(filepath))) Then System IO Directory CreateDirectory(System IO Path GetDirectoryName(filepath)) End If Dim sw As StreamWriter = New StreamWriter(filepath False System Text Encoding Default) Try sw Write(temp) Catch ex as Exception Throw ex Finally sw Flush() sw Close() End Try效果就不多说了 跟第1种方法问题一样!(但是我在webservice中用上面这个方法生成时还是可以成功的 但是速度慢很多.)
第3种
就是最常用也最实用的字符替代方法String Replace() 从文件读取模版 替换模版中的参数后输出文件 这种方法的生成速度上比第一种要快许多 而且模版内容可以用工具任意编辑
主要代码
String Replace方法
Dim sr As New System IO StreamReader( d://newsDetail_template System Text Encoding Default) Dim temp As String = sr ReadToEnd() temp = temp Replace( @$_CREATEDATE_$@ DateTime Now ToString) Dim filepath As String = d://news// If Not (System IO Directory Exists(System IO Path GetDirectoryName(filepath))) Then System IO Directory CreateDirectory(System IO Path GetDirectoryName(filepath)) End If Dim sw As StreamWriter = New StreamWriter(filepath False System Text Encoding Default) Try sw Write(temp) Catch Return false Finally sw Flush() sw Close() End Try lishixinzhi/Article/program/net/201311/15243 很赞哦! (1035)