C#中如何进行URL编码和解码

avatar
作者
筋斗云
阅读量:0

在C#中进行URL编码和解码可以使用System.Web.HttpUtility类中的UrlEncode和UrlDecode方法。

using System; using System.Web;  class Program {     static void Main()     {         string originalUrl = "https://www.example.com/?query=hello world";                  // URL编码         string encodedUrl = HttpUtility.UrlEncode(originalUrl);         Console.WriteLine("Encoded URL: " + encodedUrl);                  // URL解码         string decodedUrl = HttpUtility.UrlDecode(encodedUrl);         Console.WriteLine("Decoded URL: " + decodedUrl);     } } 

在上面的代码示例中,我们首先使用UrlEncode方法对原始URL进行编码,然后使用UrlDecode方法对编码后的URL进行解码。最终输出的结果会分别是编码后的URL和解码后的URL。

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!