Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Trying to make a post request. I need to send in form data some russian text:
?q=test&a=русский текст
and, when i do the request, server get this:
?q=test&a=Ñ?Ñ?сский Ñ?ексÑ
in form data.
Here is the code:
C#
var values = new NameValueCollection();
values["q"] = "test";
values["a"] = "русский текст";

var parameters = new StringBuilder();

foreach (string key in values.Keys)
{
    parameters.AppendFormat("{0}={1}&", key, values[key]);
}
parameters.Length -= 1;
...
using (var writer = request.GetRequestStream())
{
    var data = Encoding.UTF8.GetBytes(parameters.ToString());
    writer.Write(data, 0, data.Length);
}

any ideas?
Posted

1 solution

You need to Encode the characters before transport and Decode after receiving on the server.

Something like this:

C#
foreach (string key in values.Keys)
{
    parameters.AppendFormat("{0}={1}&", key, Server.UrlEncode(values[key]));
}


Look at MSDN documentation:

http://msdn.microsoft.com/en-us/library/zttxte6w.aspx[^]

http://msdn.microsoft.com/en-us/library/6196h3wt.aspx[^]
 
Share this answer
 
Comments
Arlert 4-Jan-15 14:54pm    
this works with english letters
the problem remains unresolved with russian letters
Manas Bhardwaj 4-Jan-15 15:00pm    
If you are using .Net 4.0 or older version, you can try using HttpUtility.UrlEncodeUnicode API.
Arlert 4-Jan-15 15:03pm    
oh, man. thank you. also, had to send charset header = utf8.
p.s. without urlencode/urldecode it does not work
Manas Bhardwaj 4-Jan-15 15:25pm    
Glad it helped!

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