{"id":1215,"date":"2025-01-25T23:08:27","date_gmt":"2025-01-25T15:08:27","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1215"},"modified":"2025-01-25T23:08:28","modified_gmt":"2025-01-25T15:08:28","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-c-%e4%b8%ad%e9%ab%98%e6%95%88%e5%9c%b0%e5%af%b9%e4%b8%8d%e5%90%8c%e5%ad%97%e7%ac%a6%e9%9b%86%e8%bf%9b%e8%a1%8c-url-%e7%bc%96%e7%a0%81%ef%bc%9a%e5%ae%8c%e6%95%b4%e6%8c%87","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/25\/%e5%a6%82%e4%bd%95%e5%9c%a8-c-%e4%b8%ad%e9%ab%98%e6%95%88%e5%9c%b0%e5%af%b9%e4%b8%8d%e5%90%8c%e5%ad%97%e7%ac%a6%e9%9b%86%e8%bf%9b%e8%a1%8c-url-%e7%bc%96%e7%a0%81%ef%bc%9a%e5%ae%8c%e6%95%b4%e6%8c%87\/","title":{"rendered":"\u5982\u4f55\u5728 C# \u4e2d\u9ad8\u6548\u5730\u5bf9\u4e0d\u540c\u5b57\u7b26\u96c6\u8fdb\u884c URL \u7f16\u7801\uff1a\u5b8c\u6574\u6307\u5357"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728 C# \u4e2d\u5bf9\u4e0d\u540c\u5b57\u7b26\u96c6\u8fdb\u884c URL \u7f16\u7801\u662f Web \u5f00\u53d1\u4e2d\u5e38\u89c1\u7684\u9700\u6c42\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u591a\u8bed\u8a00\u652f\u6301\u3001\u7528\u6237\u8f93\u5165\u3001\u7279\u6b8a\u5b57\u7b26\u6216\u975e ASCII \u5b57\u7b26\u65f6\u3002C# \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u9ad8\u6548\u7f16\u7801 URL\uff0c\u4f46\u9488\u5bf9\u4e0d\u540c\u5b57\u7b26\u96c6\u7684\u9700\u6c42\uff08\u5982 UTF-8\u3001ASCII \u7b49\uff09\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u7f16\u7801\u8fc7\u7a0b\u80fd\u591f\u6b63\u786e\u5904\u7406\u5404\u79cd\u5b57\u7b26\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528 C# \u9ad8\u6548\u5730\u8fdb\u884c URL \u7f16\u7801\u7684\u8be6\u7ec6\u65b9\u6cd5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u4f7f\u7528 <code>Uri.EscapeDataString<\/code> \u65b9\u6cd5<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Uri.EscapeDataString<\/code> \u662f C# \u4e2d\u6700\u5e38\u7528\u7684 URL \u7f16\u7801\u65b9\u6cd5\uff0c\u80fd\u591f\u5904\u7406\u5b57\u7b26\u4e32\u4e2d\u7684\u975e ASCII \u5b57\u7b26\u5e76\u8fdb\u884c\u7f16\u7801\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\u3002\u5b83\u5bf9 UTF-8 \u7f16\u7801\u7684\u5b57\u7b26\u96c6\u975e\u5e38\u53cb\u597d\uff0c\u80fd\u591f\u786e\u4fdd\u6240\u6709\u5b57\u7b26\u90fd\u88ab\u6b63\u786e\u5730\u8f6c\u4e49\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\n\nclass Program\n{\n    static void Main()\n    {\n        string text = \"\u4f60\u597d\uff0c\u4e16\u754c!\";  \/\/ \u5305\u542b\u975e ASCII \u5b57\u7b26\u7684\u5b57\u7b26\u4e32\n        string encodedText = Uri.EscapeDataString(text);  \/\/ \u4f7f\u7528 URI \u7f16\u7801\n        Console.WriteLine(encodedText);  \/\/ \u8f93\u51fa\uff1a%E4%BD%A0%E5%A5%BD%EF%BC%8C%E4%B8%96%E7%95%8C%21\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u91ca\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Uri.EscapeDataString<\/code> \u4f1a\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u975e ASCII \u5b57\u7b26\uff08\u5982\u4e2d\u6587\u3001\u7b26\u53f7\u7b49\uff09\u7f16\u7801\u4e3a URL \u5b89\u5168\u683c\u5f0f\u3002<\/li>\n\n\n\n<li>\u5b83\u5c06\u6bcf\u4e2a\u5b57\u7b26\u8f6c\u6362\u4e3a <code>%<\/code> \u540e\u8ddf\u968f\u5b57\u7b26\u7684 UTF-8 \u5341\u516d\u8fdb\u5236\u8868\u793a\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f18\u70b9\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9002\u7528\u4e8e\u5927\u591a\u6570\u9700\u8981\u7f16\u7801\u7684\u573a\u666f\u3002<\/li>\n\n\n\n<li>\u652f\u6301 UTF-8 \u5b57\u7b26\u96c6\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u4f7f\u7528 <code>HttpUtility.UrlEncode<\/code> \u65b9\u6cd5<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>HttpUtility.UrlEncode<\/code> \u662f\u53e6\u4e00\u4e2a\u5e38\u7528\u7684 URL \u7f16\u7801\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u5904\u7406\u6574\u4e2a URL \u6216\u8005 URL \u7684\u67e5\u8be2\u5b57\u7b26\u4e32\u90e8\u5206\u3002\u4e0e <code>Uri.EscapeDataString<\/code> \u7c7b\u4f3c\uff0c\u5b83\u4f1a\u5c06\u975e ASCII \u5b57\u7b26\u8f6c\u6362\u4e3a URL \u7f16\u7801\u683c\u5f0f\uff0c\u5e76\u9002\u5e94\u4e0d\u540c\u5b57\u7b26\u96c6\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Web;\n\nclass Program\n{\n    static void Main()\n    {\n        string text = \"\u4f60\u597d\uff0c\u4e16\u754c!\";  \/\/ \u5305\u542b\u975e ASCII \u5b57\u7b26\u7684\u5b57\u7b26\u4e32\n        string encodedText = HttpUtility.UrlEncode(text);  \/\/ \u4f7f\u7528 HttpUtility \u7f16\u7801\n        Console.WriteLine(encodedText);  \/\/ \u8f93\u51fa\uff1a%E4%BD%A0%E5%A5%BD%EF%BC%8C%E4%B8%96%E7%95%8C%21\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u91ca\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>HttpUtility.UrlEncode<\/code> \u7c7b\u4f3c\u4e8e <code>Uri.EscapeDataString<\/code>\uff0c\u4f46\u662f\u5b83\u5904\u7406\u67e5\u8be2\u5b57\u7b26\u4e32\u65f6\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\u3002<\/li>\n\n\n\n<li><code>HttpUtility<\/code> \u5c5e\u4e8e <code>System.Web<\/code> \u547d\u540d\u7a7a\u95f4\uff0c\u901a\u5e38\u7528\u4e8e ASP.NET \u9879\u76ee\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f18\u70b9\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u652f\u6301\u5b8c\u6574\u7684 URL \u7f16\u7801\uff0c\u7279\u522b\u9002\u7528\u4e8e Web \u5e94\u7528\u5f00\u53d1\u3002<\/li>\n\n\n\n<li>\u80fd\u591f\u6b63\u786e\u5904\u7406\u591a\u8bed\u8a00\u5b57\u7b26\u548c\u7279\u6b8a\u7b26\u53f7\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u4f7f\u7528 <code>WebUtility.UrlEncode<\/code> \u65b9\u6cd5\uff08\u9002\u7528\u4e8e .NET Core\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 .NET Core \u6216 .NET 5+ \u4e2d\uff0c<code>WebUtility.UrlEncode<\/code> \u65b9\u6cd5\u662f\u6700\u5e38\u7528\u7684 URL \u7f16\u7801\u65b9\u6cd5\uff0c\u529f\u80fd\u4e0e <code>HttpUtility.UrlEncode<\/code> \u7c7b\u4f3c\uff0c\u4f46\u4e0d\u9700\u8981\u5f15\u7528 <code>System.Web<\/code> \u547d\u540d\u7a7a\u95f4\u3002\u5b83\u53ef\u4ee5\u5904\u7406 ASCII \u548c UTF-8 \u5b57\u7b26\u96c6\uff0c\u786e\u4fdd URL \u7f16\u7801\u7684\u4e00\u81f4\u6027\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Net;\n\nclass Program\n{\n    static void Main()\n    {\n        string text = \"\u4f60\u597d\uff0c\u4e16\u754c!\";  \/\/ \u5305\u542b\u975e ASCII \u5b57\u7b26\u7684\u5b57\u7b26\u4e32\n        string encodedText = WebUtility.UrlEncode(text);  \/\/ \u4f7f\u7528 WebUtility \u7f16\u7801\n        Console.WriteLine(encodedText);  \/\/ \u8f93\u51fa\uff1a%E4%BD%A0%E5%A5%BD%EF%BC%8C%E4%B8%96%E7%95%8C%21\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u91ca\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>WebUtility.UrlEncode<\/code> \u65b9\u6cd5\u9002\u7528\u4e8e\u73b0\u4ee3 .NET \u5e94\u7528\uff08\u5982 .NET Core\u3001.NET 5+\uff09\u3002<\/li>\n\n\n\n<li>\u5b83\u786e\u4fdd\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5b57\u7b26\uff08\u5305\u62ec\u975e ASCII \u5b57\u7b26\uff09\u90fd\u80fd\u6b63\u786e\u8f6c\u4e49\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f18\u70b9\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9002\u7528\u4e8e .NET Core \u548c\u73b0\u4ee3 .NET \u5e94\u7528\u3002<\/li>\n\n\n\n<li>\u4e0d\u4f9d\u8d56\u4e8e <code>System.Web<\/code> \u547d\u540d\u7a7a\u95f4\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u81ea\u5b9a\u4e49 URL \u7f16\u7801\u65b9\u6cd5\uff08\u9ad8\u7ea7\u9700\u6c42\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u67d0\u4e9b\u9ad8\u7ea7\u9700\u6c42\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u5bf9 URL \u7f16\u7801\u8fc7\u7a0b\u8fdb\u884c\u81ea\u5b9a\u4e49\uff0c\u7279\u522b\u662f\u5bf9\u4e8e\u7279\u5b9a\u5b57\u7b26\u96c6\uff08\u5982 ISO-8859-1\u3001GB2312 \u7b49\uff09\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>Encoding.GetEncoding<\/code> \u6765\u5904\u7406\u4e0d\u540c\u5b57\u7b26\u96c6\u7684\u7f16\u7801\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Text;\n\nclass Program\n{\n    static string CustomUrlEncode(string text, string charset)\n    {\n        Encoding encoding = Encoding.GetEncoding(charset);  \/\/ \u4f7f\u7528\u6307\u5b9a\u5b57\u7b26\u96c6\n        byte&#91;] bytes = encoding.GetBytes(text);  \/\/ \u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u7ec4\n        StringBuilder encodedText = new StringBuilder();\n\n        foreach (byte b in bytes)\n        {\n            encodedText.AppendFormat(\"%{0:X2}\", b);  \/\/ \u5c06\u5b57\u8282\u8f6c\u6362\u4e3a URL \u7f16\u7801\u683c\u5f0f\n        }\n\n        return encodedText.ToString();\n    }\n\n    static void Main()\n    {\n        string text = \"\u4f60\u597d\uff0c\u4e16\u754c!\";\n        string encodedText = CustomUrlEncode(text, \"GB2312\");  \/\/ \u4f7f\u7528 GB2312 \u5b57\u7b26\u96c6\n        Console.WriteLine(encodedText);  \/\/ \u8f93\u51fa\uff1a%C4%EA%BA%BA%2C%CD%F8%B1%BE\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u91ca\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Encoding.GetEncoding<\/code> \u65b9\u6cd5\u5141\u8bb8\u4f60\u6307\u5b9a\u4e0d\u540c\u7684\u5b57\u7b26\u96c6\uff0c\u5982 <code>GB2312<\/code>\u3001<code>ISO-8859-1<\/code> \u7b49\uff0c\u5904\u7406\u5b57\u7b26\u7f16\u7801\u3002<\/li>\n\n\n\n<li>\u5b57\u7b26\u4e32\u901a\u8fc7\u6307\u5b9a\u5b57\u7b26\u96c6\u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u7ec4\uff0c\u518d\u8f6c\u4e3a URL \u7f16\u7801\u683c\u5f0f\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f18\u70b9\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u63d0\u4f9b\u4e86\u5bf9\u4e0d\u540c\u5b57\u7b26\u96c6\u7684\u9ad8\u5ea6\u7075\u6d3b\u6027\u3002<\/li>\n\n\n\n<li>\u53ef\u4ee5\u6ee1\u8db3\u7279\u5b9a\u9700\u6c42\uff0c\u5982\u652f\u6301\u591a\u8bed\u8a00\u73af\u5883\u6216\u4e0d\u540c\u7684\u5b57\u7b26\u7f16\u7801\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">5. \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6e05\u7406\u5b57\u7b26\uff08\u53ef\u9009\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u53ea\u9700\u8981\u5bf9 URL \u4e2d\u7684\u975e\u6cd5\u5b57\u7b26\u8fdb\u884c\u6e05\u7406\uff08\u5982\u7a7a\u683c\u3001\u6807\u70b9\u7b26\u53f7\u7b49\uff09\uff0c\u53ef\u4ee5\u7ed3\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u8fc7\u6ee4\u6216\u66ff\u6362\u5b57\u7b26\uff0c\u7136\u540e\u8fdb\u884c URL \u7f16\u7801\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Text.RegularExpressions;\n\nclass Program\n{\n    static string CleanAndEncode(string text)\n    {\n        \/\/ \u6e05\u7406\u7a7a\u683c\u548c\u7279\u6b8a\u7b26\u53f7\n        string cleanedText = Regex.Replace(text, @\"&#91;^a-zA-Z0-9\\-_.~]\", \"_\");\n        return Uri.EscapeDataString(cleanedText);\n    }\n\n    static void Main()\n    {\n        string text = \"\u4f60\u597d\uff0c \u4e16\u754c!\";\n        string encodedText = CleanAndEncode(text);\n        Console.WriteLine(encodedText);  \/\/ \u8f93\u51fa\uff1a%E4%BD%A0%E5%A5%BD%EF%BC%8C_%E4%B8%96%E7%95%8C%21\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u91ca\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f <code>[^a-zA-Z0-9\\-_.~]<\/code> \u6765\u5339\u914d\u5e76\u66ff\u6362\u4e0d\u5141\u8bb8\u7684\u5b57\u7b26\u3002<\/li>\n\n\n\n<li>\u7136\u540e\u4f7f\u7528 <code>Uri.EscapeDataString<\/code> \u5bf9\u5df2\u6e05\u7406\u7684\u5b57\u7b26\u4e32\u8fdb\u884c URL \u7f16\u7801\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u6bd4\u8f83\u548c\u9009\u62e9\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>Uri.EscapeDataString<\/code><\/strong>\uff1a\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\uff0c\u7279\u522b\u662f\u5904\u7406 UTF-8 \u7f16\u7801\u7684\u5b57\u7b26\u96c6\u3002<\/li>\n\n\n\n<li><strong><code>HttpUtility.UrlEncode<\/code> \u548c <code>WebUtility.UrlEncode<\/code><\/strong>\uff1a\u9002\u5408 ASP.NET \u548c .NET Core \u73af\u5883\uff0c\u80fd\u66f4\u5168\u9762\u5904\u7406 URL \u7f16\u7801\u3002<\/li>\n\n\n\n<li><strong>\u81ea\u5b9a\u4e49\u7f16\u7801\u65b9\u6cd5<\/strong>\uff1a\u9002\u5408\u9700\u8981\u5904\u7406\u7279\u5b9a\u5b57\u7b26\u96c6\u7684\u573a\u666f\uff0c\u5982 GB2312 \u6216 ISO-8859-1 \u7f16\u7801\u3002<\/li>\n\n\n\n<li><strong>\u6b63\u5219\u8868\u8fbe\u5f0f\u6e05\u7406<\/strong>\uff1a\u9002\u7528\u4e8e\u5904\u7406\u975e\u6cd5\u5b57\u7b26\u6216\u7279\u6b8a\u5b57\u7b26\u7684\u60c5\u51b5\uff0c\u786e\u4fdd URL \u7684\u5b89\u5168\u6027\u548c\u6709\u6548\u6027\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 C# \u4e2d\u5bf9\u4e0d\u540c\u5b57\u7b26\u96c6\u8fdb\u884c URL \u7f16\u7801\u662f Web \u5f00\u53d1\u4e2d\u5e38\u89c1\u7684\u9700\u6c42\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u591a\u8bed\u8a00\u652f\u6301\u3001\u7528\u6237\u8f93\u5165\u3001\u7279\u6b8a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[],"class_list":["post-1215","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1215","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=1215"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1215\/revisions"}],"predecessor-version":[{"id":1216,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1215\/revisions\/1216"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}