Dave's Brain

Browse - Programming Tips - How do I encode a string so its safe to use in a URL?

Date: 2013dec21
Language: Java

Q.  How do I encode a string so its safe to use in a URL?

A.
import java.net.URLEncoder; import java.io.UnsupportedEncodingException; String encodeForUrl(final String param) { try { return URLEncoder.encode(param, "UTF-8"); } catch (UnsupportedEncodingException e) { return ""; } } // Example use: String url = "http://example.com?param=" + encodeForUrl(param);

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.