URLEncoder
Stay organized with collections
Save and categorize content based on your preferences.
Utility class for HTML form encoding. This class contains static methods
for converting a String to the application/x-www-form-urlencoded
MIME
format. For more information about HTML form encoding, consult the HTML
specification.
When encoding a String, the following rules apply:
- The alphanumeric characters "
a
" through
"z
", "A
" through
"Z
" and "0
"
through "9
" remain the same.
- The special characters "
.
",
"-
", "*
", and
"_
" remain the same.
- The space character " " is
converted into a plus sign "
+
".
- All other characters are unsafe and are first converted into
one or more bytes using some encoding scheme. Then each byte is
represented by the 3-character string
"
%xy
", where xy is the
two-digit hexadecimal representation of the byte.
The recommended encoding scheme to use is UTF-8. However,
for compatibility reasons, if an encoding is not specified,
then the default encoding of the platform is used.
For example using UTF-8 as the encoding scheme the string "The
string ü@foo-bar" would get converted to
"The+string+%C3%BC%40foo-bar" because in UTF-8 the character
ü is encoded as two bytes C3 (hex) and BC (hex), and the
character @ is encoded as one byte 40 (hex).
Public Method Summary
static
String
|
encode( String s, String enc)
Translates a string into application/x-www-form-urlencoded
format using a specific encoding scheme.
|
static
String
|
encode( String s)
This method is deprecated.
The resulting string may vary depending on the platform's
default encoding. Instead, use the encode(String,String)
method to specify the encoding.
|
Inherited Method Summary
From class
java.lang.Object
Object
|
clone()
Creates and returns a copy of this Object .
|
boolean
|
equals( Object obj)
Compares this instance with the specified object and indicates if they
are equal.
|
void
|
finalize()
Invoked when the garbage collector has detected that this instance is no longer reachable.
|
final
Class<?>
|
getClass()
Returns the unique instance of Class that represents this
object's class.
|
int
|
hashCode()
Returns an integer hash code for this object.
|
final
void
|
notify()
Causes a thread which is waiting on this object's monitor (by means of
calling one of the wait() methods) to be woken up.
|
final
void
|
notifyAll()
Causes all threads which are waiting on this object's monitor (by means
of calling one of the wait() methods) to be woken up.
|
String
|
toString()
Returns a string containing a concise, human-readable description of this
object.
|
final
void
|
wait(long timeout, int nanos)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the
specified timeout expires.
|
final
void
|
wait(long timeout)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the
specified timeout expires.
|
final
void
|
wait()
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object.
|
Public Methods
Translates a string into application/x-www-form-urlencoded
format using a specific encoding scheme. This method uses the
supplied encoding scheme to obtain the bytes for unsafe
characters.
Note: The
World Wide Web Consortium Recommendation states that
UTF-8 should be used. Not doing so may introduce
incompatibilities.
public
static
String
encode
(String s)
This method is deprecated.
The resulting string may vary depending on the platform's
default encoding. Instead, use the encode(String,String)
method to specify the encoding.
Translates a string into x-www-form-urlencoded
format. This method uses the platform's default encoding
as the encoding scheme to obtain the bytes for unsafe characters.
Parameters
s |
String to be translated. |
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-10 UTC."],[[["\u003cp\u003e\u003ccode\u003eURLEncoder\u003c/code\u003e is a utility class used to convert a \u003ccode\u003eString\u003c/code\u003e into \u003ccode\u003eapplication/x-www-form-urlencoded\u003c/code\u003e format, which is commonly used in HTML forms.\u003c/p\u003e\n"],["\u003cp\u003eIt provides methods to encode strings based on specific encoding schemes like UTF-8 or the platform's default encoding, although using the platform default is deprecated due to potential inconsistencies.\u003c/p\u003e\n"],["\u003cp\u003eDuring encoding, alphanumeric characters, ".", "-", "*", and "_" remain unchanged, spaces are converted to "+", and other characters are converted into their hexadecimal representation using the specified encoding.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eencode(String, String)\u003c/code\u003e method is recommended for encoding with a specific scheme, such as UTF-8, to ensure compatibility across different platforms.\u003c/p\u003e\n"],["\u003cp\u003eWhile other methods are inherited from the \u003ccode\u003eObject\u003c/code\u003e class, the primary focus of \u003ccode\u003eURLEncoder\u003c/code\u003e remains on its static encoding functionalities.\u003c/p\u003e\n"]]],[],null,["public class **URLEncoder** extends [Object](../../../reference/java/lang/Object.html) \nUtility class for HTML form encoding. This class contains static methods\nfor converting a String to the `application/x-www-form-urlencoded` MIME\nformat. For more information about HTML form encoding, consult the HTML\n[specification](http://www.w3.org/TR/html4/).\n\n\nWhen encoding a String, the following rules apply:\n\n- The alphanumeric characters \"`a`\" through \"`z`\", \"`A`\" through \"`Z`\" and \"`0`\" through \"`9`\" remain the same.\n- The special characters \"`.`\", \"`-`\", \"`*`\", and \"`_`\" remain the same.\n- The space character \" \" is converted into a plus sign \"`+`\".\n- All other characters are unsafe and are first converted into one or more bytes using some encoding scheme. Then each byte is represented by the 3-character string \"*`%xy`* \", where *xy* is the two-digit hexadecimal representation of the byte. The recommended encoding scheme to use is UTF-8. However, for compatibility reasons, if an encoding is not specified, then the default encoding of the platform is used.\n\n\nFor example using UTF-8 as the encoding scheme the string \"The\nstring ü@foo-bar\" would get converted to\n\"The+string+%C3%BC%40foo-bar\" because in UTF-8 the character\nü is encoded as two bytes C3 (hex) and BC (hex), and the\ncharacter @ is encoded as one byte 40 (hex). \n\nPublic Method Summary\n\n|-----------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| static [String](../../../reference/java/lang/String.html) | [encode](../../../reference/java/net/URLEncoder.html#encode(java.lang.String,%20java.lang.String))([String](../../../reference/java/lang/String.html) s, [String](../../../reference/java/lang/String.html) enc) Translates a string into `application/x-www-form-urlencoded` format using a specific encoding scheme. |\n| static [String](../../../reference/java/lang/String.html) | [encode](../../../reference/java/net/URLEncoder.html#encode(java.lang.String))([String](../../../reference/java/lang/String.html) s) *This method is deprecated. The resulting string may vary depending on the platform's default encoding. Instead, use the encode(String,String) method to specify the encoding.* |\n\nInherited Method Summary \nFrom class [java.lang.Object](../../../reference/java/lang/Object.html) \n\n|-------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [Object](../../../reference/java/lang/Object.html) | [clone](../../../reference/java/lang/Object.html#clone())() Creates and returns a copy of this `Object`. |\n| boolean | [equals](../../../reference/java/lang/Object.html#equals(java.lang.Object))([Object](../../../reference/java/lang/Object.html) obj) Compares this instance with the specified object and indicates if they are equal. |\n| void | [finalize](../../../reference/java/lang/Object.html#finalize())() Invoked when the garbage collector has detected that this instance is no longer reachable. |\n| final [Class](../../../reference/java/lang/Class.html)\\\u003c?\\\u003e | [getClass](../../../reference/java/lang/Object.html#getClass())() Returns the unique instance of [Class](../../../reference/java/lang/Class.html) that represents this object's class. |\n| int | [hashCode](../../../reference/java/lang/Object.html#hashCode())() Returns an integer hash code for this object. |\n| final void | [notify](../../../reference/java/lang/Object.html#notify())() Causes a thread which is waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |\n| final void | [notifyAll](../../../reference/java/lang/Object.html#notifyAll())() Causes all threads which are waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |\n| [String](../../../reference/java/lang/String.html) | [toString](../../../reference/java/lang/Object.html#toString())() Returns a string containing a concise, human-readable description of this object. |\n| final void | [wait](../../../reference/java/lang/Object.html#wait(long,%20int))(long timeout, int nanos) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |\n| final void | [wait](../../../reference/java/lang/Object.html#wait(long))(long timeout) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |\n| final void | [wait](../../../reference/java/lang/Object.html#wait())() Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object. |\n\nPublic Methods \n\npublic static [String](../../../reference/java/lang/String.html)\n**encode**\n([String](../../../reference/java/lang/String.html) s, [String](../../../reference/java/lang/String.html) enc) \nTranslates a string into `application/x-www-form-urlencoded`\nformat using a specific encoding scheme. This method uses the\nsupplied encoding scheme to obtain the bytes for unsafe\ncharacters.\n\n\n***Note:** The [World Wide Web Consortium Recommendation](http://www.w3.org/TR/html40/appendix/notes.html#non-ascii-chars) states that\nUTF-8 should be used. Not doing so may introduce\nincompatibilities.* \n\nParameters\n\n| s | `String` to be translated. |\n| enc | The name of a supported [character encoding](../lang/package-summary.html#charenc). |\n|-----|-------------------------------------------------------------------------------------|\n\nReturns\n\n- the translated `String`. \n\nThrows\n\n| [UnsupportedEncodingException](../../../reference/java/io/UnsupportedEncodingException.html) | If the named encoding is not supported |\n|----------------------------------------------------------------------------------------------|----------------------------------------|\n\nSee Also\n\n- [URLDecoder.decode(java.lang.String, java.lang.String)](../../../reference/java/net/URLDecoder.html#decode(java.lang.String,%20java.lang.String)) \n\npublic static [String](../../../reference/java/lang/String.html)\n**encode**\n([String](../../../reference/java/lang/String.html) s) \n\n\n**This method is deprecated.** \nThe resulting string may vary depending on the platform's\ndefault encoding. Instead, use the encode(String,String)\nmethod to specify the encoding. \nTranslates a string into `x-www-form-urlencoded`\nformat. This method uses the platform's default encoding\nas the encoding scheme to obtain the bytes for unsafe characters. \n\nParameters\n\n| s | `String` to be translated. |\n|---|----------------------------|\n\nReturns\n\n- the translated `String`."]]