A simple question from a Java newbie. I want to encode the URL so that non-standard characters are converted to their hexadecimal value (i.e.% XX), while the characters that everyone expects to see in the letter-letter, numbers, slashes, question marks, etc. Will be left untouched.
For example, coding
"hi/hello?who=moris\\boris"
should appear with
"hi/hello?who=moris%5cboris"
ideas?
This is actually a rather complicated problem. And the reason this is complicated is because different parts of the URL need to be processed (encoded) differently.
, - URL- URL URI, .
, , , . , () "?" ( ) (escape it).
OWASP Enterprise Security API .
, . http://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet#RULE_.235_-_URL_Escape_Before_Inserting_Untrusted_Data_into_HTML_URL_Parameter_Values
http://code.google.com/p/owasp-esapi-java/source/browse/trunk/src/main/java/org/owasp/esapi/codecs/PercentCodec.java
, URL-. URL
public static String escapeSpecialCharacters(String input) { StringBuilder resultStr = new StringBuilder(); for (char ch : input.toCharArray()) { if (isSafe(ch)) { resultStr.append(ch); } else{ resultStr.append('%'); resultStr.append(toHex(ch / 16)); resultStr.append(toHex(ch % 16)); } } return resultStr.toString(); } private static char toHex(int ch) { return (char) (ch < 10 ? '0' + ch : 'A' + ch - 10); } private static boolean isSafe(char ch) { return ((ch>='A' && ch<='Z') || (ch>='a' && ch<='z') || (ch>='0' && ch<='9') || "-_.~".indexOf(ch)>=0); }
URLEncoder.encode(url, "UTF-8"), . Javadoc.
URLEncoder.encode(url, "UTF-8")
org.apache.commons.codec.net.URLCodec (, \, ). , , URL-, . , ? = , , .
spring UriUtils., , / URL- URL-.
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/util/UriUtils.html
Source: https://habr.com/ru/post/1792579/More articles:WPF Calendar: Configuring a control to display calendar numbers - wpfElementTree XPath weird behavior - pythonreturn the first n letters of a column - sqliPhone encryption with certificate - securityGoogle Analytics API - Choosing a Metric Value Affects Dimension Values? - google-analyticsSQL Server group in a bitwise way? - sqlHow to select unique XML nodes using Ruby? - ruby | fooobar.comClearCanvas DicomFile.DataSet - Как добавить новый тег? - dicomEditing XML with PowerShell and the "file format" error - xmlJQuery GET request on Sinatra always results in an error - jqueryAll Articles