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?
A:
There's a client profile usable version, System.Net.WebUtility class, present in client profile System.dll. Here's the MSDN Link:
Eugarps
2010-10-01 15:55:12
Unfortunately there's no `UrlEncode` method there.
Darin Dimitrov
2010-10-01 15:57:40
ahh well, fail for me =( Do you review ever single stack overflow question?
Eugarps
2010-10-01 16:02:54
@Eugarps, only the ones I am interested in.
Darin Dimitrov
2010-10-01 16:04:53
+2
A:
You can use
Uri.EscapeUriString (see http://msdn.microsoft.com/en-us/library/system.uri.escapeuristring.aspx)
Matthew Manela
2010-10-01 16:03:41
You want to use EscapeUriString. The EscapeUriString will try to encode the whole url (include http:// part) while EscapeUriString understands what parts actually should be encoded
Matthew Manela
2010-10-01 16:45:03
I see, so in this instance I would probably want EscapeDataString as I may want to pass a URL as a get parameter. I am appending to a URL in this instance.
Martin Brown
2010-10-01 16:55:53
A:
Here's an example of sending a POST request that properly encodes parameters using application/x-www-form-urlencoded
content type:
using (var client = new WebClient())
{
var values = new NameValueCollection
{
{ "param1", "value1" },
{ "param2", "value2" },
};
var result = client.UploadValues("http://foo.com", values);
}
Darin Dimitrov
2010-10-01 16:11:59