最近公司时间比较赶所以我就用这个方法给主页生成静态..
至于为什么生成静态.有什么好处..
这里我就不多说了,各位去搜索了解就成.
这个方法我应用在首页...
设置了服务器默认打开是Default.html,第二个是Default.aspx..
所以在生成的时候即使有人访问也可以建立连接.
下面采用的是C#代码,但是思想很简单,无论什么语言都可以使用。
有什么问题可以提出来一起讨论吧....o(∩_∩)o...!
闲话少说吧.写代码吧
#region
"using namespace"
using System;
using System.Web;
using System.Net;
using System.Text;
using System.IO;
#endregion
using System;
using System.Web;
using System.Net;
using System.Text;
using System.IO;
#endregion
// 创建WebClient实例提供向URI 标识的资源发送数据和从URI 标识的资源接收数据
WebClient myWebClient =
new WebClient();
// 获取或设置用于对向 Internet 资源的请求进行身份验证的网络凭据。
myWebClient.Credentials = CredentialCache.DefaultCredentials;
// 从资源下载数据并返回字节数组。
byte [ ] pagedata = myWebClient.DownloadData( Url );
// 得到远程流
string myDataBuffer = Encoding.Default.GetString( pagedata );
// 生成Html静态文件的路径
string path = HttpContext.Current.Server.MapPath(
"/" );
// 编码格式
Encoding code = Encoding.GetEncoding(
"gb2312" );
// 生成的文件名称.可以是 shtml htm html
string htmlfilename =
"Default.html";
// 实现文本流写入类
// 参数1 创建一个指定路径的空文件,
// 参数2 不追加数据
// 参数3 设置文件的指定编码
StreamWriter sw = new StreamWriter( path + htmlfilename , false , code );
// 写入文件内容
// 清理缓冲区
sw.WriteLine( myDataBuffer );
sw.Flush();
sw.Flush();
// 完整代码
/// <summary>
/// 生成HTML版本.
/// </summary>
/// <param name="Url">生成地址</param>
public void GetRemoteHtmlCode( string Url )
{
WebClient myWebClient = new WebClient();
myWebClient.Credentials = CredentialCache.DefaultCredentials;
byte [ ] pagedata = myWebClient.DownloadData( Url );
string myDataBuffer = Encoding.Default.GetString( pagedata );
string path = HttpContext.Current.Server.MapPath( "/" );
Encoding code = Encoding.GetEncoding( "gb2312" );
string htmlfilename = "Default.html";
try
{
StreamWriter sw = new StreamWriter(path + htmlfilename, false, code);
sw.WriteLine(myDataBuffer);
sw.Flush();
Response.Write( "ok");
}
catch( Exception ex )
{
File.Delete( path + htmlfilename );
HttpContext.Current.Response.Write( ex.Message );
HttpContext.Current.Response.End();
Response.Write( "no" );
}
finally
{
if( sw != null )
sw.Close();
}
}
/// 生成HTML版本.
/// </summary>
/// <param name="Url">生成地址</param>
public void GetRemoteHtmlCode( string Url )
{
WebClient myWebClient = new WebClient();
myWebClient.Credentials = CredentialCache.DefaultCredentials;
byte [ ] pagedata = myWebClient.DownloadData( Url );
string myDataBuffer = Encoding.Default.GetString( pagedata );
string path = HttpContext.Current.Server.MapPath( "/" );
Encoding code = Encoding.GetEncoding( "gb2312" );
string htmlfilename = "Default.html";
try
{
StreamWriter sw = new StreamWriter(path + htmlfilename, false, code);
sw.WriteLine(myDataBuffer);
sw.Flush();
Response.Write( "ok");
}
catch( Exception ex )
{
File.Delete( path + htmlfilename );
HttpContext.Current.Response.Write( ex.Message );
HttpContext.Current.Response.End();
Response.Write( "no" );
}
finally
{
if( sw != null )
sw.Close();
}
}
调用例子:
try
{
/* 本机调试需要换成页面地址.例如:[url]http://localhost:10118/Default.aspx[/url] */
GetRemoteHtmlCode(
"http://" + Request.Url.Host.ToString() + "/Default.aspx" );
}
catch( Exception ex )
{
throw new Exception( ex.Message +
"Url:" + "http://" + Request.Url.Host.ToString() + "/Default.aspx" );
}
{
/* 本机调试需要换成页面地址.例如:[url]http://localhost:10118/Default.aspx[/url] */
GetRemoteHtmlCode(
"http://" + Request.Url.Host.ToString() + "/Default.aspx" );
}
catch( Exception ex )
{
throw new Exception( ex.Message +
"Url:" + "http://" + Request.Url.Host.ToString() + "/Default.aspx" );
}