C#编码、解码

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。

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进行编码时,该用哪一个?这两都使用上有什么区别吗?

测试:
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 也是这样.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值