简体   繁体   English

我可以在.NET CF上使用什么来替代HttpUtility.UrlEncode

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

I need to move .NET code to the Compact Framework. 我需要将.NET代码移动到Compact Framework。 That code uses HttpUtility.UrlEncode to encode query parameters, but System.Web isn't available on CF. 该代码使用HttpUtility.UrlEncode对查询参数进行编码,但CF上没有System.Web。 What can I use instead? 我可以用什么呢?

Use Uri.EscapeDataString . 使用Uri.EscapeDataString It's nearly equivalent, and probably better anyway, and is included in NetCF. 它几乎相当,而且可能更好,并且包含在NetCF中。

More info on their differences . 更多关于他们差异的信息

I used Uri.EscapeUriString instead since Uri.EscapeDataString encodes the full string, not just the parameters, for example 我使用了Uri.EscapeUriString因为Uri.EscapeDataString对完整的字符串进行编码,而不仅仅是参数,例如

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

With EscapeDataString it gets transformed to: 使用EscapeDataString,它将转换为:

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

And with Uri.EscapeUriString, it correctly turns into this: 使用Uri.EscapeUriString,它正确地变为:

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

You can look at bundling mono's implementation (optionally simplified). 您可以查看捆绑mono的实现 (可选择简化)。 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. 使用反射器,您将很快发现UrlEncode有许多重载方法,调用只需要字符串参数最终将字符串转换为字节数组并调用内部方法的版本。

Edit: Code snippet removed in case of copyright violation... 修改:在版权违规的情况下删除了代码段...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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