URLDecoder: Illegal hex characters in escape (%) pattern - For input string:

来自:http://stackoverflow.com/questions/11257509/urldecoder-illegal-hex-characters-in-escape-pattern-for-input-string-p

Whoever created the URL should have percent encoded the % by writing %25.

Example invalid URL

http://example.com/test?q=%.P

Example valid URL

http://example.com/test?q=%25.P

The answer provided by Mark Byers will work just fine if there‘re only % chars that need to be escaped but will fail if url contains percent-encoded chars. To avoid this there‘s a little bit more work needed.

In percent-encoding (url-encoding) only reserved and unreserved chars won‘t be percent-encoded.

Reserved chars:
╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗
║ ! ║ # ║ $ ║ & ║ ‘ ║ ( ║ ) ║ * ║ + ║ , ║ / ║ : ║ ; ║ = ║ ? ║ @ ║ [ ║ ] ║
╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝

Unreserved chars:
╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗
║ A ║ B ║ C ║ D ║ E ║ F ║ G ║ H ║ I ║ J ║ K ║ L ║ M ║ N ║ O ║ P ║ Q ║ R ║
╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝
╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗
║ S ║ T ║ U ║ V ║ W ║ X ║ Y ║ Z ║ a ║ b ║ c ║ d ║ e ║ f ║ g ║ h ║ i ║ j ║
╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝
╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗
║ k ║ l ║ m ║ n ║ o ║ p ║ q ║ r ║ s ║ t ║ u ║ v ║ w ║ x ║ y ║ z ║
╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝
╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗
║ 0 ║ 1 ║ 2 ║ 3 ║ 4 ║ 5 ║ 6 ║ 7 ║ 8 ║ 9 ║ - ║ _ ║ . ║ ~ ║
╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝

According to RFC 3986 percent-encoded character has following format: % + hex. So if you want to properly escape url that has unescaped % chars without breaking the whole url before actually decoding it, you need to replace only those % signs that are not followed by hex.

Finding substring that violates some pattern is pretty easy task with regex. In this case pattern will look like this:

%(?![0-9a-fA-F]{2})


Sample:

class Main
{
    public static void main (String[] args) throws java.lang.Exception
    {
        String url = "http://example.com/test?q=%.P%20some%20other%20Text";
        url = url.replaceAll("%(?![0-9a-fA-F]{2})", "%25");
        System.out.println(url);
    }
}
时间: 2024-08-22 03:00:24

URLDecoder: Illegal hex characters in escape (%) pattern - For input string:的相关文章

【Java】Java URLDecoder异常Illegal hex characters in escape (%)

如果收到的HTTP请求参数(URL中的GET请求)中有一个字符串,是中文,比如“10%是黄段子”,服务器段使用URLDecoder.decode就会出现此异常.URL只能使用英文字母.阿拉伯数字和某些标点符号,不能使用其他文字和符号.如果内容中存在中文,必须要进行编解码.“10%是黄段子”转码过后是“10%25%E6%98%AF%E9%BB%84%E6%AE%B5%E5%AD%90%”被用来作为转义字符使用. 上面的字符串中'%'是一个中文字符'是',而转换的实现是将%后面的两个字符一起转为一个

URLDecoder: Incomplete trailing escape (%) pattern

在使用URLDecoder对字符串进行解码的时候 报以下异常信息: Exception in thread "main" java.lang.IllegalArgumentException: URLDecoder: Incomplete trailing escape (%) pattern 原因是字符串中包含有%字符 解决方法如下 example: String str = "hello该字符串中包含%"; System.out.println(URLDecod

URLDecoder: Incomplete trailing escape (%) pattern问题处理

http://blog.csdn.net/yangbobo1992/article/details/10076335 ________________________________________________________ 最近在用的项目中,分页页面在导出excel抛出 java.lang.IllegalArgumentException: URLDecoder: Incomplete trailing escape (%) pattern 该页面采用的是DWR分页,经过一番搜罗,终于修

iconv() : Detected an illegal character in input string 报错解决(抄的)

HP传给JS字符串用ecsape转换加到url里,又用PHP接收,再用网上找的unscape函数转换一下,这样得到的字符串是UTF-8的,但我需要的是GB2312,于是用iconv转换 开始是这样用的 $str = iconv('UTF-8', 'GB2312', unescape(isset($_GET['str'])? $_GET['str']:'')); 上线后报一堆这样的错:iconv() : Detected an illegal character in input string 考

LeetCode(Easy)--C++笔记

前言:这是关于LeetCode上面练习题C++的笔记,有些地方参考有网友的解题方法(可能有些参考没能注明,望谅解),如有需要改进的地方希望留言指教,多谢! 目录: ZigZag Conversion Reverse digits of an integer Implement atoi to convert a string to an integer Determine whether an integer is a palindrome Write a function to find th

leetcode笔记—String

3. Longest Substring Without Repeating Characters (medium) 最长的无重复的子字符串 Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given

leetcode栈--5、valid-parentheses(有效括号)

题目描述 Given a string containing just the characters'(',')','{','}','['and']', determine if the input string is valid. The brackets must close in the correct order,"()"and"()[]{}"are all valid but"(]"and"([)]"are not.

每日刷题总结

2017-5-5 https://leetcode.com/problems/valid-parentheses/#/description 题目:Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" a

20. Valid Parentheses

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]"