Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does it show question marks on the message box instead of text

Why does it shows question marks on the message box instead of text

enter image description here

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://teamxor.net/vb/tx48/"+ page);

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

StreamReader sr = new StreamReader(response.GetResponseStream());

string result = sr.ReadToEnd();

Regex r = new Regex("<div>.*?</div>");
MatchCollection mr = r.Matches(result);

foreach (Match m in mr)
{
    MessageBox.Show(m.Value, "Test", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.RtlReading);
}      
like image 668
Khaled Badawy Avatar asked Dec 24 '14 08:12

Khaled Badawy


People also ask

Why do I see question marks instead of text?

It means your Unicode text is getting converted to ANSI text somewhere. Since Unicode characters outside of Latin-1 can't be converted to ANSI, they are converted to question marks.

What does a question mark mean in a text message?

The QUESTION MARK is a request for clarity or confirmation. The QUESTION MARK emoji ( ) is often used in text messaging as a request for clarity or confirmation.

Why are there question marks instead of letters on my Mac?

The question marks indicate font substitution occurred because your Mac does not have the same font that was used when the content was created. The substituted font does not have the glyphs (character shapes) that exist in the specified font that is not present.


2 Answers

The problem lies in the use of a non-default code page. Your HTML shows you are using code page 1256. You have to tell .NET that, else it thinks it is UTF-8:

StreamReader sr = new StreamReader( response.GetResponseStream()
                                  , Encoding.GetEncoding(1256) // <-- this one
                                  );

Use Encoding.GetEncoding to get the right code page. I suggest to use UTF8 instead, since that is easily recognized by .NET.

like image 70
Patrick Hofman Avatar answered Sep 28 '22 03:09

Patrick Hofman


Web servers can return a response in whatever encoding they want, although they typically choose an encoding that matches the browser's preferred language.

The encoding used is returned as the charset element of the Content-Type header. In .NET you can retrieve the encoding used from the HttpWebResponse.CharacterSet property. You can use the returned charset to construct an Encoding object to use for reading the response:

var charset= response.CharacterSet;
var encoding = Encoding.GetEncoding(charset);
var sr= new StreamReader(response.GetResponseStream(),encoding);
....
like image 43
Panagiotis Kanavos Avatar answered Sep 28 '22 05:09

Panagiotis Kanavos