用IHttpModule解决输入中文地址乱码问题(二)

本文是 用IHttpModule解决输入中文地址乱码问题(一)  的续文。

上文说到,需要对已有的地址进行GB2312编码,这样大大增加了工作量,有没更好的办法呢?

 1       public   class  HookModule : IHttpModule
 2      {
 3 
 4           #region  IHttpModule 成员
 5 
 6           public   void  Dispose()
 7          {
 8              
 9          }
10 
11           public   void  Init(HttpApplication context)
12          {
13              context.BeginRequest  +=   new  EventHandler(context_BeginRequest);
14          }
15 
16           void  context_BeginRequest( object  sender, EventArgs e)
17          {
18              
19              HttpApplication application  =  (HttpApplication)sender;
20              HttpContext context  =  application.Context;
21              IdentifyEncoding ie  =   new  IdentifyEncoding();
22 
23              
24               string  rawurl  =  context.Request.RawUrl;
25              rawurl  =  HttpUtility.UrlDecode(rawurl);
26               byte [] bytes  =  System.Web.HttpUtility.UrlDecodeToBytes(rawurl, Encoding.Default);
27 
28              Encoding enc  =  Encoding.Default;
29               try
30              {
31                  enc  =  Encoding.GetEncoding(ie.GetEncodingName(IdentifyEncoding.ToSByteArray(bytes)));
32              }
33               catch  { }
34               string  s  =  enc.GetString(bytes);
35              context.RewritePath(s);
36          }
37 
38 
39 
40           #endregion
41      }


这里用了一个检查当前请求地址六编码的函数。这个函数实际上是从java转过来的(Create By lion),用来做蜘蛛自动侦测网站编码还不错。
由于代码较长,需要的可以从附件下载。

在这种处理中,实际上还有另外一个问题,假如,你把链接的中文编码成utf-8是会出现问题的。

一下是详细的用法:

1、网页中有 <a href="http://localhost/a.aspx?key=就是中文">http://localhost/a.aspx?key=就是中文</a> 这样的中文链接,无论这个网页的编码是什么类型的,都是不会出现乱码的;
2、网页中有<a href="http://localhost/a.aspx?key=System.Web.HttpUtility.UrlEncode("就是中文",Encoding.GetEncoding("gb2312"))
">http://localhost/a.aspx?key=就是中文</a>这样的中文链接,无论这个网页的编码是什么类型的,都是不会出现乱码的;
3、比如,使用地址重写中,有这个样的链接 "http://localhost/w/就是中文" ,无论你在IE中还是FF中直接地址栏输入都不会出现乱码。

附件:
1、DLL( http://files.cnblogs.com/birdshover/YesHJ.Search.GBHookModule.rar )

注:附件中的DLL可以在WEB.CONFIG中使用
      <httpModules>
        <add name="GB" type="YesHJ.Search.GBHookModule.HookModule,YesHJ.Search.GBHookModule"/>
    <add name="Rewriter" type="XP.Framework.HttpModule.UrlRewriter.HttpModule,XP.Framework.HttpModule.UrlRewriter"/>
      </httpModules>

在根目录建立文件Rewriter.config,放入重写规则,例如

<?xml version="1.0" encoding="utf-8" ?>
<Rules>
 <RewriterRule>
  <LookFor>/w/(?&lt;type&gt;[a-z])/(?&lt;word&gt;[^/]*)/(?&lt;page&gt;\d+)/*</LookFor>
  <SendTo>~/so.aspx?t=$type$&amp;key=$word$&amp;p=$page$</SendTo>
 </RewriterRule>
 <RewriterRule>
  <LookFor>/w/(?&lt;type&gt;[a-z])/(?&lt;word&gt;[^/]*)/?</LookFor>
  <SendTo>~/so.aspx?t=$type$&amp;key=$word$</SendTo>
 </RewriterRule>
 <RewriterRule>
  <LookFor>/w/(?&lt;word&gt;[^/]*)/(?&lt;page&gt;\d+)/?</LookFor>
  <SendTo>~/so.aspx?key=$word$&amp;p=$page$</SendTo>
 </RewriterRule>
 <RewriterRule>
  <LookFor>/w/(?&lt;word&gt;[^/]*)/?</LookFor>
  <SendTo>~/so.aspx?key=$word$</SendTo>
 </RewriterRule>
</Rules>

可以用源代码查看工具,查看DLL源码。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值