public string CreatHtm_URL()
{
WebClient MyWebClient = new WebClient();
//获取或设置用于向Internet资源的请求进行身份验证的网络凭据
MyWebClient.Credentials = CredentialCache.DefaultCredentials;
//从指定网站下载数据
Byte[] pageData = MyWebClient.DownloadData("http://www.baidu.com");
//如果获取网站页面采用的是GB2312,则使用这句
string pageHtml = Encoding.UTF8.GetString(pageData);
string fileName = Guid.NewGuid().ToString() + ".htm";
//以日期创建文件夹存储C盘
string strDate = System.DateTime.Now.ToString("yyyyMMdd");
string path = "C:/htmlTemp" + "/" + strDate;
//将C盘下htmlTemp发布IIS后地址 http://192.168.1.1:8060/Htmltemp/
string htmlurl = "http://192.168.1.1:8060/Htmltemp/" + strDate;
if (Directory.Exists(path))
{
Console.WriteLine("此文件夹已经存在,无需创建!");
}
else
{
Directory.CreateDirectory(path);
}
using (FileStream fs = new FileStream(path + "/" + fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
String htmlContent = pageHtml;
//可以指定盘符,也可以指定任意文件名,还可以为word等文件
//FileStream fs = new FileStream("C:/htmlTemp/" + fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
// 创建写入流
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(htmlContent);
//关闭文件
sw.Close();
//如果不需要客户端下载该html文件,则可以删除。
//System.IO.File.Delete("C:/htmlTemp/" + fileName);
//返回IIS发布下载路径
return htmlurl + "/" + fileName;
}
}