WebClient.DownloadFile下载文件,URL包含中文的问题

leavesosoon 2017-03-21 05:11:47
System.Net.WebClient webClient = new System.Net.WebClient();
webClient.DownloadFile("http://localhost:8080/downloadfile/中文测试.txt", "D\中文测试.txt");
我用这种方式进行下载文件,但是URL包含中文,解析不到地址,导致报错404.应该怎么搞?
...全文
367 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuxiaol2 2017-10-20
  • 打赏
  • 举报
回复
System.Net.WebClient webClient = new System.Net.WebClient(); 这样 webClient.DownloadFile(Server.HtmlEncode("http://localhost:8080/downloadfile/中文测试.txt"), "D\中文测试.txt"); 或这样 webClient.DownloadFile(HttpUtility.HtmlEncode("http://localhost:8080/downloadfile/中文测试.txt"), "D\中文测试.txt");
Hvai 2017-10-20
  • 打赏
  • 举报
回复
我也遇到这问题,有搞定吗?
SoulRed 2017-03-21
  • 打赏
  • 举报
回复
encoder 一下。转码。
using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Net.Sockets; using System.Collections; using System.IO; using System.Text.RegularExpressions; using RE = System.Text.RegularExpressions.Regex; using System.Security.Cryptography.X509Certificates; /*************************************************************************************************************************************************** * *文件名:HttpProc.cs * *创建人:HeDaode * *日 期:2007.09.01 * *描 述:实现HTTP协议中的GET、POST请求 * *使 用:HttpProc.WebClient client = new HttpProc.WebClient(); client.Encoding = System.Text.Encoding.Default;//默认编码方式,根据需要设置其他类型 client.OpenRead("http://www.baidu.com");//普通get请求 MessageBox.Show(client.RespHtml);//获取返回的网页源代码 client.DownloadFile("http://www.codepub.com/upload/163album.rar",@"C:\163album.rar");//下载文件 client.OpenRead("http://passport.baidu.com/?login","username=zhangsan&password=123456");//提交表单,此处是登录百度的示例 client.UploadFile("http://hiup.baidu.com/zhangsan/upload", @"file1=D:\1.mp3");//上传文件 client.UploadFile("http://hiup.baidu.com/zhangsan/upload", "folder=myfolder&size=4003550",@"file1=D:\1.mp3");//提交含文本域和文件域的表单 *****************************************************************************************************************************************************/ namespace HttpProc { /// ///上传事件委托 /// /// /// public delegate void WebClientUploadEvent(object sender, HttpProc.UploadEventArgs e); /// ///下载事件委托 /// /// /// public delegate void WebClientDownloadEvent(object sender, HttpProc.DownloadEventArgs e); /// ///上传事件参数 /// public struct UploadEventArgs { /// ///上传数据总大小 ///

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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