关于HttpUtility.UrlEncode、UrlDecode,Server.UrlEncode、UrlDecode编码

关于HttpUtility.UrlEncode、UrlDecode,Server.UrlEncode、UrlDecode编码

HttpUtility.UrlEncode 方法:

对 URL 字符串进行编码,以便实现从 Web 服务器到客户端的可靠的 HTTP 传输。

重载列表
将字节数组转换为已编码的 URL 字符串,以便实现从 Web 服务器到客户端的可靠的 HTTP 传输。
[C#] public static string UrlEncode(byte[]);

对 URL 字符串进行编码,以便实现从 Web 服务器到客户端的可靠的 HTTP 传输。
[C#] public static string UrlEncode(string);

使用指定的编码对象对 URL 字符串进行编码,以便实现从 Web 服务器到客户端的可靠 HTTP 传输。
[C#] public static string UrlEncode(string, Encoding);

从数组中的指定位置开始一直到指定的字节数为止,将字节数组转换为 URL 编码的字符串,以便实现从 Web 服务器到客户端的可靠的 HTTP 传输。
[C#] public static string UrlEncode(byte[], int, int);

HttpUtility.UrlDecode 方法:

将已经为在 URL 中传输而编码的字符串转换为解码的字符串。

重载列表
将已经为在 URL 中传输而编码的字符串转换为解码的字符串。
[C#] public static string UrlDecode(string);

使用指定的解码对象将 URL 编码的字节数组转换为已解码的字符串。
[C#] public static string UrlDecode(byte[], Encoding);

使用指定的编码对象将 URL 编码的字符串转换为已解码的字符串。
[C#] public static string UrlDecode(string, Encoding);

使用指定的编码对象,从数组中的指定位置开始到指定的字节数为止,将 URL 编码的字节数组转换为已解码的字符串。
[C#] public static string UrlDecode(byte[], int, int, Encoding);


Server是HttpServerUtility类的实例,是System.Web.UI.Page的属性。
HttpServerUtility.UrlEncode 方法:
编码字符串,以便通过 URL 从 Web 服务器到客户端进行可靠的 HTTP 传输。

重载列表
对字符串进行 URL 编码,并返回已编码的字符串。
[C#] public string UrlEncode(string);

URL 对字符串进行编码,并将结果输出发送到 TextWriter 输出流。
[C#] public void UrlEncode(string, TextWriter);

HttpServerUtility.UrlDecode 方法:
对字符串进行解码,该字符串为了进行 HTTP 传输而进行编码并在 URL 中发送到服务器。

重载列表
对字符串进行 URL 解码并返回已解码的字符串。
[C#] public string UrlDecode(string);

对在 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://www.xingzhu.net.cn?name=客户管理";
Response.Write(HttpUtility.UrlEncode(url));
Response.Write("<br>");
Response.Write(Server.UrlEncode(url));

输出结果是:
http%3a%2f%2fwww.xingzhu.net.cn%3fname%3d%e5%ae%a2%e6%88%b7%e7%ae%a1%e7%90%86
http%3a%2f%2fwww.xingzhu.net.cn%3fname%3d%e5%ae%a2%e6%88%b7%e7%ae%a1%e7%90%86


原因:Server.UrlEncode的编码方式是按照本地程序设置的编码方式进行编码的,而HttpUtility.UrlEncode是默认的按照.net的utf-8格式进行编码的。

如果改一下程序:

string url1="http://www.xingzhu.net.cn/Search.aspx?type=news&tags=制造业";
Response.Write(HttpUtility.UrlEncode(url1,System.Text.Encoding.GetEncoding("GB2312")));
Response.Write("<br>");
Response.Write(Server.UrlEncode(url1));

输出的结果是:

http%3a%2f%2fwww.xingzhu.net.cn%2fSearch.aspx%3ftype%3dnews%26tags%3d%d6%c6%d4%ec%d2%b5
http%3a%2f%2fwww.xingzhu.net.cn%2fSearch.aspx%3ftype%3dnews%26tags%3d%e5%88%b6%e9%80%a0%e4%b8%9a

3、有时候可能别的系统传递过来的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();

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值