我试图写一个windows客户端应用程序,调用一个网站的数据。为了使安装最小化,我尝试只使用。net框架客户端配置文件中的dll。麻烦的是,我需要UrlEncode一些参数,有没有一个简单的方法来做到这一点,而不导入System.Web.dll,这不是客户端Pofile的一部分?
当前回答
你可以使用
Uri。EscapeUriString(参见http://msdn.microsoft.com/en-us/library/system.uri.escapeuristring.aspx)
其他回答
你可以使用
Uri。EscapeUriString(参见http://msdn.microsoft.com/en-us/library/system.uri.escapeuristring.aspx)
这里的答案很好,但对我来说仍然不够。
我写了一个比较Uri的小循环。带Uri的EscapeUriString。为0到255的所有字符提供EscapeDataString。
注意:这两个函数都具有内置智能,即0x80以上的字符首先被UTF-8编码,然后被百分比编码。
结果如下:
******* 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用于编码url,而EscapeDataString用于编码例如Cookie的内容,因为Cookie数据不能包含保留字符'='和';'。
UrlEncode不使用系统。网络:
String s = System.Net.WebUtility.UrlEncode(str);
//fix some different between WebUtility.UrlEncode and HttpUtility.UrlEncode
s = Regex.Replace(s, "(%[0-9A-F]{2})", c => c.Value.ToLowerInvariant());
更多的细节: https://www.samnoble.co.uk/2014/05/21/beware-webutility-urlencode-vs-httputility-urlencode/
System.Net.WebUtility.HtmlDecode
我已经被迫在我构建的一些项目中使用。net 4.0,由于这个原因,WebUtility和HttpUtility都不包含这些。我使用了Uri.EscapeDataString()方法,它工作得非常好,但我不喜欢它没有编码所有标准特殊字符的事实(意思是!"#$%&'()*+,-./:;<=>?@[\]^_ ' {|}~)我也更多地使用Visual Basic而不是c#,所以我不确定要转换下面的内容需要什么,但它非常适合我的基本需求。
我不会处理任何UTF-8格式的字符串,因为它只用于一些非常基本的文本操作,到目前为止对我来说已经很好了。它不会以任何方式解析换行符(我正在操作的文本不会有换行符),您必须首先处理%符号,以防止它破坏其余符号的编码。有点花哨,但很管用。
Function EncodeURL(ByVal DecodedString As String) As String
DecodedString = Replace(DecodedString, "%", "%25", 1, vbTextCompare)
DecodedString = Replace(DecodedString, " ", "%20", 1, vbTextCompare)
DecodedString = Replace(DecodedString, "!", "%21", 1, vbTextCompare)
DecodedString = Replace(DecodedString, """", "%22", 1, vbTextCompare)
DecodedString = Replace(DecodedString, "#", "%23", 1, vbTextCompare)
DecodedString = Replace(DecodedString, "$", "%24", 1, vbTextCompare)
DecodedString = Replace(DecodedString, "&", "%26", 1, vbTextCompare)
DecodedString = Replace(DecodedString, "'", "%27", 1, vbTextCompare)
DecodedString = Replace(DecodedString, "(", "%28", 1, vbTextCompare)
DecodedString = Replace(DecodedString, ")", "%29", 1, vbTextCompare)
DecodedString = Replace(DecodedString, "*", "%2A", 1, vbTextCompare)
DecodedString = Replace(DecodedString, "+", "%2B", 1, vbTextCompare)
DecodedString = Replace(DecodedString, ",", "%2C", 1, vbTextCompare)
DecodedString = Replace(DecodedString, "-", "%2D", 1, vbTextCompare)
DecodedString = Replace(DecodedString, ".", "%2E", 1, vbTextCompare)
DecodedString = Replace(DecodedString, "/", "%2F", 1, vbTextCompare)
DecodedString = Replace(DecodedString, ":", "%3A", 1, vbTextCompare)
DecodedString = Replace(DecodedString, ";", "%3B", 1, vbTextCompare)
DecodedString = Replace(DecodedString, "<", "%3C", 1, vbTextCompare)
DecodedString = Replace(DecodedString, "=", "%3D", 1, vbTextCompare)
DecodedString = Replace(DecodedString, ">", "%3E", 1, vbTextCompare)
DecodedString = Replace(DecodedString, "?", "%3F", 1, vbTextCompare)
DecodedString = Replace(DecodedString, "@", "%40", 1, vbTextCompare)
DecodedString = Replace(DecodedString, "[", "%5B", 1, vbTextCompare)
DecodedString = Replace(DecodedString, "\", "%5C", 1, vbTextCompare)
DecodedString = Replace(DecodedString, "]", "%5D", 1, vbTextCompare)
DecodedString = Replace(DecodedString, "^", "%5E", 1, vbTextCompare)
DecodedString = Replace(DecodedString, "_", "%5F", 1, vbTextCompare)
DecodedString = Replace(DecodedString, "`", "%60", 1, vbTextCompare)
DecodedString = Replace(DecodedString, "{", "%7B", 1, vbTextCompare)
DecodedString = Replace(DecodedString, "|", "%7C", 1, vbTextCompare)
DecodedString = Replace(DecodedString, "}", "%7D", 1, vbTextCompare)
DecodedString = Replace(DecodedString, "~", "%7E", 1, vbTextCompare)
EncodeURL = DecodedString
End Function
输入:
! "#$%&'()*+,-./:;<=>?@[]^_`{|}~
输出
%21%20%22%23%24%25%26%27%28%29%2A%2B%2C%2D%2E%2F%3A%3B%3C%3D%3E%3F%40%5B%5C%5D%5E%5F%60%7B%7C%7D%7E