zoukankan
html css js c++ java
根据url地址生成静态页面
最近在做一个新闻系统,前台新闻页面要生成HTML静态页面,
自己找了一些资料,终于达到预期效果
现分享如下:
using
System.IO;
using
System.Text;
using
System.Net;
using
System.Configuration;
private
void
Button1_Click(
object
sender, System.EventArgs e)
{
Encoding code
=
Encoding.GetEncoding(
"
utf-8
"
);
StreamReader sr
=
null
;
StreamWriter sw
=
null
;
string
str
=
null
;
//
读取远程路径
WebRequest temp
=
WebRequest.Create(txtUrl.Text.Trim());
WebResponse myTemp
=
temp.GetResponse();
sr
=
new
StreamReader(myTemp.GetResponseStream(), code);
//
读取
try
{
sr
=
new
StreamReader(myTemp.GetResponseStream(), code);
str
=
sr.ReadToEnd();
}
catch
(Exception ex)
{
throw
ex;
}
finally
{
sr.Close();
}
string
path
=
HttpContext.Current.Server.MapPath(
"
../news
"
);
//
取得新闻当前文件夹
DateTime year
=
DateTime.Now;
string
years
=
Convert.ToString(year.Year);
//
当前年
string
month
=
Convert.ToString(year.Month);
//
当前月
string
CurrentPath
=
path
+
"
/
"
+
years;
//
设置当前年目录
if
(Directory.Exists(CurrentPath)
==
false
)
//
若该目录不存在,创建该目录
Directory.CreateDirectory(CurrentPath);
string
CurrentMonthPath
=
CurrentPath
+
"
/
"
+
month;
//
设置当前月目录
if
(Directory.Exists(CurrentMonthPath)
==
false
)
//
若该目录不存在,创建该目录
Directory.CreateDirectory(CurrentMonthPath);
string
fileName
=
DateTime.Now.ToString(
"
ddHHmmss
"
)
+
"
.htm
"
;
//
写入
try
{
sw
=
new
StreamWriter(CurrentMonthPath
+
"
/
"
+
fileName,
false
, code);
sw.Write(str);
sw.Flush();
}
catch
(Exception ex)
{
throw
ex;
}
finally
{
sw.Close();
Response.Write(
"
恭喜<a href=
"
+
CurrentMonthPath
+
"
/
"
+
fileName
+
"
target=_blank>
"
+
fileName
+
"
</a>已经生成,保存在htm文件夹下!
"
);
Response.Write(CurrentMonthPath
+
"
/
"
+
fileName);
}
}
查看全文
相关阅读:
Linux如何同步时间/更新时间
Linux关机/重启/及注销命令
Linux如何查看别名和取消别名
Linux查看历史记录
Linux命令行快捷键有哪些
win10产品密钥 win10永久激活密钥(可激活win10所有版本 )
Xshell如何连接
Xshell6 优化
逻辑运算符
可变类型与不可变类型
原文地址:https://www.cnblogs.com/mickey/p/796146.html
最新文章
Streaming Principal Component Analysis in Noisy Settings
Oja’s rule
Randomized Online PCA Algorithms with Regret Bounds that are Logarithmic in the Dimension
Stochastic Optimization of PCA with Capped MSG
QR分解
Sparse Principal Component Analysis
Deflation Methods for Sparse PCA
Full Regularization Path for Sparse Principal Component Analysis
Selenium全屏截图,使用PIL拼接滚动截图
Selenium执行cdp命令,driver.execute_cdp_cmd用法
热门文章
Selenium中使用Cookies绕过登录
Pytest权威教程19-编写钩子(Hooks)方法函数
Pytest权威教程18-插件编写
Pytest权威教程17-安装和使用插件
Pytest权威教程16-经典xUnit风格的setup/teardown
Pytest权威教程15-运行Nose用例
Pytest权威教程14-缓存:使用跨执行状态
Pytest权威教程13-Fixture方法及测试用例的参数化
Linux如何显示所有IP地址
Linux如何获取所有网卡的IP地址
Copyright © 2011-2022 走看看