How do I retrieve a URL in Java?

How can I encode URL in java?

  • I have an url like the one below, http://example-server:8080/v1?url=http://www.example.com?q=how+to+encode+the+complete+url  How can i encode the above url completely  I tried with java.net.URLEncoder but it didn't encode the url completely  can someone help me out of this pls?

  • Answer:

    URLEncoder.encode() works just fine: import java.net.URLDecoder; import java.net.URLEncoder; public class TestClass { public static void main(String[] args) throws Exception { String url = "http://example-server:8080/v1?url=http://www.example.com?q=how+to+encode+the+complete+url "; String encodedUrl = URLEncoder.encode(url, "UTF-8"); System.out.println("Encoded URL " + encodedUrl); String decodedUrl = URLDecoder.decode(encodedUrl, "UTF-8"); System.out.println("Decoded URL " + decodedUrl); } } Output: Encoded URL http%3A%2F%2Fexample-server%3A8080%2Fv1%3Furl%3Dhttp%3A%2F%http://2Fwww.example.com%3Fq%3Dhow%2Bto%2Bencode%2Bthe%2Bcomplete%2Burl++ Decoded URL http://example-server:8080/v1?url=http://www.example.com?q=how+to+encode+the+complete+url

Luigi Mistrik at Quora Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.