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

时间:2021-11-05 03:46:33

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?

我需要将.NET代码移动到Compact Framework。该代码使用HttpUtility.UrlEncode对查询参数进行编码,但CF上没有System.Web。我可以用什么呢?

4 个解决方案

#1


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

使用Uri.EscapeDataString。它几乎相当,而且可能更好,并且包含在NetCF中。

More info on their differences.

更多关于他们差异的信息。

#2


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.

您可以查看捆绑mono的实现(可选择简化)。不过,我不知道有多少依赖项需要引入。它可能是也可能不可行。

#3


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...

修改:在版权违规的情况下删除了代码段...

#4


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

#1


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

使用Uri.EscapeDataString。它几乎相当,而且可能更好,并且包含在NetCF中。

More info on their differences.

更多关于他们差异的信息。

#2


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.

您可以查看捆绑mono的实现(可选择简化)。不过,我不知道有多少依赖项需要引入。它可能是也可能不可行。

#3


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...

修改:在版权违规的情况下删除了代码段...

#4


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