我试图写一个windows客户端应用程序,调用一个网站的数据。为了使安装最小化,我尝试只使用。net框架客户端配置文件中的dll。麻烦的是,我需要UrlEncode一些参数,有没有一个简单的方法来做到这一点,而不导入System.Web.dll,这不是客户端Pofile的一部分?
当前回答
我已经被迫在我构建的一些项目中使用。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
其他回答
在.Net 4.5+中使用WebUtility
只是为了格式,我把这个作为答案提交。
我找不到任何比较它们的例子,所以:
string testString = "http://test# space 123/text?var=val&another=two";
Console.WriteLine("UrlEncode: " + System.Web.HttpUtility.UrlEncode(testString));
Console.WriteLine("EscapeUriString: " + Uri.EscapeUriString(testString));
Console.WriteLine("EscapeDataString: " + Uri.EscapeDataString(testString));
Console.WriteLine("EscapeDataReplace: " + Uri.EscapeDataString(testString).Replace("%20", "+"));
Console.WriteLine("HtmlEncode: " + System.Web.HttpUtility.HtmlEncode(testString));
Console.WriteLine("UrlPathEncode: " + System.Web.HttpUtility.UrlPathEncode(testString));
//.Net 4.0+
Console.WriteLine("WebUtility.HtmlEncode: " + WebUtility.HtmlEncode(testString));
//.Net 4.5+
Console.WriteLine("WebUtility.UrlEncode: " + WebUtility.UrlEncode(testString));
输出:
UrlEncode: http%3a%2f%2ftest%23+space+123%2ftext%3fvar%3dval%26another%3dtwo
EscapeUriString: http://test#%20space%20123/text?var=val&another=two
EscapeDataString: http%3A%2F%2Ftest%23%20space%20123%2Ftext%3Fvar%3Dval%26another%3Dtwo
EscapeDataReplace: http%3A%2F%2Ftest%23+space+123%2Ftext%3Fvar%3Dval%26another%3Dtwo
HtmlEncode: http://test# space 123/text?var=val&another=two
UrlPathEncode: http://test#%20space%20123/text?var=val&another=two
//.Net 4.0+
WebUtility.HtmlEncode: http://test# space 123/text?var=val&another=two
//.Net 4.5+
WebUtility.UrlEncode: http%3A%2F%2Ftest%23+space+123%2Ftext%3Fvar%3Dval%26another%3Dtwo
在. net 4.5+中使用WebUtility。UrlEncode
这似乎复制了HttpUtility。UrlEncode (v4.0之前)用于更常见的字符: Uri.EscapeDataString (testString)。替换(“% 20”,“+”)。替换(“”、“”)。替换(“~”,“% 7 e”) 注意:EscapeUriString将保留一个有效的uri字符串,这将导致它使用尽可能多的明文字符。
请参阅比较各种编码的表格的答案: https://stackoverflow.com/a/11236038/555798
换行符 这里列出的所有代码(HttpUtility.HtmlEncode除外)都将“\n\r”转换为%0a%0d或%0a%0d
请随意编辑,并向我的测试字符串中添加新字符,或将它们留在评论中,我会编辑它。
System.Uri.EscapeUriString()对于某些字符可能会有问题,对我来说,它是字符串中的数字/磅“#”符号。
如果这对你来说是个问题,试试:
System.Uri.EscapeDataString() //Works excellent with individual values
下面是一个SO问题的答案,解释了两者的区别:
EscapeUriString和EscapeDataString的区别是什么?
并建议在任何方面使用Uri.EscapeDataString()。
你可以使用
Uri。EscapeUriString(参见http://msdn.microsoft.com/en-us/library/system.uri.escapeuristring.aspx)
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类,在客户端配置文件System.dll中。这是MSDN链接:
WebUtility
推荐文章
- Linq-to-Entities Join vs GroupJoin
- 为什么字符串类型的默认值是null而不是空字符串?
- 在list中获取不同值的列表
- 组合框:向项目添加文本和值(无绑定源)
- AutoMapper:“忽略剩下的?”
- 如何为ASP.net/C#应用程序配置文件值中的值添加&号
- 从System.Drawing.Bitmap中加载WPF BitmapImage
- 如何找出一个文件存在于c# / .NET?
- 为什么更快地检查字典是否包含键,而不是捕捉异常,以防它不?
- [DataContract]的命名空间
- string. isnullorempty (string) vs. string. isnullowhitespace (string)
- 完全外部连接
- URLEncoder不能翻译空格字符
- 如何使用。net 4运行时运行PowerShell ?
- 在foreach循环中编辑字典值