注册 | 登录 忘记密码? 51cto首页 | 博客 | 论坛 | 招聘
热点文章 一个黑客与一个电脑白痴的..
 帮助

通过 WebClient 类 快速生成静态Html .


2008-02-17 11:37:18
版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://maddish.blog.51cto.com/282288/62259
最近公司时间比较赶所以我就用这个方法给主页生成静态..
至于为什么生成静态.有什么好处..
这里我就不多说了,各位去搜索了解就成. 
这个方法我应用在首页...
设置了服务器默认打开是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
 
// 创建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();
 
// 完整代码
        /// <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();
            }
        }
 
调用例子:
            try
            {
                /* 本机调试需要换成页面地址.例如:http://localhost:10118/Default.aspx */

                GetRemoteHtmlCode(  
                    "http://" + Request.Url.Host.ToString() + "/Default.aspx" );
            }
            catch( Exception ex )
            {
                throw new Exception( ex.Message +  
                    "Url:" + "http://" + Request.Url.Host.ToString() + "/Default.aspx" );
            }
 

本文出自 “maddish” 博客,请务必保留此出处http://maddish.blog.51cto.com/282288/62259





    文章评论
 
2008-02-17 21:36:38
不错 在许多网页中可以用到

 

发表评论

昵   称:
验证码:  点击图片可刷新验证码  博客过2级,无需填写验证码
内   容: