用webclient实现.aspx生成html问题 高手请进

bulebirds 2009-03-16 03:50:39
现项目中要实现动态文件.aspx页面生成静态文件.html页面的功能

这几天查了些资料,发现使用webclient最为简洁
(new webClient()).DownloadFile(sourceUrl, targetFullyFile)
[sourceUrl表示新闻详细信息页面,targetFullyFile表示静态页面地址]
但是遇到个问题一直无法解决
就是项目的用户验证问题,项目中所有的页面都要登录后才能访问,否则都跳转到登录页面
现在sourceUrl每次生成的都是登录页面的html,虽然我是在项目中调用DownloadFile的,
但是好像new webClient()后上下文信息就没了,所有调用后生成的页面是DownloadFile
执行过程中挑战到的login.aspx页面。

请高手指点迷津!!!谢谢
...全文
231 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
bulebirds 2009-03-17
  • 打赏
  • 举报
回复
好像还是不行哎
che2piaopiao 2009-03-17
  • 打赏
  • 举报
回复

最近公司时间比较赶所以我就用这个方法给主页生成静态..
至于为什么生成静态.有什么好处..
这里我就不多说了,各位去搜索了解就成.
这个方法我应用在首页...
设置了服务器默认打开是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" );
}
che2piaopiao 2009-03-17
  • 打赏
  • 举报
回复

最近公司时间比较赶所以我就用这个方法给主页生成静态..
至于为什么生成静态.有什么好处..
这里我就不多说了,各位去搜索了解就成.
这个方法我应用在首页...
设置了服务器默认打开是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" );
}
周公 2009-03-16
  • 打赏
  • 举报
回复
要设置登录信息。

62,072

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧