Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encode and Decode rfc2396 URLs

Tags:

java

rfc2396

What is the best way to encode URL strings such that they are rfc2396 compliant and to decode a rfc2396 compliant string such that for example %20 is replaced with a space character?

edit: URLEncoder and URLDecoder classes do not encode/decode rfc2396 compliant URLs, they encode to a MIME type of application/x-www-form-urlencoded which is used to encode HTML form parameter data.

like image 390
Martin OConnor Avatar asked Nov 20 '08 09:11

Martin OConnor


People also ask

How do I decode a URL?

Decode from URL-encoded format. Simply enter your data then push the decode button. For encoded binaries (like images, documents, etc.) use the file upload form a little further down on this page. Source character set.

What is %20 in the URL?

A space is assigned number 32, which is 20 in hexadecimal. When you see “%20,” it represents a space in an encoded URL, for example, http://www.example.com/products%20and%20services.html.

How do I know if a URL is encoded?

(This assumes you will not be given an incomplete URI with no scheme.) So you can test if the string contains a colon, if not, urldecode it, and if that string contains a colon, the original string was url encoded, if not, check if the strings are different and if so, urldecode again and if not, it is not a valid URI.


1 Answers

Use the URI class as follows:

URI uri = new URI("http", "//www.someurl.com/has spaces in url", null);
URL url = uri.toURL();

or if you want a String:

String urlString = uri.toASCIIString();
like image 158
larf311 Avatar answered Oct 19 '22 17:10

larf311