I have problem using UrlEncoder and UrlDecoder.
It looks like this:
Example of token:
3vv3XIvofg3KIoMjLwU07329C6dsk8HJceuDT2F5jOwox2hyqAnL+03TPej/lW4TCeFWRadRkPKgW0aGxq+9B1VZLMvoevyFfaVXhvzIyLF8AN3NDCqk0hoqb51wlGtb4hUvOYKq5b63wuW2pfssr9O0dgCEK4VZz8QZ4jRpxZw=
I set token on Customer in my Spring Application. Then I encode token to use it in url:
String token = // generated by mechanism
String encodedToken = UrlEncoder.encode(token, "UTF-8");
String url = "https://myapp.url?token=" + encodedToken;
I receive token as a #RequestParam. Then the token is decoded by UrlDecoder
String decodedToken = UrlDecoder.decode(token, "UTF-8");
The problem is as follows:
Sometimes it works correctly and I am able to find user by token, but sometimes I have error, because decoded token is invalid, and it looks different than token. What is the problem? It is strange because sometimes it works, sometimes not
Your token will already be decoded by Spring because of the #RequestParam annotation.
If the token contains a +, the second decoding will replace the + with a (white space).
I am assuming you are using java.net.URLEncoder and java.net.URLDecoder? As the Javadoc of the encode(String, String) and decode(String, String) methods describes, they encode to and decode from the application/x-www-form-urlencoded format. In this format, the + is a special character replacing a space.
So a + will be encoded to %2B and decoded to . Because Spring will already have taken care of the decoding, the %2B will be a + again. Your second decoding translates it to a and the tokens won't match anymore.
Related
I have following code:
String convertedBuilder = builder.toUriString();
convertedBuilder = convertedBuilder.replace(",", "\\,");
URI uri = UriComponentsBuilder.fromUriString(convertedBuilder)
.build().toUri();
The idea is to replace a single comma ',' by a '\,' (slash and comma).
Originated URL should be something like
'server-url'?name=te%5C%2Cst for parameter value 'te\,st.
However Spring generates this one:
'server-url'?name=te%5C%252Cst
What am I doing wrong?
Regards,
I had a similar requirement where I had to call a service with a GPS coordinate like this:
http://example.com/path?param1=40.678806%2C-73.943244
UriComponentsBuilder alone would either:
not encode the , (comma) within the parameter value
encode the encoded comma if I already had encoded the parameter with URLEncoder, thus producing the value 40.678806%252C-73.943244
I resolved by explicitly encoding the query parameter with URLEncoder and telling UriComponentsBuilder that the URL was already encoded:
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://example.com/path")
.queryParam("param1" , URLEncoder.encode("40.678806,-73.943244", "UTF-8"));
ResponseEntity<String> resp = template.exchange(
builder.build(true).toUri(),
HttpMethod.GET,
null, new ParameterizedTypeReference<String> () {}
);
How to encode comma in Spring UriComponentsBuilder?
UriComponentsBuilder doesn't encode the comma since RFC doesn't require it to be encoded. You will have to encode it manually as suggested by Andreas in the comments.
However Spring generates this one:
'server-url'?name=te%5C%252Cst
No, it doesn't, as also mentioned by Andreas. It encodes the backslash but not the comma. Please fix the question if you need any more information.
I need to replace the spaces inside a string with the % symbol but I'm having some issues, what I tried is:
imageUrl = imageUrl.replace(' ', "%20");
But It gives me an error in the replace function.
Then:
imageUrl = imageUrl.replace(' ', "%%20");
But It still gives me an error in the replace function.
The I tried with the unicode symbol:
imageUrl = imageUrl.replace(' ', (char) U+0025 + "20");
But it still gives error.
Is there an easy way to do it?
String.replace(String, String) is the method you want.
replace
imageUrl.replace(' ', "%");
with
imageUrl.replace(" ", "%");
System.out.println("This is working".replace(" ", "%"));
I suggest you to use a URL Encoder for Encoding Strings in java.
String searchQuery = "list of banks in the world";
String url = "http://mypage.com/pages?q=" + URLEncoder.encode(searchQuery, "UTF-8");
I've ran into issues like this in the past with certain frameworks. I don't have enough of your code to know for sure, but what might be happening is whatever http framework you are using, in my case it was spring, is encoding the URL again. I spent a few days trying to solve a similar problem where I thought that string replace and the URI.builder() was broken. What ended up being the problem was that my http framework had taken my encoded url, and encoded it again. that means that any place it saw a "%20", it would see the '%' charictor and switch it out for '%' http code, "%25", resulting in. "%2520". The request would then fail because %2520 didn't translate into the space my server was expecting. While the issue apeared to be one of my encoding not working, it was really an issue of encoding too many times. I have an example from some working code in one of my projects below
//the Url of the server
String fullUrl = "http://myapiserver.com/path/";
//The parameter to append. contains a space that will need to be encoded
String param 1 = "parameter 1"
//Use Uri.Builder to append parameter
Uri.Builder uriBuilder = Uri.parse(fullUrl).buildUpon();
uriBuilder.appendQueryParameter("parameter1",param1);
/* Below is where it is important to understand how your
http framework handles unencoded url. In my case, which is Spring
framework, the urls are encoded when performing requests.
The result is that a url that is already encoded will be
encoded twice. For instance, if you're url is
"http://myapiserver.com/path?parameter1=param 1"
and it needs to be read by the server as
"http://myapiserver.com/path?parameter1=param%201"
it makes sense to encode the url using URI.builder().append, or any valid
solutions listed in other posts. However, If the framework is already
encoding your url, then it is likely to run into the issue where you
accidently encode the url twice: Once when you are preparing the URL to be
sent, and once again when you are sending the message through the framework.
this results in sending a url that looks like
"http://myapiserver.com/path?parameter1=param%25201"
where the '%' in "%20" was replaced with "%25", http's representation of '%'
when what you wanted was
"http://myapiserver.com/path?parameter1=param%201"
this can be a difficult bug to squash because you can copy the url in the
debugger prior to it being sent and paste it into a tool like fiddler and
have the fiddler request work but the program request fail.
since my http framework was already encoding the urls, I had to unencode the
urls after appending the parameters so they would only be encoded once.
I'm not saying it's the most gracefull solution, but the code works.
*/
String finalUrl = uriBuilder.build().toString().replace("%2F","/")
.replace("%3A", ":").replace("%20", " ");
//Call the server and ask for the menu. the Menu is saved to a string
//rest.GET() uses spring framework. The url is encoded again as
part of the framework.
menuStringFromIoms = rest.GET(finalUrl);
There is likely a more graceful way to keep a url from encoding twice. I hope this example helps point you on the right direction or eliminate a possability. Good luck.
Try this:
imageUrl = imageUrl.replaceAll(" ", "%20");
Replace spaces is not enought, try this
url = java.net.URLEncoder.encode(url, "UTF-8");
I have problems with the character +(and maybe others) at the URIBuilder is suppose to get a decoded url but when I extract the query the + is replaced
String decodedUrl = "www.foo.com?sign=AZrhQaTRSiys5GZtlwZ+H3qUyIY=&more=boo";
URIBuilder builder = new URIBuilder(decodedUrl);
List<NameValuePair> params = builder.getQueryParams();
String sign = params.get(0).getValue();
the value of sing is AZrhQaTRSiys5GZtlwZ H3qUyIY= with a space instead +. How can I extract the correct value?
other way is:
URI uri = new URI(decodedUrl);
String query = uri.getQuery();
the value of query is sign=AZrhQaTRSiys5GZtlwZ+H3qUyIY=&more=boo in this case is correct, but I have to strip it. Is there another way to do that?
Use it differently:
String decodedUrl = "www.foo.com";
URIBuilder builder = new URIBuilder(decodedUrl);
builder.addParameter("sign", "AZrhQaTRSiys5GZtlwZ+H3qUyIY=");
builder.addParameter("more", "boo");
List<NameValuePair> params = builder.getQueryParams();
String sign = params.get(0).getValue();
addParameter method is responsible for adding parameters as to the builder. The constructor of the builder should include the base URL only.
If this URL is given to you as is, then the + is already decoded and stands for the space character. If you are the one who generates this URL, you probably skipped the URL encoding step (which can be done using the code snipped above).
Read a bit about URL encoding: http://en.wikipedia.org/wiki/Query_string#URL_encoding
That is because if you send space as parameter in url it is encoded as +. This happens because there are some rules which characters are valid in URL. See URL RFC.
It is necessary to encode any characters disallowed in a URL, including spaces and other binary data not in the allowed character set, using the standard convention of the "%" character followed by two hexadecimal digits.
If you want to have + as symbol in url you need to encode it into %2B. For example 2+2 is encoded as 2%2B2 and i am as i+am. So in your case I believe you have to correct result as AZrhQaTRSiys5GZtlwZ+H3qUyIY decodes into AZrhQaTRSiys5GZtlwZ H3qUyIY.
How can i configure RestTemplate (Springframework) to encode using percent-encoding rather characters encoding, for example i am posting this parameters to a server:
client_id=xxx
client_secret=xxx
grant_type=client_credentials
scope=public_read registration
but when posting, spring send it as:
client_id=xxx&client_secret=xxx&grant_type=client_credentials&scope=public_read+registration
and i want it to be like that:
client_id=xxx&client_secret=xxx&grant_type=client_credentials&scope=public_read%20registration
it converts spaces to + and i want it to be %20
thx
You can use this:
String formated_urlString = URLEncoder.encode(unformated_url_string, "utf-8").replace("+", "%20").
Also see
URLEncoder not able to translate space character
I have passes a encoded SSO token in URL...so the URL looks like
http://127.0.0.1:7101/CRMOD_UCM-Sankalp-context-root/BrowseFile.jsp?token=%246%24zhxjx%2fpz6dVucl9cgG43ii2Tr4qVnNbeqJg8jCy6Jj7vRvXN4%3d%3b%246%24GlRGp%2fxfEM308NZGmY%2fhjHav2yjHSvbww1l0%2fCcCtcVjzl%2bCQFlQPdBRKO0t1XUmF0I6xLmfQ%2fnb7VgJeSYnvrAb9YUQQ3tvr%2fBZ%2bIRZiBAGU2%2fZg%3d
but when i retreive the value of variable and print it using
out.println("SSO Token:"+request.getParameter("token"));
it prints
$6$zhxjx/pz6dVucl9cgG43ii2Tr4qVnNbeqJg8jCy6Jj7vRvXN4=;$6$GlRGp/xfEM308NZGmY/hjHav2yjHSvbww1l0/CcCtcVjzl+CQFlQPdBRKO0t1XUmF0I6xLmfQ/nb7VgJeSYnvrAb9YUQQ3tvr/BZ+IRZiBAGU2/Zg=
The correct token is which is present in the URL, WHY i am getting such a different value in my print...
Kindly help......
request.getParameter("token") decodes the parameter value, so the value that gets printed is the decoded version of the parameter value in the URL.
See Java EE, accent in url, request.getParameter wrong value for more information.
In the URL, the token is URL encoded. This is necessary because some characters have special meanings in an URL.
By URL encoding the value, those special characters are replaced by %nn codes where nn is the character code of the special character.
Java automatically decodes it for you, so that you get the actual value when you call request.getParameter("token") instead of the URL-encoded value.
You are getting encoded value if you want decode then use following code. By default system decode for us but in your case it is not decoded and you must explicitly decode value of request.getParameter("token").
String token = request.getParameter("token").toString();
// To decode url
String decodedtoken = URLDecoder.decode(token , "UTF-8");
System.out.println("Decoded token value "+ decodedtoken);
// To encode url
String encodedtoken = URLEncoder.encode(token , "UTF-8");
System.out.println("Encoded token value "+ encodedtoken);