Java how to escape "&" character in url params? - java

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");

Related

How to avoid special character from URL using java

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);

How to encode URL string without escape ':' and '/' characters using java?

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..

Illegal character in URL

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

Server side URL Encoding

How can we Sanitize, via URL/HTML encoding, potentially danger characters & (< > “ ; /) in the server side, i am using encodeURI() and escape() in jsp page to encode on client side and i used URLEncoder.encode() in my java file to handel server side but it not encoding.
String needsEncodingPart = "?!##$%^&*() <>/\"'[]{}\"";
String baseURL = "http://url:80/test";
String encodedPart = URLEncoder.encode(needsEncodingPart,"UTF-8").replace("+", "%20");
System.out.println(baseURL + "/" + encodedPart);
needs to be replaced with "%20" as URLEncoder basically works with HTML type of encoding which replaces spaces with +
2nd way is to use java.net.URI
URL url = new URL("http://url:80/test/test1?!##$%^&*() <>/\"'[]{}\"");
URI uri = null;
uri = new URI(url.getProtocol(), url.getHost() + ":" + url.getPort(), url.getPath(), url.getQuery(), null);
uri.toString() will return encoded url. But in case of # encountered it might create some issue in encoding.
Thanks,
Gaurav

Encoding troubles with Java

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.

Categories

Resources