在C#中,您可以使用System.Net.WebUtility类中的UrlEncode方法来对URL进行编码。对于空格字符,它会被编码为+。以下是一个简单的示例:
using System;
using System.Net.WebUtility;
class Program
{
static void Main()
{
string url = "https://example.com/search?q=hello world";
string encodedUrl = WebUtility.UrlEncode(url);
Console.WriteLine("Encoded URL: " + encodedUrl);
}
}
输出结果:
Encoded URL: https://example.com/search?q=hello+world
在这个例子中,原始URL中的空格字符被编码为+。