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.
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);
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);
I'm simply trying to do a HttpGet.
Here is the string that is being passed:
fullString = "?nOne=" + node1 + "&nTwo=" + node2 + "&nThree=" + node3 + "&nFour=" + node4 + "&power=" + power + "&color=" + colorRGB;
All the variables are a single integer except for color which is 9 digits.
That string is passed to a function doing the following:
String get_url = URLEncoder.encode("http://192.168.30.80/" + str, "UTF-8");
HttpClient Client = new DefaultHttpClient();
HttpGet httpget;
ResponseHandler<String> responseHandler = new BasicResponseHandler();
httpget = new HttpGet(get_url);
String content = Client.execute(httpget, responseHandler);
I originally just tried:
String get_url = "http://192.168.30.80/" + str;
But that gave me an illegal character error. After trying urlencode now I get a:
java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=http://192.168.30.80/[Ljava.lang.String;#1a50d830
Why can't it just be a string? (Obviously this is my first attempt with android/java)
Please help me understand what is going wrong, thanks.
URLEncoder.encode does not encode a full URL but should be used for the values of the GET parameters.
eg.
fullString = "?nOne=" + URLEncoder.encode(node1, "UTF-8");
fullString += "&nTwo=" + URLEncoder.encode(node2, "UTF-8");
Looking at the response, the path is not getting parsed to extract the scheme or host as these are both null.
Looking at the documentation, it should work from a string. Have you checked that the string is correctly encoded? It seems like it is unable to identify the scheme or host.
You could try inspecting the string value before it is passed to the HttpGet, or you might want to try using a URI.
URI address = new URI("http://192.168.30.80/" + str);
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(address);
HttpResponse response = client.execute(request);
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 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