本文翻译自:How do you UrlEncode without using System.Web?
I am trying to write a windows client application that calls a web site for data. 我正在尝试编写一个调用网站数据的Windows客户端应用程序。 To keep the install to a minimum I am trying only use dlls in the .NET Framework Client Profile . 为了使安装保持最小,我尝试仅在.NET Framework客户端配置文件中使用dll 。 Trouble is that I need to UrlEncode some parameters, is there an easy way to do this without importing System.Web.dll which is not part of the Client Pofile? 麻烦的是我需要UrlEncode一些参数,有没有一种简单的方法可以在不导入System.Web.dll的情况下执行此操作,而System.Web.dll不是Client Pofile的一部分?
#1楼
参考:https://stackoom.com/question/G79m/如何在不使用System-Web的情况下进行UrlEncode
#2楼
System.Net.WebUtility.HtmlDecode
#3楼
The answers here are very good, but still insufficient for me. 这里的答案非常好,但对我来说仍然不够。
I wrote a small loop that compares Uri.EscapeUriString
with Uri.EscapeDataString
for all characters from 0 to 255. 我写了一个小循环,将Uri.EscapeUriString
与Uri.EscapeDataString
进行比较,以Uri.EscapeDataString
0到255之间的所有字符。
NOTE: Both functions have the built-in intelligence that characters above 0x80 are first UTF-8 encoded and then percent encoded. 注意:两个函数都具有内置智能,即0x80以上的字符首先是UTF-8编码,然后是百分比编码。
Here is the result: 结果如下:
******* Different *******
'#' -> Uri "#" Data "%23"
'$' -> Uri "$" Data "%24"
'&' -> Uri "&" Data "%26"
'+' -> Uri "+" Data "%2B"
',' -> Uri "," Data "%2C"
'/' -> Uri "/" Data "%2F"
':' -> Uri ":" Data "%3A"
';' -> Uri ";" Data "%3B"
'=' -> Uri "=" Data "%3D"
'?' -> Uri "?" Data "%3F"
'@' -> Uri "@" Data "%40"
******* Not escaped *******
'!' -> Uri "!" Data "!"
''' -> Uri "'" Data "'"
'(' -> Uri "(" Data "("
')' -> Uri ")" Data ")"
'*' -> Uri "*" Data "*"
'-' -> Uri "-" Data "-"
'.' -> Uri "." Data "."
'_' -> Uri "_" Data "_"
'~' -> Uri "~" Data "~"
'0' -> Uri "0" Data "0"
.....
'9' -> Uri "9" Data "9"
'A' -> Uri "A" Data "A"
......
'Z' -> Uri "Z" Data "Z"
'a' -> Uri "a" Data "a"
.....
'z' -> Uri "z" Data "z"
******* UTF 8 *******
.....
'Ò' -> Uri "%C3%92" Data "%C3%92"
'Ó' -> Uri "%C3%93" Data "%C3%93"
'Ô' -> Uri "%C3%94" Data "%C3%94"
'Õ' -> Uri "%C3%95" Data "%C3%95"
'Ö' -> Uri "%C3%96" Data "%C3%96"
.....
EscapeUriString
is to be used to encode URLs, while EscapeDataString
is to be used to encode for example the content of a Cookie, because Cookie data must not contain the reserved characters '='
and ';'
EscapeUriString
用于编码URL,而EscapeDataString
用于编码例如Cookie的内容,因为Cookie数据不得包含保留字符'='
和';'
. 。
#4楼
There's a client profile usable version, System.Net.WebUtility class, present in client profile System.dll. 有一个客户端配置文件可用版本,System.Net.WebUtility类,存在于客户端配置文件System.dll中。 Here's the MSDN Link: 这是MSDN链接:
#5楼
You can use 您可以使用
Uri.EscapeUriString (see http://msdn.microsoft.com/en-us/library/system.uri.escapeuristring.aspx ) Uri.EscapeUriString(参见http://msdn.microsoft.com/en-us/library/system.uri.escapeuristring.aspx )
#6楼
Here's an example of sending a POST request that properly encodes parameters using application/x-www-form-urlencoded
content type: 以下是使用application/x-www-form-urlencoded
内容类型发送正确编码参数的POST请求的示例:
using (var client = new WebClient())
{
var values = new NameValueCollection
{
{ "param1", "value1" },
{ "param2", "value2" },
};
var result = client.UploadValues("http://foo.com", values);
}