Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to encode a URL in WinForms?

Tags:

I'm creating a Windows application and I need to pass an encoded URL. But I'm not sure how to encode it in WinForms C#?

like image 566
Andrew Avatar asked May 21 '11 02:05

Andrew


1 Answers

If you need to URL-encode data for a querystring, you can use either Uri.EscapeDataString or, if you don't mind referencing System.Web, HttpUtility.UrlEncode:

var rawString = @"this & that"; var uriEncoded = Uri.EscapeDataString(rawString); var httpUtilityEncoded = HttpUtility.UrlEncode(rawString); 

They're very similar but can produce subtly different results in the way special characters, like spaces, are encoded:

Console.WriteLine(uriEncoded); // uriEncoded = "this%20%26%20that"  Console.WriteLine(httpUtilityEncoded); // httpUtilityEncoded = "this+%26+that" 
like image 140
Chris Fulstow Avatar answered Oct 12 '22 05:10

Chris Fulstow