2

I use post method to submit data to and i have used

HttpUtility.UrlEncode(string data)

for Url Encode of data. But for some certain string the data at server side is shown different then what i submitted.

EX:

string data = "¦=¦=¦";
data = HttpUtility.UrlEncode(data);

after submitting data to server it shows string as ¦=¦=¦ but original string was ¦=¦=¦.

And which url encode i should use that can encode "(" to %28 , ")" to %29. I can find this.

I also used HttpUtility.UrlEncodeUnicode ,HttpUtility.UrlPathEncode but don't work. Which url encoding is suitable for this type of string?

Prince123
  • 97
  • 1
  • 8

2 Answers2

2

Use HttpUtility.UrlDecode(data) to get it back as "¦=¦=¦"

HatSoft
  • 11,077
  • 3
  • 28
  • 43
  • I would suggest you keep the encoding same on each side e.g string data = "¦=¦=¦"; data = HttpUtility.UrlEncode(data, Encoding.UTF8); string dataOnServerSide = HttpUtility.UrlDecode(data, Encoding.UTF8); – HatSoft Jun 21 '12 at 13:32
  • you can achieve decode from server side by Creating Custom HttpHandler http://msdn.microsoft.com/en-us/library/bb398986.aspx – HatSoft Jun 21 '12 at 13:38
  • didn't work for me. I can find the problem is with my code or else? – Prince123 Jun 21 '12 at 15:16
  • i think i have problem in sending of that data stream but not sure. – Prince123 Jun 21 '12 at 15:30
  • 1
    Incase you haven't, Install Fiddler2 and use it to trace request and data, check the raw format, it will give you better idea whats going on – HatSoft Jun 21 '12 at 15:34
  • i have installed fiddler2 and raw data are missed match with raw data using the browser so i think this may be sending code fault. So i have added the sending code that i am using now. – Prince123 Jun 21 '12 at 15:46
1

In your code example you are using ASCII for char "¦" this is not a valid ASCII char. Use Unicode and it might work.

System.Text.UnicodeEncoding uni = new UnicodeEncoding();
byteArray = uni.GetBytes(data);
retslig
  • 888
  • 5
  • 22
  • You are using ASCII for char "¦" this is not a valid ASCII char. Use Unicode and it might work. – retslig Jun 22 '12 at 13:02
  • Can you show the code you are using again, as when I test this I get the ¦=¦=¦ as expected. However I am using MVC controller. – retslig Jun 22 '12 at 18:45
  • My code is here http://stackoverflow.com/questions/11158916/what-url-encoding-web-browser-uses – Prince123 Jun 22 '12 at 19:49