Click here to Skip to main content
15,891,375 members

Character encoding problem in ListView

DoomMaker79 asked:

Open original thread
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?
Tags: C#, Encoding, WebResponse

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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