Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Java by (3.4k points)

My Java standalone application gets a URL (which points to a file) from the user and I need to hit it and download it. The problem I am facing is that I am not able to encode the HTTP URL address properly...

Example:

URL:  http://search.barnesandnoble.com/booksearch/first book.pdf

java.net.URLEncoder.encode(url.toString(), "ISO-8859-1");

returns me:

http%3A%2F%2Fsearch.barnesandnoble.com%2Fbooksearch%2Ffirst+book.pdf

But, what I want is

http://search.barnesandnoble.com/booksearch/first%20book.pdf

(space replaced by %20)

I guess URLEncoder is not designed to encode HTTP URLs... The JavaDoc says "Utility class for HTML form encoding"... Is there any other way to do this?

1 Answer

0 votes
by (46k points)

The java.net.URI class can aid; in the documentation of URL you obtain

Note, the URI class acts make escaping of its component fields in certain circumstances. The recommended way to manage the encoding and decoding of URLs is to use an URI

Try one of the constructors including more than one argument, like:

URI uri = new URI(

    "http", 

    "search.barnesandnoble.com", 

    "/booksearch/first book.pdf",

    null);

URL url = uri.toURL();

//or String request = uri.toString();

(the single-argument constructor of URI doesn't escape illegal characters)

Only illegal characters get left by the above code - it doesn't avoid non-ASCII characters (see faith's comment).

The to ASCII string arrangement can be done to get a String just with US-ASCII characters:

URI URI = new URI(

    "http", 

    "search.barnesandnoble.com", 

    "/booksearch/é",

    null);

String request = uri.toASCIIString();

As an URL with an inquiry like http://www.google.com/ig/api?weather=São Paulo, use the 5-parameter version of the constructor:

URI uri = new URI(

        "http", 

        "www.google.com", 

        "/ig/api",

        "weather=São Paulo",

        null);

String request = uri.toASCIIString();

Related questions

0 votes
1 answer
asked Jul 24, 2019 in Java by Nigam (4k points)
0 votes
1 answer
asked Nov 24, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
0 votes
1 answer
asked Oct 9, 2019 in Java by Anvi (10.2k points)

Browse Categories

...