Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.net.MalformedURLException: no protocol on URL based on a string modified with URLEncoder

So I was attempting to use this String in a URL :-

http://site-test.com/Meetings/IC/DownloadDocument?meetingId=c21c905c-8359-4bd6-b864-844709e05754&itemId=a4b724d1-282e-4b36-9d16-d619a807ba67&file=\\s604132shvw140\Test-Documents\c21c905c-8359-4bd6-b864-844709e05754_attachments\7e89c3cb-ce53-4a04-a9ee-1a584e157987\myDoc.pdf 

In this code: -

String fileToDownloadLocation = //The above string URL fileToDownload = new URL(fileToDownloadLocation); HttpGet httpget = new HttpGet(fileToDownload.toURI()); 

But at this point I get the error: -

java.net.URISyntaxException: Illegal character in query at index 169:Blahblahblah 

I realised with a bit of googling this was due to the characters in the URL (guessing the &), so I then added in some code so it now looks like so: -

String fileToDownloadLocation = //The above string fileToDownloadLocation = URLEncoder.encode(fileToDownloadLocation, "UTF-8"); URL fileToDownload = new URL(fileToDownloadLocation); HttpGet httpget = new HttpGet(fileToDownload.toURI()); 

However, when I try and run this I get an error when I try and create the URL, the error then reads: -

java.net.MalformedURLException: no protocol: http%3A%2F%2Fsite-test.testsite.com%2FMeetings%2FIC%2FDownloadDocument%3FmeetingId%3Dc21c905c-8359-4bd6-b864-844709e05754%26itemId%3Da4b724d1-282e-4b36-9d16-d619a807ba67%26file%3D%5C%5Cs604132shvw140%5CTest-Documents%5Cc21c905c-8359-4bd6-b864-844709e05754_attachments%5C7e89c3cb-ce53-4a04-a9ee-1a584e157987%myDoc.pdf 

It looks like I can't do the encoding until after I've created the URL else it replaces slashes and things which it shouldn't, but I can't see how I can create the URL with the string and then format it so its suitable for use. I'm not particularly familiar with all this and was hoping someone might be able to point out to me what I'm missing to get string A into a suitably formatted URL to then use with the correct characters replaced?

Any suggestions greatly appreciated!

like image 352
MorkPork Avatar asked Feb 28 '14 11:02

MorkPork


People also ask

How do I resolve Java net MalformedURLException?

Handling MalformedURLException The only Solution for this is to make sure that the url you have passed is legal, with a proper protocol. The best way to do it is validating the URL before you proceed with your program. For validation you can use regular expression or other libraries that provide url validators.

What is malformed URL exception?

Class MalformedURLExceptionThrown to indicate that a malformed URL has occurred. Either no legal protocol could be found in a specification string or the string could not be parsed.


2 Answers

You need to encode your parameter's values before concatenating them to URL.
Backslash \ is special character which have to be escaped as %5C

Escaping example:

String paramValue = "param\\with\\backslash"; String yourURLStr = "http://host.com?param=" + java.net.URLEncoder.encode(paramValue, "UTF-8"); java.net.URL url = new java.net.URL(yourURLStr); 

The result is http://host.com?param=param%5Cwith%5Cbackslash which is properly formatted url string.

like image 155
m-szalik Avatar answered Oct 18 '22 00:10

m-szalik


I have the same problem, i read the url with an properties file:

String configFile = System.getenv("system.Environment");         if (configFile == null || "".equalsIgnoreCase(configFile.trim())) {             configFile = "dev.properties";         }         // Load properties          Properties properties = new Properties();         properties.load(getClass().getResourceAsStream("/" + configFile));        //read url from file         apiUrl = properties.getProperty("url").trim();             URL url = new URL(apiUrl);             //throw exception here     URLConnection conn = url.openConnection(); 

dev.properties

url = "https://myDevServer.com/dev/api/gate" 

it should be

dev.properties

url = https://myDevServer.com/dev/api/gate 

without "" and my problem is solved.

According to oracle documentation

  • Thrown to indicate that a malformed URL has occurred. Either no legal protocol could be found in a specification string or the string could not be parsed.

So it means it is not parsed inside the string.

like image 40
erhun Avatar answered Oct 18 '22 00:10

erhun