0

I have to encode string in c# and decode it with javascript unescape function.

the javascript unescape is the only option since I am sending the string with get request to some api that using unescape to decoed it.

i tried almost everything

server.urlencode
WebUtility.HtmlEncode

and a lot other encoding! I even tried Uri.EscapeDataString using jscript

Nothing isn't encode like the "escape" function

Any idea How to make it work?

EDIT:

this is my code

string apiGetRequest = String.Format("http://212.00.00.00/Klita?name={0}&city={1}&CREATEBY=test ", Uri.EscapeDataString(name), Uri.EscapeDataString(city));
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(apiGetRequest);
req.GetResponse();
Oded
  • 489,969
  • 99
  • 883
  • 1,009
baaroz
  • 19,119
  • 7
  • 37
  • 47
  • 1
    What does the string represent? That is, is it HTML? A URL? Something else? Why do you need to encode it? Please post some sample strings and how you expect them to be escaped. – Oded Sep 04 '12 at 09:14
  • look at this: http://stackoverflow.com/questions/3778165/unescape-javascripts-escape-using-c-sharp – Sethu Sep 04 '12 at 09:24
  • I edit mt question and added code sample – baaroz Sep 04 '12 at 09:28
  • And what seems to be the problem on the JavaScript side? You should be able to access the URL parameters without a problem in JavaScript. – Oded Sep 04 '12 at 09:29
  • when I take the encode string and try to unescape it in firebug ,it doesn't seem to change it back to original string – baaroz Sep 04 '12 at 09:43
  • Can you please post examples of the original string and the escaped version, so we can see what you are talking about? – Oded Sep 04 '12 at 09:44

1 Answers1

1

Can you give an example of the string you want do encode and the encoded result?

URLencoding is the correct encoding-type you need. Make sure, you don't double encode your string somewhere in your code.

You might need to use decodeURIComponent instead of unescape, since unescape is not UTF-8 aware, thus might result in in broken string after decoding.

See http://xkr.us/articles/javascript/encode-compare/ for more information.

EDIT:

I don't know much about asp, but it looks like your trying to access the url not with a browser but with your ASP-server-side application. Well, your server does not run any JS code. You will just retrieve the HTML markup and maybe some JS code as a big string. This code would be parsed and executed within a browser but not within ASP.

LeJared
  • 2,032
  • 1
  • 17
  • 28
  • I know that decodeURIComponent will work,unfortunately I am sending it to some api and it only work with unescape – baaroz Sep 04 '12 at 09:31
  • Well, then there is nothing you can do about this. `escape` is an old legacy function with limited capabilities. Try to contact the provider of the JS-Code and ask him to fix his code. – LeJared Sep 04 '12 at 09:40