Reputation: 21600
I feel like I must have missed the right thread, because this seems like it would have been clearly answered before, so sorry if it's a repeat. I read a lot of the threads here and elsewhere and I didn't see a clear "This is how you do it" kind of answer to this question...
The program will be receiving full urls as strings. For example: "http://www.stackoverflow.com/questions/somepage?somequery#somefragment"
I want to take this string and ensure it is properly encoded for use in a connection like with HttpClient.
From what I read, it seems like the way to do this is to use a URI() constructor with the various parts as parameters. So that then begs the question, what is the most reliable way to take a string url, that we know nothing about, other than it is a url, and break it up into the various pieces needed for the URI() constructor.
Here is what I pieced together. Are there any flaws in this method?
URL url = new URL(urlString);
URI uri = new URI(url.getProtocol(), url.getAuthority(), url.getPath(), url.getQuery(), null);
Then, the uri variable contains a encoded ready to use URI. This code works so far, but I want to make sure there aren't any pitfalls that might occur.
Thanks
Upvotes: 2
Views: 593
Reputation: 2651
The URI class does perform escaping of its component fields in certain circumstances. The recommended way to manage the encoding and decoding of URLs is to use URI, and to convert between these two classes using toURI() and URI.toURL(). Ref: http://download.oracle.com/javase/1.5.0/docs/api/java/net/URL.html
Note, any URL instance that complies with RFC 2396 can be converted to a URI. However, some URLs that are not strictly in compliance can not be converted to a URI.
URI.toURL is a convenience method works as if invoking it were equivalent to evaluating the expression new URL(this.toString())
*after first checking that this URI is absolut*e.
Upvotes: 4
Reputation: 80176
I do not think there is any need to construct a URI. The url constructed using "URL url = new URL(urlString);" is perfectly valid to be used by HttpClient unless the URL construction throws a MalformedURLException.
If you are going to have a query string (for GET), then you have to URLEncode it
Upvotes: 0