开发者

How to encode a URL in WinForms?

I'm creating a Windows application and开发者_如何学Go I need to pass an encoded URL. But I'm not sure how to encode it in WinForms C#?


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"


Try Uri.EscapeUriString()


Did you tried with:

var url = System.Net.WebUtility.UrlEncode(string);

You don't need a dependency on System.Web and you can use it in PCL, I used it in my Xamarin forms project.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜