url中(GET请求)中包含特殊符号字符等导致请求报错的解决方案

时间:2024-02-20 09:44:42

因为GET和POST请求不同Get请求特殊符号浏览器并不认识(如:*,#,{},[],^..等等)那么我们应该怎么做才能然浏览器认识呢?其实我们至于要把特殊符号替换成浏览器可以认识的特殊符号就可以了,话不多说详细请看下面两种方法:

方法一

直接替换:

假如我发送的内容如:127.0.0.1:8030/xxx.action?key=ysude${"operators":"77777",flow_no":"AAA150301000001","vipno":"001012001212120121","certifytype":"0"}

首先可以先把这条数据的放入到Map中

map.put ( "key","VIPCERTIFY${\"operators\":\"77777\",flow_no\":\"AAA150301000001\",\"vipno\":\"001012001212120121\",\"certifytype\":\"0\"}");

for(Map.Entry<String,String> mapEntrty : map.entrySet ()){
                List_operators = mapEntrty.getValue ();
 }//遍历map取出value 。或者直接用map.get("key").toString();获取到Map的键 两种方式都可以

  String MSG_List = List_operators;
    for (int i = 0; i <List_operators.length () ; i++) {//遍历Value
         String chartString = List_operators.charAt ( i )+"";//通过charAt(i)意思就是根据每一个字符串的下标找到每一个字符串,返回一个字符串
         MSG_List =    ArrayUtil.replaceArray (MSG_List,chartString); //去替换
   }

ArrayUtil工具类:

/**
     *  解密GET请求方式URL
     * @param List_operators 参数
     * @param arr 每一个参数
     * @return  转译后的参数
     */
 public  static String replaceArray(String List_operators,String arr){
        Map<String,String> map = new HashMap<String, String> (  );
        map.put ( "$","%24");
        map.put ( ":","%3A");
        map.put ( "[","%5B");
        map.put ( "]","%5D");
        map.put ( ",","%2C");
        map.put ( "{","%7B");
        map.put ( "}","%7D");
        map.put ( "#","%23");
        map.put ( "\"","%22");
        map.put ( "\\","%5C");
        map.put ( "-","%2D" );
        map.put ( " ","%20" );
        map.put ( "!","%21" );
        map.put ( "%","%25" );
        map.put ( "&","%26" );
        map.put ( "\'","%27" );
        map.put ( "(","%28" );
        map.put ( ")","%29" );
        map.put ( "*","%2A" );
        map.put ( "+","%2B" );
        map.put ( ".","%2E" );
        map.put ( "/","%2F" );
        map.put ( ";","%3B" );
        map.put ( "<","%3C" );
        map.put ( "=","%3D" );
        map.put ( ">","%3E" );
        map.put ( "@","%40" );
        map.put ( "^","%5E" );
        map.put ( "_","%5F" );
        map.put ( "`","%60" );
        map.put ( "|","%7C" );
        map.put ( "~","%7E" );
        map.put ( "ƒ","%83" );
        map.put ( "…","%85" );
        map.put ( "†","%86" );
        map.put ( "‡","%87" );
        map.put ( "ˆ","%88" );
        map.put ( "‰","%89" );
        map.put ( "Š","%8A" );
        map.put ( "‹","%8B" );
        map.put ( "Œ","%8C" );
        map.put ( "Ž","%8E" );
        map.put ( "•","%95" );
        map.put ( "–","%96" );
        map.put ( "—","%97" );
        map.put ( "˜","%98" );
        map.put ( "™","%99" );
        map.put ( "š","%9A" );
        map.put ( "›","%9B" );
        map.put ( "œ","%9C" );
        map.put ( "ž","%9E" );
        map.put ( "Ÿ","%9F" );
        map.put ( "¢","%A2" );
        map.put ( "£","%A3" );
        map.put ( "¤","%A4" );
        map.put ( "¥","%A5" );
        map.put ( "¦","%A6" );
        map.put ( "§","%A7" );
        map.put ( "¨","%A8" );
        map.put ( "©","%A9" );
        map.put ( "«","%AB" );
        map.put ( "¬","%AC" );
        map.put ( "®","%AE" );

        for(Map.Entry<String,String> entry : map.entrySet ()){
          if(entry.getKey ().equals ( arr )){
              List_operators=  List_operators.replace( arr ,entry.getValue ()).toString ();//找到特殊符号就替换否则则不替换
              break;
          }
        }
        return  List_operators;
    }

例子解析完的路径:http://192.168.2.136:8030/request/json.action?key=SENDFP%24%7B%22flow%5Fno%22%3A%2220160419155921100425555%22%2C%22salehead%22%3A%7B%22operators%22%3A%225000%22%2C%22vmkt%22%3A%22FZ0060%22%2C%22vsyjh%22%3A%221004%22%2C%22yys%22%3A%22SCR%2DCJ%22%2C%22fphm%22%3A%22577026%22%2C%22rqsj%22%3A%222016%2D04%2D19 16%3A04%3A12%22%7D%2C%22salegoods%22%3A%5B%7B%22rowno%22%3A1%2C%22code%22%3A%22244631%22%2C%22name%22%3A%22怡宝纯净水350ml%22%2C%22lsj%22%3A1%2E3000%2C%22price%22%3A1%2E3000%2C%94total%94%3A1%2E3000%2C%94plutype%94%3A%940%94%22sl%22%3A1%2E0000%2C%22lszke%22%3A0%2E0000%2C%94inputbarcode%94%3A%942%94%7D%5D%2C%22salepayments%22%3A%5B%7B%22rowno%22%3A1%2C%22paycode%22%3A%220101%22%2C%22name%22%3A%22现金%22%2C%22rqsj%22%3A%222016%2D04%2D19 16%3A04%3A12%22%2C%22consumers%5Fid%22%3A%22%22%2C%22total%22%3A1%2E3000%2C%22totalfrac%22%3A0%2E0000%2C%22account%22%3A%22%22%2C%22reference%22%3A%22%22%2C%22terminalid%22%3A%22%22%2C%22merchantid%22%3A%22%22%2C%22batchno%22%3A%22%22%2C%22trace%22%3A%22%22%7D%5D%7D

转译符号大全我会在后面给大家贴出来。

方法二

直接用

path = path + "?key="+URLEncoder.encode(map.get("key").toString(),"UTF-8");//path这里是请求路径
URLEncoder.encode(map.get("key").toString(),"UTF-8");//这个方法会自动转译我们的参数特殊符号
该类:
 public final class URLEncoder {
    private static BitSet dontNeedEncoding = new BitSet(256);
    private static final int caseDiff = 32;

    private URLEncoder() {
    }

    public static String encode(String var0, String var1) throws UnsupportedEncodingException {
        boolean var2 = false;
        boolean var3 = false;
        byte var4 = 10;
        StringBuffer var5 = new StringBuffer(var0.length());
        ByteArrayOutputStream var6 = new ByteArrayOutputStream(var4);
        OutputStreamWriter var7 = new OutputStreamWriter(var6, var1);

        for(int var8 = 0; var8 < var0.length(); ++var8) {
            char var9 = var0.charAt(var8);
            if (dontNeedEncoding.get(var9)) {
                if (var9 == \' \') {
                    var9 = \'+\';
                    var2 = true;
                }

                var5.append((char)var9);
                var3 = true;
            } else {
                try {
                    if (var3) {
                        var7 = new OutputStreamWriter(var6, var1);
                        var3 = false;
                    }

                    var7.write(var9);
                    if (var9 >= \'\ud800\' && var9 <= \'\udbff\' && var8 + 1 < var0.length()) {
                        char var10 = var0.charAt(var8 + 1);
                        if (var10 >= \'\udc00\' && var10 <= \'\udfff\') {
                            var7.write(var10);
                            ++var8;
                        }
                    }

                    var7.flush();
                } catch (IOException var13) {
                    var6.reset();
                    continue;
                }

                byte[] var14 = var6.toByteArray();

                for(int var11 = 0; var11 < var14.length; ++var11) {
                    var5.append(\'%\');
                    char var12 = Character.forDigit(var14[var11] >> 4 & 15, 16);
                    if (Character.isLetter(var12)) {
                        var12 = (char)(var12 - 32);
                    }

                    var5.append(var12);
                    var12 = Character.forDigit(var14[var11] & 15, 16);
                    if (Character.isLetter(var12)) {
                        var12 = (char)(var12 - 32);
                    }

                    var5.append(var12);
                }

                var6.reset();
                var2 = true;
            }
        }

        return var2 ? var5.toString() : var0;
    }

    static {
        int var0;
        for(var0 = 97; var0 <= 122; ++var0) {
            dontNeedEncoding.set(var0);
        }

        for(var0 = 65; var0 <= 90; ++var0) {
            dontNeedEncoding.set(var0);
        }

        for(var0 = 48; var0 <= 57; ++var0) {
            dontNeedEncoding.set(var0);
        }

        dontNeedEncoding.set(32);
        dontNeedEncoding.set(45);
        dontNeedEncoding.set(95);
        dontNeedEncoding.set(46);
        dontNeedEncoding.set(42);
    }
}
//--------------------------------------------------------------------------------转译类

 

续接

续接