zoukankan      html  css  js  c++  java
  • 解决“在多字节的目标代码页中,没有此Unicode字符可以映射到的字符”

    今天在处理Google网站管理员中的500错误时发现这样一些URL:

    http://www.cnblogs.com/Garnai/tag/3D%3F%96%CA/
    http://www.cnblogs.com/henryfan/tag/%3F%3F%3F%90%B6%90%AC%3F%8C%8F/
    http://www.cnblogs.com/zhangpengshou/tag/%3F%96%DA%3F%97%9D%94V%8FC%3F/
    http://www.cnblogs.com/henryfan/tag/%3F%3F%3F%90%B6%90%AC%3F%8C%8F/
    ...

    这些URL不仅出现500错误,而且不显示自定义错误,只显示ASP.NET的默认错误页面:

    运行时错误

    服务器日志中记录具体的错误信息是:

    [ArgumentOutOfRangeException: 在多字节的目标代码页中,没有此 Unicode 字符可以映射到的字符。 (异常来自 HRESULT:0x80070459)]
       System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo) +0
       System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode) +13563503
       System.Web.Hosting.IIS7WorkerRequest.GetServerVariableInternal(String name) +50
       System.Web.Hosting.IIS7WorkerRequest.ReadRequestHeaders() +144
       System.Web.Hosting.IIS7WorkerRequest.GetKnownRequestHeader(Int32 index) +109
       System.Web.HttpWorkerRequest.HasEntityBody() +27
       System.Web.HttpRequest.GetEncodingFromHeaders() +126
       System.Web.HttpRequest.get_ContentEncoding() +162
       System.Web.HttpRequest.get_QueryStringEncoding() +10
       System.Web.HttpRequest.get_QueryStringText() +209
       System.Web.HttpRequest.ValidateInputIfRequiredByConfig() +87
       System.Web.PipelineStepManager.ValidateHelper(HttpContext context) +55

    对应的英文错误信息是:

    No mapping for the Unicode character exists in the target multi-byte code page.

    从这些出错的URL中观察到了一个规律:都包含%3F这个编码,解码出来对应的字符是?。

    从错误信息的代码执行堆栈信息 System.Web.HttpRequest.get_QueryStringText() 中,可以看出错误发生在从URL中读取查询字符串的时候。

    可是出错的URL中并没有查询字符串。。。

    后来突然想到,ASP.NET是先进行UrlDecode,然后再进行get_QueryStringText()的。

    比如将  http://www.cnblogs.com/Garnai/tag/3D%3F%96%CA/ 进行URLDecode之后得到的URL是:

    http://www.cnblogs.com/Garnai/tag/3D?柺/

    看到没有,出现了问号,变成有查询字符串的URL。于是,ASP.NET将问号之后的字符作为key进行读取,由于key是不支持中文的,于是引发“在多字节的目标代码页中,没有此Unicode字符可以映射到的字符”。如果ASP.NET先进行get_QueryStringText(),再进行UrlDecode就不会触发这个问题,可是ASP.NET偏偏不这么干。

    那如何解决这个问题?

    虽然问题出在ASP.NET,但我们无法改变ASP.NET,只能另辟蹊径。

    既然是ASP.NET处理上的问题,那我们别无选择,只能抢在ASP.NET之前拦截这样的URL请求,而进行这样的拦截最简单的工具就是IIS的Url Rewrite module。

    根据我们的应用场景,在rewriteRules.config中添加一条规则——在URL中如果/tag/之后出现问号就直接返回404,规则定义如下:

    <rule name="block_invalid_tag_url" stopProcessing="true">
        <match url="^[^/]+/tag/.*??.*$" />
        <action type="CustomResponse" statusCode="404" statusReason="The request URL is invalid" 
            statusDescription="The request URL is invalid" />
    </rule>

    然后就搞定了这个问题,写了这篇博客。 

  • 相关阅读:
    Y2K Accounting Bug(POJ 2586)
    Power of Cryptography(POJ 2109 math )
    codeforces C. Valera and Tubes
    codeforces C. Devu and Partitioning of the Array
    codeforces C. Ryouko's Memory Note
    codeforces C. k-Tree
    codeforces C. Prime Swaps
    codeforces C. Xor-tree
    codeforces B. Prison Transfer
    codeforces C. Sereja and Swaps
  • 原文地址:https://www.cnblogs.com/dudu/p/no-mapping-for-the-unicode-character.html
Copyright © 2011-2022 走看看