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
Related Q & A:
- How can I make a Spinner only by java?Best solution by stackoverflow.com
- How can I mount a network drive in Mac OS X in Java?Best solution by Stack Overflow
- How can I change url name?Best solution by Yahoo! Answers
- how can I listen for database changes using java?Best solution by Stack Overflow
- How can I enable Java script?Best solution by eHow old
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.