判断URL是否编码,编码后的sign对签名和验签都有影响,导致验签不通过
判断URL是否编码,编码后的sign对签名和验签都有影响,导致验签不通过
如果含有 + %符号无法判断, 否则判断不准或报错 Exception in thread "main" java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in escape (%) pattern - For input string: "#&"
package com.example.core.mydemo.java; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; /** * url1=%3D%3D * decodedUrl=== * URL 1 is encoded: true * decodedUrl=== * URL 2 is encoded: false * url1=%2B+%2F%3F%25%23%26 * decodedUrl=+ /?%#& * URL 3 is encoded: true * decodedUrl= /?#& * URL 4 is encoded: false * decodedUrl= * URL 5 is encoded: true * URL 6 is encoded: true * java.lang.IllegalArgumentException: URLDecoder: Incomplete trailing escape (%) pattern * at java.net.URLDecoder.decode(URLDecoder.java:187) * at com.example.core.mydemo.java.URLUtils.isUrlEncoded(URLUtils.java:22) * at com.example.core.mydemo.java.URLUtils.main(URLUtils.java:56) * */ public class URLUtils { public static boolean isUrlEncoded(String url) { try { String decodedUrl = URLDecoder.decode(url, "UTF-8"); System.out.println("decodedUrl=" + decodedUrl); return !decodedUrl.equals(url); } catch (UnsupportedEncodingException e) { // 处理异常 e.printStackTrace(); return false; }catch (IllegalArgumentException ex){ ex.printStackTrace(); return true; } } public static void main(String[] args) throws UnsupportedEncodingException { String url1 = URLEncoder.encode("==","UTF-8"); System.out.println("url1=" + url1); String url2 = "=="; System.out.println("URL 1 is encoded: " + isUrlEncoded(url1)); System.out.println("URL 2 is encoded: " + isUrlEncoded(url2)); url1 = URLEncoder.encode("+ /?%#&","UTF-8"); System.out.println("url1=" + url1); url2 = " /?#&"; //去掉 + % 否则判断不准或报错 Exception in thread "main" java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in escape (%) pattern - For input string: "#&" System.out.println("URL 3 is encoded: " + isUrlEncoded(url1)); System.out.println("URL 4 is encoded: " + isUrlEncoded(url2)); url1 = "+"; url2 = "%"; System.out.println("URL 5 is encoded: " + isUrlEncoded(url1)); System.out.println("URL 6 is encoded: " + isUrlEncoded(url2)); } }