第1种方法
用serverExecute(path As Stringwriter As SysetemIOTextWriter) 方法这种方法很简单向服务器放松动态网页请求获取页面的客户端html代码然后把内容写进文件里.这种方法写起来比较简单
Dim swHtml As StringWriter = New StringWriter()
ServerExecute(http://localhost/newsSzhome/manage/newstemplateaspx swHtml)
Dim strContent As String = swHtmlToString()
Dim filepath As String = d//news//html
If Not (SystemIODirectoryExists(SystemIOPathGetDirectoryName(filepath))) Then
SystemIODirectoryCreateDirectory(SystemIOPathGetDirectoryName(filepath))
End If
Dim sw As StreamWriter = New StreamWriter(filepath False SystemTextEncodingDefault)
Try
swWrite(strContent )
Catch ex As Exception
Throw ex
Finally
swFlush()
swClose()
End Try
这种方法是必须读网页地址缺点显而易见速度慢另外如果请求的动态页面有验证控件的话返回的html页面却无法进行数据验证如果在同一个项目里的程序用这个还是很好的但是如果是要把生成程序跟网页程序分开(如写成webservice)的话用这个方法就相当与去打开一个外网网页效率肯定会大打折扣(而且我在webservice上用这方法根本运行不了程序出异常!具体原因没有去探索估计应该是权限的问题).
第2种方法
这个方法跟第1种方法相似(也是需要读取网页内容)用SystemNetWebRequestCreate(path As String)方法建里一个需要读取的网页的webRequest再获得它的WebResponse再以流的形式写入文件.
SystemNetWebRequest 代码实例
Dim wReq As System
Net
WebRequest = System
Net
WebRequest
Create(
http://localhost/newsSzhome/manage/newstemplate
aspx
Dim wResp As SystemNetWebResponse = wReqGetResponse
Dim srs As SystemIOStream = wRespGetResponseStream
Dim sr As SystemIOStreamReader = New SystemIOStreamReader(srs SystemTextEncodingDefault) GetEncoding(gb))
Dim strContent As String = srReadToEnd()
Dim filepath As String = d://news//html
If Not (SystemIODirectoryExists(SystemIOPathGetDirectoryName(filepath))) Then
SystemIODirectoryCreateDirectory(SystemIOPathGetDirectoryName(filepath))
End If
Dim sw As StreamWriter = New StreamWriter(filepath False SystemTextEncodingDefault)
Try
swWrite(temp)
Catch ex as Exception
Throw ex
Finally
swFlush()
swClose()
End Try
效果就不多说了跟第1种方法问题一样!(但是我在webservice中用上面这个方法生成时还是可以成功的但是速度慢很多.)
第3种
就是最常用也最实用的字符替代方法StringReplace()从文件读取模版替换模版中的参数后输出文件这种方法的生成速度上比第一种要快许多而且模版内容可以用工具任意编辑
主要代码
StringReplace方法
Dim sr As New System
IO
StreamReader(
d://newsDetail_template
htm
System
Text
Encoding
Default)
Dim temp As String = srReadToEnd()
temp = tempReplace(@$_CREATEDATE_$@ DateTimeNowToString)
Dim filepath As String = d://news//html
If Not (SystemIODirectoryExists(SystemIOPathGetDirectoryName(filepath))) Then
SystemIODirectoryCreateDirectory(SystemIOPathGetDirectoryName(filepath))
End If
Dim sw As StreamWriter = New StreamWriter(filepath False SystemTextEncodingDefault)
Try
swWrite(temp)
Catch
Return false
Finally
swFlush()
swClose()
End Try
这个方法读取的是硬盘里的纯文件类型在程序后台查询数据去替换掉模板文件里的特定字符.