gpt4 book ai didi

c# - 如何在 WinForms 中对 URL 进行编码?

转载 作者:IT王子 更新时间:2023-10-29 04:37:11 24 4
gpt4 key购买 nike

我正在创建一个 Windows 应用程序,我需要传递一个编码的 URL。但我不确定如何在 WinForms C# 中对其进行编码?

最佳答案

如果您需要对查询字符串的数据进行 URL 编码,您可以使用 Uri.EscapeDataString或者,如果您不介意引用 System.Web,HttpUtility.UrlEncode :

var rawString = @"this & that";
var uriEncoded = Uri.EscapeDataString(rawString);
var httpUtilityEncoded = HttpUtility.UrlEncode(rawString);

它们非常相似,但在特殊字符(如空格)的编码方式上会产生细微不同的结果:

Console.WriteLine(uriEncoded);
// uriEncoded = "this%20%26%20that"

Console.WriteLine(httpUtilityEncoded);
// httpUtilityEncoded = "this+%26+that"

关于c# - 如何在 WinForms 中对 URL 进行编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6079290/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com