ListView中的字符编码问题 [英] Character encoding problem in ListView
问题描述
大家好,
我有一个小代码,它将服务器上的目录列为ListView。我的问题是,如果文件名包含外来字符或空格,则字符以类似Web格式显示(空格=%20等)。我列出目录的URL是一个HTTPS网址!我的代码是:
Hi all,
I have a small code that lists a directory on a server to a ListView. My problem is that if a filename contains foreign characters or maybe spaces, the characters are shown in 'web-like' format (space = %20, etc). The URL from I'm listing the directory is a HTTPS url! My code is:
url = "https://myaddress/directory/";
Uri uri = new Uri(url);
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);//it is needed because of the HTTPS
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
HttpWebResponse response = null;
response = (HttpWebResponse)request.GetResponse();
Encoding encoding = Encoding.GetEncoding("UTF-8");
StreamReader reader = new StreamReader(response.GetResponseStream(), encoding);
string html = reader.ReadToEnd();
我尝试了在谷歌找到的GetResponseStream部分之后的所有内容。尝试添加以下内容,但没有成功:
I tried I think everything after the GetResponseStream part, found by google. Tried to add the followings, but no success:
request.ContentType = "text/html; charset=UTF-8";
request.Headers.Add(HttpRequestHeader.AcceptCharset, "UTF-8");
当然我不想以这种丑陋的方式解决这个问题:
Of course I don't want to solve this problem in this ugly way:
html = html.Replace("%20", " ")
.Replace("%C3%81", "Á")
.Replace("%C3%A1", "á")
.Replace("%C3%89", "É")
.Replace("%C3%A9", "é")
.Replace("%C3%8D", "Í")
.Replace("%C3%AD", "í")
.Replace("%C3%93", "Ó")
.Replace("%C3%B3", "ó")
.Replace("%C3%96", "Ö")
.Replace("%C3%B6", "ö")
.Replace("%C5%90", "Ő")
.Replace("%C5%91", "ő")
.Replace("%C3%9A", "Ú")
.Replace("%C3%BA", "ú")
.Replace("%C3%9C", "Ü")
.Replace("%C3%BC", "ü")
.Replace("%C5%B0", "Ű")
.Replace("%C5%B1", "ű");
有什么想法吗?
Any idea?
推荐答案
您可以使用System.Web命名空间中提供的HttpServerUtility.UrlDecode(myUrl)
You can use HttpServerUtility.UrlDecode(myUrl) provided in System.Web namespace
这篇关于ListView中的字符编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!