I am using below code to eliminate the special characters from URL:
String url1 = "https://dev/ABC/v1/XYZ?itemnumber%255Bin%255D=%255B3001%252C3005%252C202%255D&limit=2&apikey=4zVYEk2Xg8zvwYxNnW&offset=2";
String decodedURL = URLDecoder.decode(url1, "UTF-8");
System.out.println(decodedURL);
Expected output:
https://dev/ABC/v1/XYZ?itemnumber[in]=[3001,3005,20]&limit=2&offset=1&apikey=4zVYEk2Xg8zvwYxNnW
Error output:
https://dev/ABC/v1/XYZ?itemnumber%5Bin%5D=%5B3001%2C3005%2C202%5D&limit=2&apikey=4zVYEk2Xg8zvwYxNnW&offset=1
Your string is double-URL encoded, see https://ideone.com/CQQbPz:
String url1 = "https://dev/ABC/v1/XYZ?itemnumber%255Bin%255D=%255B3001%252C3005%252C202%255D&limit=2&apikey=4zVYEk2Xg8zvwYxNnW&offset=2";
System.out.println(URLDecoder.decode(url1, "UTF-8"));
System.out.println(URLDecoder.decode(URLDecoder.decode(url1, "UTF-8"), "UTF-8"));
Output:
https://dev/ABC/v1/XYZ?itemnumber%5Bin%5D=%5B3001%2C3005%2C202%5D&limit=2&apikey=4zVYEk2Xg8zvwYxNnW&offset=2
https://dev/ABC/v1/XYZ?itemnumber[in]=[3001,3005,202]&limit=2&apikey=4zVYEk2Xg8zvwYxNnW&offset=2
Browsers and many other http programs convert illegitimate url request symbols to URL encoding scheme that place a % percent sign in front of two numerals. Before use, use
String decoded = java.net.URLDecoder.decode(request);
Related
In my url there is query part. in Query I pass %26.
Example
"http://host.com/api/1/searcharticles?key=XXXXXX&query=(subject:"Polls %26 Surveys")"
If I run same URL in Postman or Browser then it returns me 21 results but if I run in the RestTemplate then it returns 0 results.
I believe API not able to identify %26.
I tried the same URL in the Python also and it is returning 21 results.
Sample code
url = "http://host.com/api/1/searcharticles?key=XXXXXX&query=(subject:"Polls %26 Surveys")";
byte[] gzipResponse = restTemplate.exchange(metabaseUrl, HttpMethod.GET, entity, byte[].class).getBody();
String string = new String(gzipResponse);
System.out.println(string);
Do not just use %26 for & while using RestTemplate try to use URLEncoder
URLEncoder.encode(your url string, "UTF-8")
Well 2 things
First:
Your url variable doesn't look correct. It should be
url = "http://host.com/api/1/searcharticles?key=XXXXXX&query=(subject:\"Polls %26 Surveys\")";
Second:
Your URL is encoded so you need to decode it before sending like this
String url = URLDecoder.decode("Polls %26 Surveys", "UTF-8");
System.out.println(url);
OUTPUT:
Polls & Surveys
Did you try like this, you don't need to use %26 as long as you encode your url before using.
url = "http://host.com/api/1/searcharticles?key=XXXXXX&query=(subject:\"Polls & Surveys\")";
url = URLEncoder.encode(url, "UTF-8");
byte[] gzipResponse = restTemplate.exchange(url, HttpMethod.GET, entity, byte[].class).getBody();
String string = new String(gzipResponse);
System.out.println(string);
String urlParameters = "login=test&password=te&ff";
I have a String urlParams, & - is part of the password, how to make it escaped, thus not be recognized as a separator?
Use a URL Encoder on each of the components: http://docs.oracle.com/javase/7/docs/api/java/net/URLEncoder.html
Encode your password using URLEcoder#encode with utf-8
String urlParameters = "login=test&password="+
URLEncoder.encode("te&ff", "UTF-8");
I hope to encode a string to a url, but URLEncoder.encode() cannot do it quite well:
URLEncoder.encode("http://www.example.com/1/hello world", "utf8")
will result in
http%3A%2F%2Fwww.example.com%2F1%2Fhello+world
What I hope to get is:
http://www.example.com/1/hello+world
without encoding the / and : characters.
EDIT
This is a just a simple example here, actually I have many non-ascii characters in the url.
you can convert "%3A" to ":" and convert "%2F" to "/" after encode. eg:
String ret = URLEncoder.encode("http://www.example.com/1/hello world", "utf8");
String ret2 = ret.replace("%3A", ":").replace("%2F", "/");
ret2 is what you want..
I get an error "Illegal character in URL" in my code and I don't know why:
I have a token and an hash that are string type.
String currentURL = "http://platform.shopyourway.com" +
"/products/get-by-tag?tagId=220431" +
"&token=" + token +
"&hash=" + hash;
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
try {
URL url = new URL(currentURL);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
[...]
but when I wrote :
URL url = new URL("http://platform.shopyourway.com/products/get-by-tag?tagId=220431&token=0_11800_253402300799_1_a9c1d19702ed3a5e873fd3b3bcae6f8e3f8b845c9686418768291042ad5709f1&hash=e68e41e4ea4ed16f4dbfb32668ed02b080bf1f2cbee64c2692ef510e7f7dc26b");
it's work, but I can't write this order because I don't know the hash and the token because I generate them every time.
thanks.
From the Oracle docs on creating URLs you need to escape the "values" of your URL string.
URL addresses with Special characters
Some URL addresses contain special characters, for example the space
character. Like this:
http://example.com/hello world/ To make these characters legal they
need to be encoded before passing them to the URL constructor.
URL url = new URL("http://example.com/hello%20world");
Encoding the special character(s) in this example is easy as there is
only one character that needs encoding, but for URL addresses that
have several of these characters or if you are unsure when writing
your code what URL addresses you will need to access, you can use the
multi-argument constructors of the java.net.URI class to automatically
take care of the encoding for you.
URI uri = new URI("http", "example.com", "/hello world/", "");
And then convert the URI to a URL.
URL url = uri.toURL();
As commented also see this other post that uses URLEncoder to replace any offending characters
How can I make String Стек look like %D0%A1%D1%82%D0%B5%D0%BA? Which encoding is it? How can I do it with Java? I thought it's UTF-8:
String myString = "Стек";
byte text[] = myString.getBytes();
String value = new String(text, "UTF-8");
System.out.println(value);
But no, I've got Стек in output.
It's not UTF-8, it's URL-like encoding, and you can get it using the URLEncoder class:
String encoded = URLEncoder.encode("Стек");
System.out.println(encoded);
Result:
%D0%A1%D1%82%D0%B5%D0%BA
IDEOne working example
The text that you've shown is Percent encoded or URL encoded.
You can use URLEncoder for converting it to the desired format:
String value = URLEncoder.encode("Стек", "ISO-8859-1");
You can use the URLEncoder class to convert a String to percent encoding:
import java.net.URLEncoder;
System.out.println(URLEncoder.encode("Стек", "utf-8"));
You'll also need to catch UnsupportedEncodingException.