Internet Explorer在查询字符串中具有特殊字符的问题 [英] Internet Explorer having problems with special chars in querystrings

查看:266
本文介绍了Internet Explorer在查询字符串中具有特殊字符的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不是一个新的 queestion



这里有很多关于IE的问题在查询字符串中处理特殊字符时遇到一些问题。在所有情况下,Chrome都是相同的:Chrome,Firefox,Safari(所有人)正确处理UTF-8编码的网址,几乎所有这些URL甚至可以处理IRI不被编码到URL中的情况。但IE坚持让开发人员努力工作。



我自己遇到了这个问题,并且已经做了很多工作。对我来说,似乎IE由于某种原因坚持将UTF-8编码的URL解码为ISO-8859-1,然后将其发送到服务器。



我的案例



我是丹麦的居民,因此我必须与丹麦语æøå一起工作。
有很多情况下,我想将参数从我的视图发送到一些C#方法。特殊字符经常弹出的两个例子:


  1. 搜索

  2. 文件名规范对于下载的文件

说一个丹麦人想要搜索丹麦语æblegrød(特殊的苹果派)。在Chrome和Firefox中,如果我只是使用IRI浏览器:

  http://example.com/Search/QuickSearch ?searchQuery =æblegrød

发送到服务器的查询将如下所示:

  http://example.com/Search/QuickSearch?searchQuery=%C3%A6blegr%C3%B8d 

在Internet Explorer中,它将如下所示:

 http://example.com/Search/QuickSearch?searchQuery=Ã|blegrød

现在很容易看到问题是什么。
Firefox& Chrome是网址编码的URL


...不是ASCII字母或数字到%HH的每个字节,其中HH是
字节的十六进制值



http://www.w3.org/International/O-URL-code.html


其中Internet Exlorer代替对字符串进行直接的UTF-8编码,导致Ã|blegrød。这也是一样的结果,如果你拿一个UTF-8字符串,并解码它就像是ISO-8859-1,这是一个巧合吗?



我尝试了一些东西



由于Internet Explorer可以选择将URL路径发送为UTF-8,因此我尝试禁用该功能。因为IE必须处理searchQuery =æblegrød时出现错误,所以我尝试对IRI进行编码,然后再将其传递给浏览器。



导致所有浏览器都可以使用以下URL:

  http://example.com/Search/QuickSearch?searchQuery= %C3%A6blegr%C3%B8d 

IE然而不在乎,我在网络中看到什么日志仍然是URL

 http://example.com/Search/QuickSearch?searchQuery=Ã|blegrød

发送到服务器。



这是我的配置如何:


  1. 文件保存为UTF-8

  2. 我设置了元标记:

     < meta charset =UTF-8> 


  3. IE发送URL路径为UTF-8(也将IE设置为
    全球化设置为UTF-8

     < worldwide 
    uiCulture =da-DK
    culture =da-Dk

    fileEncoding =utf-8
    responseEncoding = utf-8
    requestEncoding =utf-8
    responseHeaderEncoding =utf-8/>


我用完了想法,不知道我做错了什么我倾向于IE创造破坏,但我真的不知道是否是我在项目中错误地设置的东西。

解决方案


玩弄这个,我得出结论,我唯一可以做的是编码所有的URL,然后使用内容处置(这个SO文章的帮助),使其适用于不同的浏览器。解决方案并不完美,它仍然有一些缺陷,但这是我迄今为止发现的最好的方法。



在我所有的情况下,链接都是用JS,所以 encodeURIComponent 是我的转到方法对URL进行编码。


This is not a new queestion

There are quite a few of questions here on SO about IE having some problems with handling special characters in the querystrings. In all of the cases it is the same: Chrome, Firefox, Safari (everyone) handles the UTF-8 encoded URLs correctly, almost all of them even handles the cases where the IRIs aren't encoded into URLs. But IE insists on make life hard for the developers.

As I have run into the problem myself, and have worked quite a bit with it. To me it seems that IE for some reason insists on decoding the UTF-8 encoded URL into ISO-8859-1 before sending it to the server.

My case

I am a resident in Denmark, and therefor I have to work with the danish letters æøå. There a many cases where I want to send parameters from my views into some C# methods. Two examples of such places where the special characters often pop up:

  1. Searching
  2. Specification of filename for a downloaded files

Say a Dane wants to search for the danish word "æblegrød" (special kind of apple pie). In Chrome and Firefox, if I just feed the browser with the IRI:

http://example.com/Search/QuickSearch?searchQuery=æblegrød

The query sent to the server would look like this:

http://example.com/Search/QuickSearch?searchQuery=%C3%A6blegr%C3%B8d

In Internet Explorer however it would look like this:

http://example.com/Search/QuickSearch?searchQuery=æblegrød

It is now easy to see what the problem is. Firefox & Chrome are URL encoding the URLs

... each byte that is not an ASCII letter or digit to %HH, where HH is the hexadecimal value of the byte

http://www.w3.org/International/O-URL-code.html

Where Internet Exlorer instead is doing a direct UTF-8 encoding of the string, resulting in "æblegrød". This is also the same end results as if you take a UTF-8 string and decode it as if it was ISO-8859-1, is this a coincidence?

I have tried some things

As Internet Explorer has the option to "send URL path as UTF-8" I tried disabling that. Changing nothing.

As it went wrong when IE has to handle "searchQuery=æblegrød" I tried encoding the IRI before handing it to the browser. Resulting in all browsers getting the following URL to work with:

http://example.com/Search/QuickSearch?searchQuery=%C3%A6blegr%C3%B8d

IE however doesn't care, what I see in the networking log is still the URL

http://example.com/Search/QuickSearch?searchQuery=æblegrød

being sent to the server.

This is how my configuration is:

  1. Files are saved as UTF-8
  2. I set the meta tag:

    <meta charset="UTF-8">
    

  3. IE sends URL-paths as UTF-8 (also set IE to do this for intranet querystrings)

  4. Globalization set to UTF-8

    <globalization          
        uiCulture="da-DK"
        culture="da-Dk"
    
        fileEncoding="utf-8"
        responseEncoding="utf-8"
        requestEncoding="utf-8"
        responseHeaderEncoding="utf-8" />
    

I am running out of ideas, I don't know what it is that I am doing wrong. I am leaning towards IE creating the havoc, but I genuinely do not know if it is something that I have set up wrongly in my project.

解决方案

An answer to the future people hitting this question.

Playing around with this, I got to the conclusion that the only thing I could do was to encode all of my URL's, and then work with the Content Disposition (with help from this SO post) to make it work for different browsers. The solution is not perfect, it still has some flaws, but it is the best approach I have found so far.

In all of my cases, the links were build with JS, so encodeURIComponent is my go-to method to encode the URLs.

这篇关于Internet Explorer在查询字符串中具有特殊字符的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆