Handling Character Encoding in URI on Tomcat

前端 未结 1 1077
伪装坚强ぢ
伪装坚强ぢ 2020-12-06 17:14

On the web site I am trying to help with, user can type in an URL in the browser, like following Chinese characters,

  http://localhost:8080?a=测试


        
相关标签:
1条回答
  • 2020-12-06 17:39

    Unfortunately, UTF-8 encoding is a "should" in the URI specification, which seems to assume that the origin server will generate all URLs in such a way that they will be meaningful to the destination server.

    There are a couple of techniques that I would consider; all involve parsing the query string yourself (although you may know better than I whether setting the request encoding affects the query string to parameter mapping or just the body).

    First, examine the query string for single "high-bytes": a valid UTF-8 sequence must have two or more bytes (the Wikipedia entry has a nice table of valid and invalid bytes).

    Less reliable would be to look a the "Accept-Charset" header in the request. I don't think this header is required (haven't looked at the HTTP spec to verify), and I know that Firefox, at least, will send a whole list of acceptable values. Picking the first value in the list might work, or it might not.

    Finally, have you done any analysis on the logs, to see if a particular user-agent will consistently use this encoding?

    0 讨论(0)
提交回复
热议问题