Package org.apache.http.client.utils
Class URLEncodedUtils
java.lang.Object
org.apache.http.client.utils.URLEncodedUtils
A collection of utilities for encoding URLs.
- Since:
- 4.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
The default HTML form content type.private static final String
private static final char
private static final BitSet
private static final BitSet
private static final BitSet
Characters which are safe to use in a path, i.e.private static final BitSet
Punctuation characters: , ; : $ invalid input: '&' + =private static final char
private static final char
private static final int
private static final BitSet
Reserved characters, i.e.private static final BitSet
Unreserved characters, i.e.private static final BitSet
Characters which are safe to use in a query or a fragment, i.e.private static final BitSet
Safe characters for x-www-form-urlencoded data, as per java.net.URLEncoder and browser behaviour, i.e.private static final BitSet
Characters which are safe to use in userinfo, i.e. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static List
<org.apache.http.NameValuePair> private static String
decodeFormFields
(String content, String charset) Decode/unescape www-url-form-encoded content.private static String
decodeFormFields
(String content, Charset charset) Decode/unescape www-url-form-encoded content.private static String
encodeFormFields
(String content, String charset) Encode/escape www-url-form-encoded content.private static String
encodeFormFields
(String content, Charset charset) Encode/escape www-url-form-encoded content.(package private) static String
Encode a String using thePATH_SPECIAL
set of characters.(package private) static String
Encode a String using theURIC
set of characters.(package private) static String
encUserInfo
(String content, Charset charset) Encode a String using theUSERINFO
set of characters.static String
format
(Iterable<? extends org.apache.http.NameValuePair> parameters, char parameterSeparator, Charset charset) Returns a String that is suitable for use as anapplication/x-www-form-urlencoded
list of parameters in an HTTP PUT or HTTP POST.static String
Returns a String that is suitable for use as anapplication/x-www-form-urlencoded
list of parameters in an HTTP PUT or HTTP POST.static String
format
(List<? extends org.apache.http.NameValuePair> parameters, char parameterSeparator, String charset) Returns a String that is suitable for use as anapplication/x-www-form-urlencoded
list of parameters in an HTTP PUT or HTTP POST.static String
Returns a String that is suitable for use as anapplication/x-www-form-urlencoded
list of parameters in an HTTP PUT or HTTP POST.static String
formatSegments
(Iterable<String> segments, Charset charset) Returns a string consisting of joint encoded path segments.static String
formatSegments
(String... segments) Returns a string consisting of joint encoded path segments.static boolean
isEncoded
(org.apache.http.HttpEntity entity) Returns true if the entity's Content-Type header isapplication/x-www-form-urlencoded
.static List
<org.apache.http.NameValuePair> Returns a list ofNameValuePair
s URI query parameters.static List
<org.apache.http.NameValuePair> Returns a list ofNameValuePairs
as parsed from the given string using the given character encoding.static List
<org.apache.http.NameValuePair> Deprecated.static List
<org.apache.http.NameValuePair> Returns a list ofNameValuePair
s URI query parameters.static void
Deprecated.(4.4) useparse(String, java.nio.charset.Charset)
static void
parse
(List<org.apache.http.NameValuePair> parameters, Scanner scanner, String parameterSepartorPattern, String charset) Deprecated.static List
<org.apache.http.NameValuePair> parse
(org.apache.http.HttpEntity entity) Returns a list ofNameValuePairs
as parsed from anHttpEntity
.static List
<org.apache.http.NameValuePair> Returns a list ofNameValuePair
s parameters.Returns a list of URI path segments.parsePathSegments
(CharSequence s, Charset charset) Returns a list of URI path segments.splitSegments
(CharSequence s, BitSet separators) private static String
Decode/unescape a portion of a URL, to use with the query part ensureplusAsBlank
is true.private static String
-
Field Details
-
CONTENT_TYPE
The default HTML form content type.- See Also:
-
QP_SEP_A
private static final char QP_SEP_A- See Also:
-
QP_SEP_S
private static final char QP_SEP_S- See Also:
-
NAME_VALUE_SEPARATOR
- See Also:
-
PATH_SEPARATOR
private static final char PATH_SEPARATOR- See Also:
-
PATH_SEPARATORS
-
UNRESERVED
Unreserved characters, i.e. alphanumeric, plus:_ - ! . ~ ' ( ) *
This list is the same as the
unreserved
list in RFC 2396 -
PUNCT
Punctuation characters: , ; : $ invalid input: '&' + =These are the additional characters allowed by userinfo.
-
USERINFO
Characters which are safe to use in userinfo, i.e.UNRESERVED
plusPUNCT
uation -
PATHSAFE
Characters which are safe to use in a path, i.e.UNRESERVED
plusPUNCT
uation plus / @ -
URIC
Characters which are safe to use in a query or a fragment, i.e.RESERVED
plusUNRESERVED
-
RESERVED
-
URLENCODER
Safe characters for x-www-form-urlencoded data, as per java.net.URLEncoder and browser behaviour, i.e. alphanumeric plus"-", "_", ".", "*"
-
PATH_SPECIAL
-
RADIX
private static final int RADIX- See Also:
-
-
Constructor Details
-
URLEncodedUtils
public URLEncodedUtils()
-
-
Method Details
-
parse
Deprecated.4.5 Useparse(URI, Charset)
-
parse
Returns a list ofNameValuePair
s URI query parameters. By convention,'&'
and';'
are accepted as parameter separators.- Parameters:
uri
- input URI.charset
- parameter charset.- Returns:
- list of query parameters.
- Since:
- 4.5
-
parse
public static List<org.apache.http.NameValuePair> parse(org.apache.http.HttpEntity entity) throws IOException Returns a list ofNameValuePairs
as parsed from anHttpEntity
. The encoding is taken from the entity's Content-Encoding header.This is typically used while parsing an HTTP POST.
- Parameters:
entity
- The entity to parse- Returns:
- a list of
NameValuePair
as built from the URI's query portion. - Throws:
IOException
- If there was an exception getting the entity's data.
-
isEncoded
public static boolean isEncoded(org.apache.http.HttpEntity entity) Returns true if the entity's Content-Type header isapplication/x-www-form-urlencoded
. -
parse
@Deprecated public static void parse(List<org.apache.http.NameValuePair> parameters, Scanner scanner, String charset) Deprecated.(4.4) useparse(String, java.nio.charset.Charset)
Adds all parameters within the Scanner to the list ofparameters
, as encoded byencoding
. For example, a scanner containing the stringa=1&b=2&c=3
would add theNameValuePairs
a=1, b=2, and c=3 to the list of parameters. By convention,'&'
and';'
are accepted as parameter separators.- Parameters:
parameters
- List to add parameters to.scanner
- Input that contains the parameters to parse.charset
- Encoding to use when decoding the parameters.
-
parse
@Deprecated public static void parse(List<org.apache.http.NameValuePair> parameters, Scanner scanner, String parameterSepartorPattern, String charset) Deprecated.Adds all parameters within the Scanner to the list ofparameters
, as encoded byencoding
. For example, a scanner containing the stringa=1&b=2&c=3
would add theNameValuePairs
a=1, b=2, and c=3 to the list of parameters.- Parameters:
parameters
- List to add parameters to.scanner
- Input that contains the parameters to parse.parameterSepartorPattern
- The Pattern string for parameter separators, by convention"[&;]"
charset
- Encoding to use when decoding the parameters.
-
parse
Returns a list ofNameValuePair
s URI query parameters. By convention,'&'
and';'
are accepted as parameter separators.- Parameters:
s
- URI query component.charset
- charset to use when decoding the parameters.- Returns:
- list of query parameters.
- Since:
- 4.2
-
parse
public static List<org.apache.http.NameValuePair> parse(String s, Charset charset, char... separators) Returns a list ofNameValuePairs
as parsed from the given string using the given character encoding.- Parameters:
s
- input text.charset
- parameter charset.separators
- parameter separators.- Returns:
- list of query parameters.
- Since:
- 4.3
-
parse
public static List<org.apache.http.NameValuePair> parse(org.apache.http.util.CharArrayBuffer buf, Charset charset, char... separators) Returns a list ofNameValuePair
s parameters.- Parameters:
buf
- text to parse.charset
- Encoding to use when decoding the parameters.separators
- element separators.- Returns:
- a list of
NameValuePair
as built from the URI's query portion. - Since:
- 4.4
-
splitSegments
-
splitPathSegments
-
parsePathSegments
Returns a list of URI path segments.- Parameters:
s
- URI path component.charset
- parameter charset.- Returns:
- list of segments.
- Since:
- 4.5
-
parsePathSegments
Returns a list of URI path segments.- Parameters:
s
- URI path component.- Returns:
- list of segments.
- Since:
- 4.5
-
formatSegments
Returns a string consisting of joint encoded path segments.- Parameters:
segments
- the segments.charset
- parameter charset.- Returns:
- URI path component
- Since:
- 4.5
-
formatSegments
Returns a string consisting of joint encoded path segments.- Parameters:
segments
- the segments.- Returns:
- URI path component
- Since:
- 4.5
-
format
public static String format(List<? extends org.apache.http.NameValuePair> parameters, String charset) Returns a String that is suitable for use as anapplication/x-www-form-urlencoded
list of parameters in an HTTP PUT or HTTP POST.- Parameters:
parameters
- The parameters to include.charset
- The encoding to use.- Returns:
- An
application/x-www-form-urlencoded
string
-
format
public static String format(List<? extends org.apache.http.NameValuePair> parameters, char parameterSeparator, String charset) Returns a String that is suitable for use as anapplication/x-www-form-urlencoded
list of parameters in an HTTP PUT or HTTP POST.- Parameters:
parameters
- The parameters to include.parameterSeparator
- The parameter separator, by convention,'&'
or';'
.charset
- The encoding to use.- Returns:
- An
application/x-www-form-urlencoded
string - Since:
- 4.3
-
format
public static String format(Iterable<? extends org.apache.http.NameValuePair> parameters, Charset charset) Returns a String that is suitable for use as anapplication/x-www-form-urlencoded
list of parameters in an HTTP PUT or HTTP POST.- Parameters:
parameters
- The parameters to include.charset
- The encoding to use.- Returns:
- An
application/x-www-form-urlencoded
string - Since:
- 4.2
-
format
public static String format(Iterable<? extends org.apache.http.NameValuePair> parameters, char parameterSeparator, Charset charset) Returns a String that is suitable for use as anapplication/x-www-form-urlencoded
list of parameters in an HTTP PUT or HTTP POST.- Parameters:
parameters
- The parameters to include.parameterSeparator
- The parameter separator, by convention,'&'
or';'
.charset
- The encoding to use.- Returns:
- An
application/x-www-form-urlencoded
string - Since:
- 4.3
-
createEmptyList
-
urlEncode
-
urlDecode
Decode/unescape a portion of a URL, to use with the query part ensureplusAsBlank
is true.- Parameters:
content
- the portion to decodecharset
- the charset to useplusAsBlank
- iftrue
, then convert '+' to space (e.g. for www-url-form-encoded content), otherwise leave as is.- Returns:
- encoded string
-
decodeFormFields
Decode/unescape www-url-form-encoded content.- Parameters:
content
- the content to decode, will decode '+' as spacecharset
- the charset to use- Returns:
- encoded string
-
decodeFormFields
Decode/unescape www-url-form-encoded content.- Parameters:
content
- the content to decode, will decode '+' as spacecharset
- the charset to use- Returns:
- encoded string
-
encodeFormFields
Encode/escape www-url-form-encoded content.Uses the
URLENCODER
set of characters, rather than theUNRESERVED
set; this is for compatibilty with previous releases, URLEncoder.encode() and most browsers.- Parameters:
content
- the content to encode, will convert space to '+'charset
- the charset to use- Returns:
- encoded string
-
encodeFormFields
Encode/escape www-url-form-encoded content.Uses the
URLENCODER
set of characters, rather than theUNRESERVED
set; this is for compatibilty with previous releases, URLEncoder.encode() and most browsers.- Parameters:
content
- the content to encode, will convert space to '+'charset
- the charset to use- Returns:
- encoded string
-
encUserInfo
Encode a String using theUSERINFO
set of characters.Used by URIBuilder to encode the userinfo segment.
- Parameters:
content
- the string to encode, does not convert space to '+'charset
- the charset to use- Returns:
- the encoded string
-
encUric
Encode a String using theURIC
set of characters.Used by URIBuilder to encode the query and fragment segments.
- Parameters:
content
- the string to encode, does not convert space to '+'charset
- the charset to use- Returns:
- the encoded string
-
encPath
Encode a String using thePATH_SPECIAL
set of characters.Used by URIBuilder to encode path segments.
- Parameters:
content
- the string to encode, does not convert space to '+'charset
- the charset to use- Returns:
- the encoded string
-
parse(URI, Charset)