e157. converting x-www-form-urlencoded data.txt

来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 20 行

TXT
20
字号
Name/value pairs that are formatted using the x-www-form-urlencoded specification appear as: 
    name1=value1&name2=value2

where nameN and valueN must be escaped. For example, a+b will appear as a%2Bb when escaped. The URLEncoder and URLDecoder classes are used to escape the names and values. 
    try {
        // Construct a x-www-form-urlencoded string
        String line = URLEncoder.encode("name1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
        line += "&" + URLEncoder.encode("name2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");
    
        // Parse a x-www-form-urlencoded string
        String[] pairs = line.split("\\&");
        for (int i=0; i<pairs.length; i++) {
            String[] fields = pairs[i].split("=");
            String name = URLDecoder.decode(fields[0], "UTF-8");
            String value = URLDecoder.decode(fields[1], "UTF-8");
        }
    } catch (UnsupportedEncodingException e) {
    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?