I am trying to write a windows client application that calls a web site for data. To keep the install to a minimum I am trying only use dlls in the .NET Framework Client Profile. 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?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Generic Generics in Managed C++
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
System.Uri.EscapeUriString()
can be problematic with certain characters, for me it was a number / pound '#' sign in the string.If that is an issue for you, try:
Here is a SO question answer that explains the difference:
What's the difference between EscapeUriString and EscapeDataString?
and recommends to use
Uri.EscapeDataString()
in any aspect.There's a client profile usable version, System.Net.WebUtility class, present in client profile System.dll. Here's the MSDN Link:
WebUtility
Here's an example of sending a POST request that properly encodes parameters using
application/x-www-form-urlencoded
content type:In .Net 4.5+ use
WebUtility
Just for formatting I'm submitting this as an answer.
Couldn't find any good examples comparing them so:
Outputs:
In .Net 4.5+ use
WebUtility
.UrlEncode
This appears to replicate
HttpUtility.UrlEncode
(pre-v4.0) for the more common characters:Uri.EscapeDataString(testString).Replace("%20", "+").Replace("'", "%27").Replace("~", "%7E")
Note:
EscapeUriString
will keep a valid uri string, which causes it to use as many plaintext characters as possible.See this answer for a Table Comparing the various Encodings:
https://stackoverflow.com/a/11236038/555798
Line Breaks All of them listed here (other than
HttpUtility.HtmlEncode
) will convert"\n\r"
into%0a%0d
or%0A%0D
Please feel free to edit this and add new characters to my test string, or leave them in the comments and I'll edit it.
You can use
Uri.EscapeUriString (see http://msdn.microsoft.com/en-us/library/system.uri.escapeuristring.aspx)