如何在不使用System.Web的情况下进行UrlEncode?

本文翻译自: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.EscapeUriStringUri.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链接:

WebUtility WebUtility


#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);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值