All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
数据库中的命名规则_[SQL,Server教程]
asp.net实现简单的用户登录_[Asp.Net教程]
获得汉字字符串的首个拼音字母的缩写_[Asp.Net教程]
跟我学SQL:(八)数值数据类型_mssql学习_编程技术
ASP.Net文件下载函数_[Asp.Net教程]
SQL,SERVER的内存会不断增加,问题分析_[SQL,Server教程]
动态网页PHP程序中文件上传的安全问题_php资料_编程技术
.NET,SDK中CorFlags.Exe的使用方法_.net资料_编程技术
预载入和javascript,Image()对象_JavaScript技术_编程技术
asp.net显示下载提示的下载网页程序_[Asp.Net教程]
Reading number is top 10 pictures
NeedWallpaper7
迷人的靓女
西游四格漫画(三)
八个盛产美女的国家2
The world's ten biggest attractions of inventory super the moon
29 the belle stars after bath figure4
Sell the barbecue as says father du breul4
漂亮的跳舞妹妹2
再发两张抽象画
Summer is most suitable for young people to travel in China9
Download software ranking
jdk1.5
Boxer vs Yellow4
Detective task-the top secret prostitution files
WebService在.NET中的实战应用教学视频 → 第2集
Tram sex maniac 2 (H) rar bag10
I'm come from Beijing1
C++教程第四版
Tram sex maniac 2 (H) rar bag3
少妇苏霞全本
Unix video tutorial5
归海一刀 published in(发表于) 2014/1/30 1:12:39 Edit(编辑)
用IHttpModule解决输入中文地址乱码问题(一)_[Asp.Net教程]

用IHttpModule解决输入中文地址乱码问题(一)_[Asp.Net教程]

用IHttpModule解决输入中文地址乱码问题(一)_[Asp.Net教程]

测试环境:

服务器 —— [本机] WIindows2003 Enterprise Edition 2003 sp2 中文版本,IIS 6.0,IE 7.0,ASP.NET 2.0
客户端 —— [本机] 同上,Firefox 2.0.0.12

作者:birdshover

本文是初步探讨文章。需要做一下几个假设:
1、网站采用的是ASP.NET部署的;
2、网站的编码是UTF-8的;
3、修改页面链接是成本很小的。

现在面临的主要问题是,页面内有很多链接,参数直接带的就是中文,这样比URL转码后的地址友好。但是,当甲用户把觉得不错的地址发送给好友乙时,问题,出现了,参数将会变成乱码!

事实上在UTF-8环境下,网站上页面的链接是可以为中文的,例如:
http://localhost/a.aspx?key=就是中文

但是,复制“http://localhost/a.aspx?key=就是中文”直接敲入地址栏,将会产生乱码。

本文旨在解决地址栏输入中文时出现的问题,这种处理方式简单,但是会与已有链接造成冲突。避免冲突的方法,比较复杂,后续文章将会介绍。

利用.net httpmodule重写,可以解决以上问题。

构造HttpModule如下:

public class HookModule : IHttpModule
{

#region IHttpModule 成员

public void Dispose()
{

}

public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}

void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
string rawurl = context.Request.RawUrl;

context.RewritePath(rawurl);

}



#endregion
}

配置完成后,在地址栏直接输入“http://localhost/a.aspx?key=就是中文”发现已经不产生乱码(IE,TheWorld 2.0)。

但是当在Firefox中敲入以上地址时,发现还是变成了乱码。
那是因为Firefox会强行给地址进行URL编码。把上述代码改造成:


public class HookModule : IHttpModule
{

#region IHttpModule 成员

public void Dispose()
{

}

public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}

void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
string rawurl = context.Request.RawUrl;
byte[] bytes = System.Web.HttpUtility.UrlDecodeToBytes(rawurl, Encoding.Default);
string s = Encoding.Default.GetString(bytes);
context.RewritePath(s);

}



#endregion
}


OK,问题解决了,在三种浏览器中都实现了正常显示。

看到这里不要结束,因为问题有可能已经产生了。

你会发现点击http://localhost/a.aspx?key=就是中文
这种地址时,反而变成了乱码。

在本文中的解决方法是,把地址编码
System.Web.HttpUtility.UrlEncode("就是中文",Encoding.GetEncoding("gb2312"))
这样就没有乱码的问题了。

注意,上述代码使用Encoding.Default是因为我的系统是中文版本的,因此默认编码就是gb2312.

全文完。


原文出自:http://www.cnblogs.com/birdshover/







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.