URL Encoding using C#

前端 未结 13 1970
北荒
北荒 2020-11-22 08:05

I have an application which sends a POST request to the VB forum software and logs someone in (without setting cookies or anything).

Once the user is logged in I cre

13条回答
  •  灰色年华
    2020-11-22 08:55

    Since .NET Framework 4.5 and .NET Standard 1.0 you should use WebUtility.UrlEncode. Advantages over alternatives:

    1. It is part of .NET Framework 4.5+, .NET Core 1.0+, .NET Standard 1.0+, UWP 10.0+ and all Xamarin platforms as well. HttpUtility, while being available in .NET Framework earlier (.NET Framework 1.1+), becomes available on other platforms much later (.NET Core 2.0+, .NET Standard 2.0+) and it still unavailable in UWP (see related question).

    2. In .NET Framework, it resides in System.dll, so it does not require any additional references, unlike HttpUtility.

    3. It properly escapes characters for URLs, unlike Uri.EscapeUriString (see comments to drweb86's answer).

    4. It does not have any limits on the length of the string, unlike Uri.EscapeDataString (see related question), so it can be used for POST requests, for example.

提交回复
热议问题