文本版|topic 高级搜索
   名人堂 帮助 论坛制度 意见反馈 | 首页 博客 周新贴 专题 求职 读书
RSS 底部
 
社区导航: 专家门诊   网络技术   操作系统   数据库   程序设计   系统应用   考试认证   CIO及信息化   站长交流   综合交流   下载基地  51CTO产品服务 设为首页 | 收藏本站
51CTO技术论坛» .Net » 通过 WebClient 类 快速生成静态Html .       [ 打印]  [ 订阅]  [ 收藏]  [ 推荐给朋友]   [ 本帖文本页]

论坛跳转:
     
标题: [原创] 通过 WebClient 类 快速生成静态Html .  ( 查看:412  回复:2 )   
 
maddish
新新人类  点击可查看详细



十二生肖之猪   天秤座   行业勋章   技术勋章   诚信兄弟  
帖子 20
精华 0
无忧币 838
积分 36
阅读权限 20
注册日期 2007-11-30
最后登录 2008-7-26 离线

[查看资料]  [发短消息]  [Blog
       
发表于:2008-2-29 12:10   标题:通过 WebClient 类 快速生成静态Html .
上一帖 |
最近公司时间比较赶所以我就用这个方法给主页生成静态..
至于为什么生成静态.有什么好处..
这里我就不多说了,各位去搜索了解就成.
这个方法我应用在首页...
设置了服务器默认打开是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" );
            }
最近公司时间比较赶所以我就用这个方法给主页生成静态..

至于为什么生成静态.有什么好处..
这里我就不多说了,各位去搜索了解就成.
这个方法我应用在首页...
设置了服务器默认打开是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();

// 完整代码

        ///  
        ///  生成HTML版本.
        ///  
        /// "Url">生成地址
        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" );
            }



网络虽虚拟,技术无边界,来看看大家“真面目”!
2008-2-29 12:101楼
[ 顶部 ]
 
maddish
新新人类  点击可查看详细



十二生肖之猪   天秤座   行业勋章   技术勋章   诚信兄弟  
帖子 20
精华 0
无忧币 838
积分 36
阅读权限 20
注册日期 2007-11-30
最后登录 2008-7-26 离线

[查看资料]  [发短消息]  [Blog
       
发表于:2008-4-29 17:53 
自己给自己顶一个先啊啊啊..



网络虽虚拟,技术无边界,来看看大家“真面目”!
2008-4-29 17:532楼
[ 顶部 ]
 
haolihai
新新人类  点击可查看详细



帖子 50
精华 0
无忧币 63
积分 50
阅读权限 20
注册日期 2007-5-27
最后登录 2008-5-13 离线

[查看资料]  [发短消息]  [Blog
       
发表于:2008-4-30 14:12 
同意,网上太多了



网络虽虚拟,技术无边界,来看看大家“真面目”!
2008-4-30 14:123楼
[ 顶部 ]
     
论坛跳转:  

| | |

| | |

| | |

标记已读 · 删除论坛Cookies · 文本版 · WAP
 
| 诚征版主 | 版主堂 | 意见建议 | 大史记 | 论坛地图
Copyright©2005-2008 51CTO.COM  Powered by Discuz!
本论坛言论纯属发布者个人意见,不代表51CTO网站立场!如有疑义,请与管理员联系。
京ICP备05051492号