Java: to use URLDecoder but reserve the plus sign(+) - java

I'm writing a lucene server. I want to receive the post query like :
http://www.site.com/search?+title:google +type:website
but the post argument "+title:google +type:website" is encoded like this: "+title:google%20+type:website"
so I use URLDecoder.decode(argument,"UTF-8") to get the original input, but I get the wrong result:
" title:goole type:website", because the URLDecoder convert the plus sign "+" into a space character " ". What can I do to get the decode argument without converting plus sign?

You can try this:
"+title:google%20+type:website".replaceAll("\\+", "%2b")
It will replace all plus signs and after that you use the decoder, which will convert back the plus sign

Actually All spaces will be replaced by %20 , so u can replace all the %20 to space once you get the URL string.

Related

Java URI Builder build method replacing "?" character in the path

I have an API at the following path
/v0/segments/ch/abc/view/status/ACTIVE?sc=%s&expiryGteInMs=%d
I am building a Client using the URIBuilder in Java.
return UriBuilder
.fromUri(config.getHost())
.path(String.format(config.getPath(),request.List(), request.getTime()))
.build();
The request contains a list to be substituted in place of %s and the time to be substituted in place of %d. But the request being formed has a path like this
/v0/segments/ch/abc/view/status/ACTIVE%3Fsc=FK,GR&expiryGteInMs=1611081000000
Basically the "?" character is being replaced by %3F. Can somebody help me with this, please?
P.S: I know that we can use the ".queryParam" option available in URIBuilder. Looking for the actual reason why this is happening?
Most probably library that you are using is encoding url, and ? encodes to %3F.
Why this happens(in short): url could contain only specific set of character, and ? is not one of them, so, in order to transfer this character, we should encode it (so called Percent-encoding).
A bit longer explanation (taken from here):
URL encoding converts characters into a format that can be transmitted over the Internet.
URLs can only be sent over the Internet using the ASCII character-set.
Since URLs often contain characters outside the ASCII set, the URL has to be converted into a valid ASCII format.
URL encoding replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits.
URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with %20.

Request.getParameter is wrongly decoding the string

I have the URL with the following query string
equipmentAccessoryRoute=LFVR+BASICACC
When I do request.getParameter("equipmentAccessoryRoute") it returns 'LFVR BASICACC' in a string variable, replacing plus sign with space.
To resolve this issue I did something like this
String accessoryRoute = java.net.URLEncoder.encode(request.getParameter("equipmentAccessoryRoute"),"UTF-8");
It worked perfectly but now yt is not working for the following query string (which was working before)
`equipmentAccessoryRoute=C1000IP5EL#-A`
Decoding converts this into 'C1000IP5EL%40-A' and stores into a string.
I am really confused. I tried to learn URL encoding but find it very hard to understand.
URL - Uniform Resource Locator
URLs can only be sent over the Internet using the ASCII
character-set. Since URLs often contain characters outside the ASCII
set, the URL has to be converted into a valid ASCII format.
URL encoding replaces unsafe ASCII characters with a "%" followed by
two hexadecimal digits.
URLs cannot contain spaces. URL encoding normally replaces a space
with a plus (+) sign or with %20.
Hope this helps.

Groovy: convert JSON array to URLencoded string?

I am writing an app for SmartThings (www.smartthings.com) in their own IDE. I have an input field here that is supposed to be text input. I ask for a departure address:
section("Departing From:"){
input "departFrom", "text", title: "Address?"
}
when putting in the value of Monterey, CA the value magically gets changed to a JSON Array with the values of [Monterey, CA]
I want to pass this value to an httpGET statement but I need to URLencode it first to omit spaces, etc.I have tried URLencoder with no success due to the JSON array.
I have tried join(",") with no luck as it adds double quotes to the value.
How can I get a clean Monterey%2C%20CA URL encoded value from this variable?
** bear in mind someone could input any combination of numbers, spaces, and commas into this input as an address. The mapquest API I am sending it to can handle all these things as long as they dont have special characters and spaces are URL encoded.
Maybe try:
def l = ['Monterey', 'CA']
assert URLEncoder.encode(l.join(', ')).replaceAll('\\+','%20') == 'Monterey%2C%20CA'
When it comes to replacing + sign, please see here
There are different types of URL encoding, but in this case there are two: One that converts spaces to %20 and one that converts spaces to +.
For the first, you'd use UriUtils:
def yourEncodedString = UriUtils.encodeUri(yourString.toString(), "UTF-8")
For the second, you'd use UrlEncoder:
def yourEncodedString = URLEncoder.encode(yourString.toString(), "UTF-8")
Alternatively (I think) you can use URLEncoder with UTF-16 to get what you want.
I've never had a fun time with UriUtils, so hopefully UrlEncoder will work for you.

Is it possible to have a name collision with URLEncoder

I am using java's URLEncoder to take a user provided string and create a string that is safe to use for filenames. What I'm wondering is it possible for two different strings to be encoded to the same value.
For example, if one string is "ABC%20D" but since % is used as a character to replace special characters is it possible that something like "ABC D" and "ABC%20D" both end up as the same encoded value? Or will the encoder always replace characters like % with something else?
It seems to encode escape characters using your example input:
String result = URLEncoder.encode("ABC%20D", "UTF-8");
System.out.println(result); //prints ABC%2520D

Android- remove URL percent symbols from string

I have a URL that looks like this:
Liberty%21%20ft.%20Whiskey%20Pete%20-%20Thunderfist%20%28Original%20Mix%29.mp3
I'm trying to extract just the words from it. Right now, I'm using string.replace("%21", "!") for each and every %20, %29, etc. because each segment represent different characters or spaces. Is there a way to just covert those symbols and numbers to what they actually mean?
Thanks.
Those symbols are URLEncoded representations of characters that can't legally exist in a URL. (%20 = a single space, etc)
You need to UrlDecode those strings:
http://icfun.blogspot.com/2009/08/java-urlencode-and-urldecode-options.html
Official documentation here:
http://download.oracle.com/javase/6/docs/api/java/net/URLDecoder.html
It seems the input string is written using the URL encoding. Instead of writing all possible replacements manually (you can hardly cover all possibilities), you can use URLDecoder class in Java.
String input = "Liberty%21%20ft.%20Whiskey%20Pete...";
String decoded = URLDecoder.decode(input, "UTF-8");

Categories