C#使用HttpUtility,HttpServerUtility、HttpUtility对URL编码、解码
1、HttpUtility.UrlEncode 方法:
对 URL 字符串进行编码,以便实现从 Web 服务器到客户端的可靠的 HTTP 传输。重载列表:
[1]将字节数组转换为已编码的 URL 字符。 [C#] public static string UrlEncode(byte[]);
[2]对 URL 字符串进行编码。 [C#] public static string UrlEncode(string);
[3]使用指定的编码对象对 URL 字符串进行编码。 [C#] public static string UrlEncode(string, Encoding);
[4]从数组中的指定位置开始一直到指定的字节数为止,将字节数组转换为 URL 编码的字符串。 [C#] public static string UrlEncode(byte[], int, int);
2、HttpUtility.UrlDecode 方法:
将已经为在 URL 中传输而编码的字符串转换为解码的字符串。重载列表:
[1][C#] public static string UrlDecode(string);
[2]使用指定的解码对象将 URL 编码的字节数组转换为已解码的字符串。 [C#] public static string UrlDecode(byte[], Encoding);
[3]使用指定的编码对象将 URL 编码的字符串转换为已解码的字符串。 [C#] public static string UrlDecode(string, Encoding);
[4]使用指定的编码对象,从数组中的指定位置开始到指定的字节数为止,将 URL 编码的字节数组转换为已解码的字符串。
[C#] public static string UrlDecode(byte[], int, int, Encoding);
3、Server是HttpServerUtility类的实例,是System.Web.UI.Page的属性。
HttpServerUtility.UrlEncode 方法:
编码字符串,以便通过 URL 从 Web 服务器到客户端进行可靠的 HTTP 传输。重载列表:
[1]对字符串进行 URL 编码,并返回已编码的字符串。 [C#] public string UrlEncode(string);
[2]URL 对字符串进行编码,并将结果输出发送到 TextWriter 输出流。 [C#] public void UrlEncode(string, TextWriter);
例: String str= "中国";
StringWriter writer = new StringWriter();
Server.UrlEncode(str, writer);
String EncodedString = writer.ToString();
4、HttpServerUtility.UrlDecode 方法:
对字符串进行解码,该字符串为了进行 HTTP 传输而进行编码并在 URL 中发送到服务器。重载列表 :
[1]对字符串进行 URL 解码并返回已解码的字符串。 [C#] public string UrlDecode(string);
[2]对在 URL 中接收的 HTML 字符串进行解码,并将结果输出发送到 TextWriter 输出流。 [C#] public void UrlDecode(string, TextWriter);
注意:
1、HttpUtility.UrlEncode,HttpUtility.UrlDecode是静态方法,而Server.UrlEncode,Server.UrlDecode是实例方法。
2、Server是HttpServerUtility类的实例,是System.Web.UI.Page的属性。
3、用HttpUtility.UrlEncode编码后的字符串和用Server.UrlEncode进行编码后的字符串对象不一样:
例如: string url="http://localhost:4349/name=中国";
Response.Write(HttpUtility.UrlEncode(url));
Response.Write("<br>");
Response.Write(Server.UrlEncode(url));
输出结果是:
http%3a%2f%2flocalhost%3a4349%2fname%3d%e4%b8%ad%e5%9b%bd
http%253a%252f%252flocalhost%253a4349%252fname%253d%25e4%25b8%25ad%25e5%259b%25bd
原因:Server.UrlEncode的编码方式是按照本地程序设置的编码方式进行编码的,而HttpUtility.UrlEncode是默认的按照.net的utf-8格式进行编码的。
如果改一下程序: string url="http://localhost:4349/name=中国";
Response.Write(HttpUtility.UrlEncode(url,System.Text.Encoding.GetEncoding("GB2312")));
Response.Write("<br>");
Response.Write(Server.UrlEncode(url));
输出的结果是:
http%253a%252f%252flocalhost%253a4349%252fname%253d%25e4%25b8%25ad%25e5%259b%25bd
http%253a%252f%252flocalhost%253a4349%252fname%253d%25e4%25b8%25ad%25e5%259b%25bd
4、有时候可能别的系统传递过来的url是用别的编码方式编码的。 介绍一个方法,可以获取指定编码格式的QueryString。
1
2
3
4
5
6
7
8
9
10
|
public string GetNonNullQueryString( string key,Encoding encoding) { //引用System.Collections.Specialized和System.Text命名空间 string stringValue; System.Collections.Specialized.NameValueCollection encodingQueryString; //该方法是在2.0中新增的 encodingQueryString = HttpUtility.ParseQueryString(Request.Url.Query,encoding); //'里面的key就是你提交的参数的Key return encodingQueryString[key] != null ? encodingQueryString[key].Trim() : "" ; } |
调用: string url = GetNonNullQueryString("url",Encoding.UTF8).Trim();
在对URL进行编码时,该用哪一个?这两都使用上有什么区别吗?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
测试: string file= "文件上(传)篇.doc" ; string Server_UrlEncode=Server.UrlEncode(file); string Server_UrlDecode=Server.UrlDecode(Server_UrlEncode); string HttpUtility_UrlEncode=System.Web.HttpUtility.UrlEncode(file); string HttpUtility_UrlDecode=System.Web.HttpUtility.UrlDecode(HttpUtility_UrlEncode); Response.Write( "原数据:" +file); SFun.WriteLine( "Server.UrlEncode:" +Server_UrlEncode); SFun.WriteLine( "Server.UrlDecode:" +Server_UrlDecode); SFun.WriteLine( "HttpUtility.UrlEncode:" +HttpUtility_UrlEncode); SFun.WriteLine( "HttpUtility.UrlDecode:" +HttpUtility_UrlDecode); 输出: 原数据:文件上(传)篇.doc Server.UrlEncode:%ce%c4%bc%fe%c9%cf%a3%a8%b4%ab%a3%a9%c6%aa.doc Server.UrlDecode:文件上(传)篇.doc HttpUtility.UrlEncode:%e6%96%87%e4%bb%b6%e4%b8%8a%ef%bc%88%e4%bc%a0%ef%bc%89%e7%af%87.doc HttpUtility.UrlDecode:文件上(传)篇.doc |
区别在于:HttpUtility.UrlEncode()默认是以UTF8对URL进行编码,而Server.UrlEncode()则以默认的编码对URL进行编码。
在用 ASP.Net 开发页面的时候, 我们常常通过 System.Web.HttpUtility.UrlEncode 和 UrlDecode 在页面间通过 URL 传递参数. 成对的使用 Encode 和 Decode 是没有问题的.
但是, 我们在编写文件下载的页面的时候, 常常用如下方法来指定下载的文件的名称: Response.AddHeader("Content-Disposition","attachment; filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8)); 之所以转换成 UTF8 是为了支持中文文件名.
出现问题:
因为 HttpUtility.UrlEncode 在 Encode 的时候, 将空格转换成加号('+'), 在 Decode 的时候将加号转为空格, 但是浏览器是不能理解加号为空格的, 所以如果文件名包含了空格, 在浏览器下载得到的文件, 空格就变成了加号.
解决办法:
在 HttpUtility 的 UrlEncode 之后, 将 "+" 替换成 "%20"( 如果原来是 "+" 则被转换成 "%2b" ) , 如:
fileName = HttpUtility.UrlEncode(fileName, Encoding.UTF8);
fileName = fileName.Replace("+", "%20");
不明白微软为什么要把空格转换成加号而不是"%20". 记得 JDK 的 UrlEncoder 是将空格转换成 "%20"的. 经检查, 在 .Net 2.0 也是这样.
2024-08-08 16:40:56【出处】:https://www.cnblogs.com/xinaixia/p/4086814.html
=======================================================================================
C#如何正确进行URL编码
在C#中进行URL编码可以使用System.Web命名空间下的HttpUtility类的UrlEncode方法。下面是一个示例代码:
using System;
using System.Web;
classProgram
{
staticvoidMain(){
string url = "https://www.example.com/?name=张三&age=25";
string encodedUrl = HttpUtility.UrlEncode(url);
Console.WriteLine("Encoded URL: " + encodedUrl);
}
}
在这个示例中,我们首先引入System.Web命名空间,然后使用HttpUtility类的UrlEncode方法对URL进行编码。最后,将编码后的URL输出到控制台。
请注意,通常情况下,对URL进行编码是为了将特殊字符转换为URL安全的形式,以避免在传输过程中出现问题。
出处:https://www.yisu.com/ask/55967478.html
=======================================================================================
关注我】。(●'◡'●)
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的【因为,我的写作热情也离不开您的肯定与支持,感谢您的阅读,我是【Jack_孟】!
本文来自博客园,作者:jack_Meng,转载请注明原文链接:https://www.cnblogs.com/mq0036/p/18349260
【免责声明】本文来自源于网络,如涉及版权或侵权问题,请及时联系我们,我们将第一时间删除或更改!
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· DeepSeek “源神”启动!「GitHub 热点速览」
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
2023-08-08 程序员常用的工具软件推荐
2023-08-08 C#/.NET/.NET Core优秀项目和框架每周精选
2023-08-08 ABP框架在线学习目录
2022-08-08 高DPI适配经验 ---- 系列文章
2018-08-08 Windows平台上使用Github搭建Git服务器的图文教程
2018-08-08 Git安装和TortoiseGit详细使用教程【基础篇】
2018-08-08 DOS命令之at命令详解