简体   繁体   中英

What can I use on .NET CF as a replacement for HttpUtility.UrlEncode

I need to move .NET code to the Compact Framework. That code uses HttpUtility.UrlEncode to encode query parameters, but System.Web isn't available on CF. What can I use instead?

Use Uri.EscapeDataString . It's nearly equivalent, and probably better anyway, and is included in NetCF.

More info on their differences .

I used Uri.EscapeUriString instead since Uri.EscapeDataString encodes the full string, not just the parameters, for example

http://website.com/a/abc{d}d 

With EscapeDataString it gets transformed to:

http%3A%2F%2Fwebsite.com%2Fa%2Fabc%7Bd%7Dd

And with Uri.EscapeUriString, it correctly turns into this:

http://website.com/a/abc%7Bd%7Dd

You can look at bundling mono's implementation (optionally simplified). I don't know how many of the dependencies you'd have to bring in, though. It may or may not be feasible.

You probably will have to roll your own, or even better, cheat. Using reflector you will quickly see that there are a number of overloaded methods for UrlEncode, calling the version that just expects the string paramater eventually ends up converting the string to an array of bytes and calling an internal method.

Edit: Code snippet removed in case of copyright violation...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM