c# - 如何在 System.Net.WebUtility.UrlEncode 中强制使用 %20 而不是 +

标签 c# .net

我需要在我不想引用 System.Web 的类库程序集中对 URL 进行编码。网址包含几个空格

https://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.quote where  symbol in ("YHOO","AAPL")&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=

当我使用 System.Net.WebUtility.UrlEncode() 时,空格被替换为“+”,这不起作用。我需要将它们替换为 %20

如何在不引用 System.Web 的情况下实现这一点?

最佳答案

你可以试试 Uri.EscapeUriString来自 System 程序集,它转义了一个 URI 字符串。对于问题中的字符串,它返回:

https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quote%20where%20%20symbol%20in%20(%22YHOO%22,%22AAPL%22)&format=json&diagnostics=true&env=store%253A%252F%252Fdatatables.org%252Falltableswithkeys&callback=

关于c# - 如何在 System.Net.WebUtility.UrlEncode 中强制使用 %20 而不是 +,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32484829/

相关文章:

c# - HttpRequest 与 HttpWebRequest

C# .NET 如何在不使用 StringBuilder.ToString() 的情况下将 StringBuilder 转换为字节数组

.net - 忽略 Perforce 文件夹差异中的文件夹

C# ? : operator

c# - TextRenderer.MeasureText() 和文本框最大文本长度

c# - 禁止在 TextBox 中选择文本

.net - SQLHelper 类

c# - 如何使用静态资源字符串填充 List<string>

c# - 使用 NetworkStream 接收文件随机失败,但始终与 Thread.Sleep() 一起使用

c# - 如何在 C# 异步任务中异步等待特定条件