locked
Encoding issue with WordPress comment RRS feed

  • Question

  • hello,

    I'm developing an app that is supposed to be able to post comment on a website provided by WordPress.

    So to send the comment, I build an HttpWebRequest object like this :

    HttpWebRequest _req = HttpWebRequest.CreateHttp("http:...");
    _req.Method = "POST";
    _req.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";

    This request call a webService written in PHP that take back the parameters (POST) and call the wp_insert_comment function provided by the Wordpress's API.

    So with this ContentType combined to the parameter's UTF-8 encoding, I thought that there would be no issue. However, when I try to insert a special character (like '€'), the result in the database is this like that 'è'...

    I tried many things to solve this problem : encoding the parameters to Unicode, use utf8_encode in the webservice, remove the charset on the contentType, I have no solutions.

    If you have any solution, Thanks very much !

    Wednesday, August 8, 2012 8:17 PM

Answers

  • '/303/251'

    Odd, that is 'į' 'û', not 'é' - but anyway.... I missed something in your first post... (EEK!)

    If you look closely, you are saying to the server "I have URL encoded data in the post" but you are sending non-url encoded and therefore invalid data.

    To URL encode the string, do this:

    string strEncoded = Uri.EscapeUriString(sb.ToString());
    byte[] byteArray = Encoding.ASCII.GetBytes(strEncoded);
    

    That will encode the data properly with UTF8 and then get the proper ascii bytes to send in the POST.  Notice I changed the encoding to ASCII, because that is the only characters EscapeURIString will return, and that is all that is allowed in a x-www-form-urlencoded body.


    Darin R.

    • Marked as answer by rafoufoun Monday, August 13, 2012 8:06 PM
    Monday, August 13, 2012 7:09 PM

All replies

  • Try and put a network monitor in between your app and the php server, is your app properly sending UTF-8, or is it incorrect there?  (Note:  the network monitor may not decode the byte properly - so stick with the binary view and work through decoding the data yourself.)

    If it is mangled in the monitor, check your writing to the output stream.  Post that code, we can help further.

    But, if the network monitor output looks OK from the phone, then start tracing into PHP and forget about the phone code being a problem.  Verify your PHP can handle unicode or utf-8.  If it can, and you can verify that - then start to look at the database.

    Is your database viewer capable of displaying those special characters in the first place? 


    Darin R.

    Wednesday, August 8, 2012 9:09 PM
  • Hello Darin,

    Fisrt, thanks for your complete answer :)

    So I tried to put a network monitor to handle the request and I discovered that the 'é' character is writter as '/303/251' in the request data. i don't know if it's normal.

    as you asked for it, I put my writing to the output stream :

    CryptClass c = new CryptClass();
    string password = c.Encrypt(this.pwd);
    
    string content = this.contentBlock.Text;
    
    StringBuilder sb = new StringBuilder();
    
    sb.AppendFormat("log={0}&pwd={1}&post_title={2}&content={3}", this.log, password, this.item.title, content);
    
    byte[] byteArray = Encoding.UTF8.GetBytes(sb.ToString());
    
    Stream dataStream = _req.EndGetRequestStream(asyncResult);
    dataStream.Write(byteArray, 0, byteArray.Length);
    dataStream.Close();

    Saturday, August 11, 2012 12:13 PM
  • '/303/251'

    Odd, that is 'į' 'û', not 'é' - but anyway.... I missed something in your first post... (EEK!)

    If you look closely, you are saying to the server "I have URL encoded data in the post" but you are sending non-url encoded and therefore invalid data.

    To URL encode the string, do this:

    string strEncoded = Uri.EscapeUriString(sb.ToString());
    byte[] byteArray = Encoding.ASCII.GetBytes(strEncoded);
    

    That will encode the data properly with UTF8 and then get the proper ascii bytes to send in the POST.  Notice I changed the encoding to ASCII, because that is the only characters EscapeURIString will return, and that is all that is allowed in a x-www-form-urlencoded body.


    Darin R.

    • Marked as answer by rafoufoun Monday, August 13, 2012 8:06 PM
    Monday, August 13, 2012 7:09 PM
  • Oh Darin, you've saved me !

    I just added

    string strEncoded = Uri.EscapeUriString(sb.ToString());
    

    to my code and it works well. However, I have kept the UTF-8 encoing because, for windows phone, we have not access to Encoding.ASCII.

    But thanks a lot Darin !

     
    Monday, August 13, 2012 8:06 PM