如何編寫(xiě)C#函數(shù)實(shí)現(xiàn)URL編碼

c#
小樊
85
2024-08-08 05:00:49

你可以使用C#中的HttpUtility類(lèi)來(lái)實(shí)現(xiàn)URL編碼。下面是一個(gè)簡(jiǎn)單的示例代碼:

using System;
using System.Web;

class Program
{
    static void Main()
    {
        string url = "https://www.example.com/?q=編碼測(cè)試";
        
        string encodedUrl = HttpUtility.UrlEncode(url);
        
        Console.WriteLine("Encoded URL: " + encodedUrl);
    }
}

這段代碼將會(huì)輸出編碼后的URL:https%3A%2F%2Fwww.example.com%2F%3Fq%3D%e7%bc%96%e7%a0%81%e6%b5%8b%e8%af%95。

0