Click here to Skip to main content
15,885,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C#
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();


I tried I think everything after the GetResponseStream part, found by google. Tried to add the followings, but no success:

C#
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:

C#
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?
Posted
Updated 17-Sep-15 23:06pm
v3

1 solution

You can use HttpServerUtility.UrlDecode(myUrl) provided in System.Web namespace
 
Share this answer
 
Comments
DoomMaker79 21-Sep-15 4:14am    
i've already tried it, not working... :( maybe I tried it in wrong way. can you please complete the code above with your suggestion?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900