Java Utililty Methods URL Encode

List of utility methods to do URL Encode

Description

The list of methods to do URL Encode are organized into topic(s).

Method

StringencodeArray(final String[] val)
Return a String representing the given String array, achieved by URLEncoding the individual String elements then concatenating with intervening blanks.
if (val == null) {
    return null;
int len = val.length;
if (len == 0) {
    return "";
StringBuffer sb = new StringBuffer();
...
StringencodeForUrl(final String s)
encode For Url
final StringBuilder result = new StringBuilder();
byte[] bytes;
try {
    bytes = s.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
    throw new RuntimeException("Missing UTF-8 support?!", e);
for (byte aByte : bytes) {
...
StringencodeSrcUrl(String fullUri)
encode Src Url
try {
    int length = fullUri.length();
    StringBuffer buf = new StringBuffer(length);
    for (int i = 0; i < length; i++) {
        char c = fullUri.charAt(i);
        if (c <= 127) {
            buf.append(c);
        } else {
...
StringencodeStringURL(String str)

Encodes a given string.

if (str.indexOf('%') == -1) {
    return encodeStringURL(str, UTF8);
} else {
    return str;
StringencodeUri(String url)
encode Uri
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(bout);
dout.writeUTF(url);
byte[] byteUrl = bout.toByteArray();
StringBuffer buf = new StringBuffer();
for (int i = 0; i < byteUrl.length; i++) {
    buf.append(byteUrl[i] + "|");
dout.close();
bout.close();
return buf.toString();
StringencodeURIComponent(String input)
Function to convert a given string into URI encoded format.
if (input == null || input.trim().length() == 0) {
    return input;
int l = input.length();
StringBuilder o = new StringBuilder(l * 3);
try {
    for (int i = 0; i < l; i++) {
        String e = input.substring(i, i + 1);
...
byte[]encodeURL(BitSet urlsafe, byte[] bytes)
encode URL
if (bytes == null) {
    return null;
if (urlsafe == null) {
    urlsafe = WWW_FORM_URL;
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
for (byte c : bytes) {
...
StringencodeUrl(final String input)
Encodes an input string according to RFC 2392.
if (input == null) {
    return null;
final StringBuilder builder = new StringBuilder();
for (final byte c : input.getBytes(US_ASCII)) {
    int b = c;
    if (b < 0) {
        b = 256 + b;
...
StringencodeURL(String s)
Encodes a String into UTF-8 for use in an URL query string or html POST data.
try {
    byte[] utf8 = s.getBytes("UTF-8");
    int len = utf8.length;
    StringBuffer sbuf = new StringBuffer(len);
    for (int i = 0; i < len; i++) {
        int ch = utf8[i];
        if (('A' <= ch && ch <= 'Z') || ('a' <= ch && ch <= 'z') || ('0' <= ch && ch <= '9')
                || (ch == '-' || ch == '_' || ch == '.' || ch == '!' || ch == '~' || ch == '*' || ch == '\''
...
StringencodeURL(String s)
Encodes characters in the given string as '%'-escaped octets using the UTF-8 scheme.
if (s == null) {
    return null;
StringBuilder encoded = null;
int oldLength = s.length();
int current = 0;
while (current < oldLength) {
    int nextToEncode = current;
...