I am hitting one URL having a plus symbol in it. A sample of the URL is given below:
https://api.digitalvault.cloud/<someText>/<someText>/samples?v=2.1&<someText>&startTime=2018-06-14T19:54:34%2b08:00&endTime=2018-06-14T01:54:34%2b08:00.
But it is not taking %2b symbol which is for Plus sign. Below is my code:
RestAssured.given()
.config(RestAssured.config().sslConfig(new SSLConfig().relaxedHTTPSValidation()))
.header("Authorization", Authorization).header(HeaderParameter1, HeaderParameterValue1)
.header(HeaderParameter2, HeaderParameterValue2).get(URI);
Where URI is the actual url.
Could someone please suggest the way to handle this?
Try something like this:
String result = java.net.URLDecoder.decode(url, "UTF-8");
Example:
String url = "https://api.digitalvault.cloud/<someText>/<someText>/samples?v=2.1&<someText>&startTime=2018-06-14T19:54:34%2b08:00&endTime=2018-06-14T01:54:34%2b08:00";
System.out.println("Before ENCODING: "+url);
String result = java.net.URLDecoder.decode(url, "UTF-8");
System.out.println("After ENCODING: "+result);
Related
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);
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);
Hi am sending a url using apache HttpClient by using following code but it has been showing a exception :java.net.URISyntaxException:
Illegal character in query at index 70: http://192.155.2.144:8080/SDAX/homePage.do?actionFlag=istrict&&MSG=1|Bdrtfggf|254td|return|null|null|null
Please help me where iam doing the problem. the following code i am sending a URL
String MSG="1|Bdrtfggf|254td|return|null|null|null" ;
String url="http://192.168.2.144:8080/SDAX/homePage.do?actionFlag=edistrict&&MSG="+MSG;
System.out.println("Url is"+url);
//String url = "http://192.168.0.6:8084/NRC_NEW_SEARCH/getVillageList.req?dist_id=1";
//String url="http://192.168.0.85:8080/poly/web/";
//FacesContext.getCurrentInstance().getExternalContext().redirect(url);
//ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
//context..redirect(url);
HttpRequestBase request = new HttpGet(url);
/*HttpParams params = new BasicHttpParams();
params.setParameter("dist_id", "1");
request.setParams(params);*/
HttpClient httpClient = new DefaultHttpClient();
httpClient.execute(request);
You should encode the MSG string before creating a URL from it.
String encodedMSG = URLEncoder.encode(MSG, "UTF-8")
String url="http://192.168.2.144:8080/SDAX/homePage.do?actionFlag=edistrict&&MSG="+ encodedMSG;
Edit
There won't be any problem in retrieving the data after encoding. If you have programmed this servlet homePage.do then you should use URLDecoder.decode() method in it.
Since vertical bar(|) is not a valid URI character thats why You are getting URISyntaxException.
Solution:
As suggested by kaysush, you need to encode/decode your url.
For more on this, Please check folowing url:
Cannot process url with vertical/pipe bar
You havn't explained what you are trying to achieve here. I hope it isn't by mistake. but
As per you question, You are trying to post a url and In your code you are using HttpGet(url);
im trying to send an email with an attachment, but it keeps saying:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Illegal character in opaque part at index 64: mailto:recipient#mailserver.com?subject=ThePDFFile&attachment=C:\Users\Rascal\AppData\Local\Temp\FreelancerList-16-12-2014_09-227568200505392670736.doc
Java Code:
Desktop desktop = Desktop.getDesktop();
String message = "mailto:recipient#mailserver.com?subject=ThePDFFile&attachment=\""+path;
URLEncoder.encode(message, "UTF-8");
URI uri = URI.create(message);
desktop.mail(uri);
Should be the colon right? But why???
You're calling URLEncoder.encode, but ignoring the result. I suspect you were trying to achieve something like this:
String encoded = URLEncoder.encode(message, "UTF-8");
URI uri = URI.create(encoded);
... although at that point you'll have encoded the colon after the mailto part as well. I suspect you really want something like:
String query = "subject=ThePDFFile&attachment=\""+path;
String prefix = "mailto:recipient#mailserver.com?";
URI uri = URI.create(prefix + URLEncoder.encode(query, "UTF-8"));
Or even encoding just the values:
String query = "subject=" + URLEncoder.encode(subject, "UTF-8");
+ "&attachment=" + URLEncoder.encode(path, "UTF-8"));
URI uri = URI.create("mailto:recipient#mailserver.com?" + query);
... or create the URI from the various different parts separately, of course.
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