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();
6
  • 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. Commented Sep 4, 2012 at 9:14
  • look at this: stackoverflow.com/questions/3778165/… Commented Sep 4, 2012 at 9:24
  • I edit mt question and added code sample Commented Sep 4, 2012 at 9: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. Commented Sep 4, 2012 at 9: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 Commented Sep 4, 2012 at 9:43

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I know that decodeURIComponent will work,unfortunately I am sending it to some api and it only work with unescape
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.