c# 中怎样读取http文件,并且写入文件
C#中想读取一个网页文件,并且保存到[X]:\name.txt中?最好带注释。好的我再加分!...
C# 中想读取一个网页文件,并且保存到[X]:\name.txt中?
最好带注释。
好的我再加分! 展开
最好带注释。
好的我再加分! 展开
展开全部
/// <summary>
/// 读取Url文件
/// </summary>
/// <param name="url">文件url</param>
/// <param name="fileName">保存文件</param>
public void ReadWebFile(string url,string fileName)
{
WebClient client = new WebClient();
client.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
client.Headers.Add("UserAgent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
client.DownloadFile(url, fileName);
}
/// 读取Url文件
/// </summary>
/// <param name="url">文件url</param>
/// <param name="fileName">保存文件</param>
public void ReadWebFile(string url,string fileName)
{
WebClient client = new WebClient();
client.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
client.Headers.Add("UserAgent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
client.DownloadFile(url, fileName);
}
展开全部
void SetType()
{
string pathHtm = Application.StartupPath + @"\1.htm";//路径
if (File.Exists(pathHtm))
File.Delete(pathHtm);
FileStream fs = new FileStream(pathHtm, FileMode.Create);
string picType = "";
StringBuilder sb = new StringBuilder();
sb.AppendFormat("网页文件", picType);
byte[] data = new UTF8Encoding().GetBytes(sb.ToString());
fs.Write(data, 0, data.Length);
fs.Flush();
fs.Close();
}
{
string pathHtm = Application.StartupPath + @"\1.htm";//路径
if (File.Exists(pathHtm))
File.Delete(pathHtm);
FileStream fs = new FileStream(pathHtm, FileMode.Create);
string picType = "";
StringBuilder sb = new StringBuilder();
sb.AppendFormat("网页文件", picType);
byte[] data = new UTF8Encoding().GetBytes(sb.ToString());
fs.Write(data, 0, data.Length);
fs.Flush();
fs.Close();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询