简体   繁体   English

查询字符串中的字符编码,希伯来语

[英]Character encoding in query string, hebrew

I am trying to send a GET request with query string parameter in hebrew. 我正在尝试发送带有希伯来语查询字符串参数的GET请求。 When the controller gets the request, the parameter is in gibberish. 当控制器收到请求时,该参数为乱码。 i've added "org.springframework.web.filter.CharacterEncodingFilter" but it didn't change a thing. 我添加了“ org.springframework.web.filter.CharacterEncodingFilter”,但是它并没有改变任何东西。

Please advise how to fix it. 请告知如何解决。

Update: here is the the request. 更新:这是请求。

GET /myapp/specialties?query=%D7%92%D7%99%D7%A0%D7%A0%D7%A0%D7%A0 HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Accept: *
Cache-Control: no-cache
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like           Gecko) Chrome/33.0.1750.117 Safari/537.36
Content-Type: application/json;charsert=utf-8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,he;q=0.6
Query String Parametersview sourceview URL encoded
query:גיננננ
Response Headersview source
Content-Type:application/json;charset=UTF-8
Date:Mon, 03 Mar 2014 20:45:17 GMT
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked

web.xml web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">

    <display-name>med.rec</display-name>


    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/application-config.xml</param-value>
    </context-param>

    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/mvc-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

-Roy 罗伊

As it turns out HttpServletRequest#setCharacterEncoding(String) which CharacterEncodingFilter uses 事实证明CharacterEncodingFilter使用的HttpServletRequest#setCharacterEncoding(String)

Overrides the name of the character encoding used in the body of this request . 覆盖此请求正文中使用的字符编码的名称 This method must be called prior to reading request parameters or reading input using getReader(). 在读取请求参数或使用getReader()读取输入之前,必须先调用此方法。 Otherwise, it has no effect. 否则,它将无效。

Which is no good for you since you aren't getting the parameters from the body, but rather from the query string. 这对您没有好处,因为您不是从正文中获取参数,而是从查询字符串中获取参数。

If you are using Tomcat, however, you are in luck. 但是,如果您使用的是Tomcat,那么您会很幸运。 Tomcat has a special Connector attribute which, when set (it's unset by default), will use that same character encoding for the query string. Tomcat具有特殊的Connector属性,该属性在设置时(默认情况下未设置)将对查询字符串使用相同的字符编码。

That attribute is useBodyEncodingForURI . 该属性是useBodyEncodingForURI If you open up your Tomcat servet.xml file, you will find an element like (without the attribute) 如果打开Tomcat servet.xml文件,则会找到一个类似的元素(没有属性)

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1"
    redirectPort="8443" useBodyEncodingForURI="true">
</Connector>

Add the attribute and it will work as intended. 添加该属性,它将按预期工作。 Make sure you are setting it for the appropriate Connector , HTTP in this case. 在这种情况下,请确保将其设置为适当的Connector ,HTTP。

Other Servlet containers probably have some similar configuration. 其他Servlet容器可能具有一些类似的配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM