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

   2024-10-02 8090
核心提示:在C#中进行URL编码和解码可以使用System.Web.HttpUtility类中的UrlEncode和UrlDecode方法。using System;using System.Web;class

在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。

 
举报打赏
 
更多>同类物流大全
推荐图文
推荐物流大全
点击排行

网站首页  |  关于我们  |  联系方式 | 网站留言    |  赣ICP备2021007278号