locked
korean characters not displayed right RRS feed

  • Question

  • User13071441 posted

    Hi,

    My asp.net page gets a parameter that might contain korean characters. I do the following:

    string title = Server.UrlDecode(this.Request.QueryString["title"]);

    status.Text = title; //where status is an asp.net label control

     

    When the above code is executed, status.Text displays a bunch of question marks.

    I tried chaning web browser encoding to korean and unicode but no luck.

    Chaning the first line of the code to

    string title = this.Request.QueryString["title"];

    doesn't help either.

     

    Does anyone know a solution to this problem?

     

    Friday, August 10, 2007 8:39 PM

Answers

  • User1622957740 posted

    What encoding are you using on the page? And what encoding is the query string in? Make sure that your Request and Response encoding in Web.config is set to UTF-8 and you should be Ok assuming that the data sent to you is properly encoded in the same way.

    <globalization requestEncoding="UTF-8" responseEncoding="UTF-8"/>

    You shouldn't need UrlDecode - ASP.NET will do that for you automatically, but content encoding can be tricky depending on how the querystring was formatted on the input. If you have UTF-8 encoding enabled but the query string isn't actually encoded that way you're likely to get junk or most likely a locale encoded double byte string instead.

    So, if you still get invalid characters it probably means that the data is not Unicode/UTF formatted but ANSI (double byte) and you need to do some conversion first. Try this:

    byte[] RawBytes = Encoding.Default.GetBytes();
    string Unicode = Encoding.Unicode.GetString(RawBytes);

    You may have to use a specific Encoding (rather than Default). The code basically converts the raw input to bytes using a specific character Encoding and the converts back to Unicode so you can display it properly.

    +++ Rick ---

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 12, 2007 2:35 AM
  • User13071441 posted

    Hey Rick.

    Thank you for your help. 

    I tried your solution, but it didn't work.

     

    Actually, the problem was on the webbrowser side.

    I have a WebBrowser control in a windows form and it is used to view my webpage.

    Paramters in url is encoded by

    string first_part="http://some url.com/default.aspx?"; 

    string second_part = string.Format("parameter={0}", HttpUtility.UrlEncode(firstparameter, System.Text.Encoding.UTF8)); //paramter is encoded in UTF8

     

    Webbrowser control in the web form opens my webpage by: 

    this.webBrowser1.Url = new System.Uri(first_part + second_part, System.UriKind.Absolute,false);    

     

    What it actually does is that it first decodes (without telling me!!) the url, so if the encoded url is "http://www.d.com/default?p=%ea" where %ea is some character in korean, it simply replaces %ea with that korean character and then my asp.net page gets decoded url!!

    Yeah, I know. I already shot my self in the foot.
     

    Chage the last code with

    this.webBrowser1.Navigate(first_part + second_part);

    and it works fine.

    I hope this helps those with same problem.

     

    And thanks again Rick for taking your time to answer my question!
     

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 12, 2007 10:41 PM

All replies

  • User1622957740 posted

    What encoding are you using on the page? And what encoding is the query string in? Make sure that your Request and Response encoding in Web.config is set to UTF-8 and you should be Ok assuming that the data sent to you is properly encoded in the same way.

    <globalization requestEncoding="UTF-8" responseEncoding="UTF-8"/>

    You shouldn't need UrlDecode - ASP.NET will do that for you automatically, but content encoding can be tricky depending on how the querystring was formatted on the input. If you have UTF-8 encoding enabled but the query string isn't actually encoded that way you're likely to get junk or most likely a locale encoded double byte string instead.

    So, if you still get invalid characters it probably means that the data is not Unicode/UTF formatted but ANSI (double byte) and you need to do some conversion first. Try this:

    byte[] RawBytes = Encoding.Default.GetBytes();
    string Unicode = Encoding.Unicode.GetString(RawBytes);

    You may have to use a specific Encoding (rather than Default). The code basically converts the raw input to bytes using a specific character Encoding and the converts back to Unicode so you can display it properly.

    +++ Rick ---

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 12, 2007 2:35 AM
  • User13071441 posted

    Hey Rick.

    Thank you for your help. 

    I tried your solution, but it didn't work.

     

    Actually, the problem was on the webbrowser side.

    I have a WebBrowser control in a windows form and it is used to view my webpage.

    Paramters in url is encoded by

    string first_part="http://some url.com/default.aspx?"; 

    string second_part = string.Format("parameter={0}", HttpUtility.UrlEncode(firstparameter, System.Text.Encoding.UTF8)); //paramter is encoded in UTF8

     

    Webbrowser control in the web form opens my webpage by: 

    this.webBrowser1.Url = new System.Uri(first_part + second_part, System.UriKind.Absolute,false);    

     

    What it actually does is that it first decodes (without telling me!!) the url, so if the encoded url is "http://www.d.com/default?p=%ea" where %ea is some character in korean, it simply replaces %ea with that korean character and then my asp.net page gets decoded url!!

    Yeah, I know. I already shot my self in the foot.
     

    Chage the last code with

    this.webBrowser1.Navigate(first_part + second_part);

    and it works fine.

    I hope this helps those with same problem.

     

    And thanks again Rick for taking your time to answer my question!
     

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 12, 2007 10:41 PM