现项目中要实现动态文件.aspx页面生成静态文件.html页面的功能这几天查了些资料,发现使用webclient最为简洁
(new webClient()).DownloadFile(sourceUrl, targetFullyFile)
[sourceUrl表示新闻详细信息页面,targetFullyFile表示静态页面地址]
但是遇到个问题一直无法解决
就是项目的用户验证问题,项目中所有的页面都要登录后才能访问,否则都跳转到登录页面
现在sourceUrl每次生成的都是登录页面的html,虽然我是在项目中调用DownloadFile的,
但是好像new webClient()后上下文信息就没了,所有调用后生成的页面是DownloadFile
执行过程中挑战到的login.aspx页面。请高手指点迷津!!!谢谢
(new webClient()).DownloadFile(sourceUrl, targetFullyFile)
[sourceUrl表示新闻详细信息页面,targetFullyFile表示静态页面地址]
但是遇到个问题一直无法解决
就是项目的用户验证问题,项目中所有的页面都要登录后才能访问,否则都跳转到登录页面
现在sourceUrl每次生成的都是登录页面的html,虽然我是在项目中调用DownloadFile的,
但是好像new webClient()后上下文信息就没了,所有调用后生成的页面是DownloadFile
执行过程中挑战到的login.aspx页面。请高手指点迷津!!!谢谢
解决方案 »
- WORD转换为HTML如何实现?
- 正则问题:能否替换html标签外的空格字符?
- 绑定????????????????
- 公司最近要我做一个Syncml同步的东西,用ASPX(C#)做,请各位高手指正一下。顶者有分。
- ASP.net 2.0中上传图片被拒绝访问
- 为什么form里加上target="_blank"后,在maxthon里提交就没反应了。
- 如何获取对一个文件的排他访问权?
- 安装aspnetforum时遇到的问题!进来看看。
- 子窗口“无提示”的刷新父窗口
- 验证控件和弹出窗口?
- 怎样把一个treeview的递归改成checkBoxlist的递归
- 如何限制GRIDVIEW从数据库里读出的图片的大小
最近公司时间比较赶所以我就用这个方法给主页生成静态..
至于为什么生成静态.有什么好处..
这里我就不多说了,各位去搜索了解就成.
这个方法我应用在首页...
设置了服务器默认打开是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();
// 完整代码
/// <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" );
}