zoukankan      html  css  js  c++  java
  • 中文网页编解码问题

    -1、解决httpexchange输入中文乱码问题

    // parse request
    Map<String, Object> parameters = new HashMap<String, Object>();
                            
    //It's not exactly the answer to my question, but it helped me to solve the issue nonetheless.
    //The mistake I have made is that I'm using URI.getQuery (that decodes the parameters) instead of using URI.getRawQuery. I was assuming that HttpServer was doing the decoding; this assumption was wrong.
    String query = requestedUri.getQuery();

    这里要使用getQuery() 而不是getRawQuery)

     

    0、说明:

    0.1、首先是unicode转义字符

    在涉及Web前端开发时, 有时会遇到uXXXX格式表示的字符, 其中XXXX是16进制数字的字符串表示形式, 在js中这个叫Unicode转义字符, 和   同属于转义字符. 在其他语言中也有类似的, 可能还有其它变形的格式.

    0.2、url编码/百分比编码

    HTTP 請求參數,必須使用請求參數名稱與請求參數值,中間以等號(=)表示成對關係,現在問題來了,如果請求參數值本身包括=符號怎麼辦?又或許你想發送的請求 參數值是「http://openhome.cc」這個值呢?假設是GET請求,你不能直接這麼在網址列上鍵入:http://openhome.cc/addBookmar.do?url=http://openhome.cc

    在URI的規範中定義了一些保留字元(Reserved character),像是「:」、「/」、「?」、「&」、「=」、「@」、「%」等字元,在URI中都有它的作用,如果你要在請求參數上表達URI中的保留字元,必須在%字元之後以十六進位數值表示方式,來表示該字元的八個位元數值。

    例如,「:」字元真正儲存時的八個位元為00111010,用十六進位數值來表示則為3A,所以必須使用「%3A」來表示「:」,「/」字元儲存時的八個 位元為00101111,用十六進位表示則為2F,所以必須使用「%2F」來表示「/」字元,所以想發送的請求參數值是「http: //openhome.cc」的話,則必須使用以下格式:http://openhome.cc/addBookmar.do?url=http%3A%2F%2Fopenhome.cc。這是URI規範中的百分比編碼(Percent-Encoding),也就是俗稱的URI編碼或URL編碼。

     

    1、16进制解码

     

    static final Pattern reUnicode = Pattern.compile("\\u([0-9a-zA-Z]{4})");

    public static String decode(String s) {

        Matcher m = reUnicode.matcher(s);

        StringBuffer sb = new StringBuffer(s.length());

        while (m.find()) {

            m.appendReplacement(sb,

                    Character.toString((char) Integer.parseInt(m.group(1), 16)));

        }

        m.appendTail(sb);

        return sb.toString();

    }

     2、然后网页解码

     URLDecoder.decode(url,"UTF-8")

    3、综合就是

    decode( URLDecoder.decode(url,"UTF-8"))

    4、参考文献

    http://netwjx.github.io/blog/2012/07/07/encode-and-decode-unicode-escape-string/

    http://openhome.cc/Gossip/Encoding/URLEncoding.html

  • 相关阅读:
    thinkphp 5 隐藏index.php
    jquery ajax参数
    图标字的使用方法
    jquery监听浏览宽度
    手机屏幕的宽度自动适应
    前站常用代码
    服务消费者(Feign-上)
    服务消费者(Ribbon)
    注册中心(Eureka/Consul)
    JDK8 日期格式化
  • 原文地址:https://www.cnblogs.com/amazement/p/5840562.html
Copyright © 2011-2022 走看看