Top Description Fields Constructors Methods
org.apache.http.client.utils

public Class URLEncodedUtils

extends Object
Class Inheritance
Imports
java.io.IOException, .InputStream, .InputStreamReader, .Reader, java.net.URI, java.nio.ByteBuffer, .CharBuffer, java.nio.charset.Charset, java.util.ArrayList, .Arrays, .BitSet, .Collections, .List, .Scanner, org.apache.http.Consts, .Header, .HeaderElement, .HttpEntity, .NameValuePair, org.apache.http.entity.ContentType, org.apache.http.message.BasicNameValuePair, .ParserCursor, .TokenParser, org.apache.http.protocol.HTTP, org.apache.http.util.Args, .CharArrayBuffer

A collection of utilities for encoding URLs.
Since
4.0

Field Summary

Modifier and TypeField and Description
public static final String
CONTENT_TYPE

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
PATHSAFE

Characters which are safe to use in a path, i.e. UNRESERVED plus PUNCTuation plus / @

private static final BitSet
PUNCT

Punctuation characters: , ; : $ & + =

These are the additional characters allowed by userinfo.

private static final char
private static final char
private static final int
private static final BitSet
RESERVED

Reserved characters, i.e. ;/?:@&=+$,[]

This list is the same as the reserved list in RFC 2396 as augmented by RFC 2732

private static final BitSet
UNRESERVED

Unreserved characters, i.e. alphanumeric, plus: _ - !

private static final BitSet
URIC

Characters which are safe to use in a query or a fragment, i.e. RESERVED plus UNRESERVED

private static final BitSet
URLENCODER

Safe characters for x-www-form-urlencoded data, as per java.net.URLEncoder and browser behaviour, i.e. alphanumeric plus "-", "_", ".", "*"

private static final BitSet
USERINFO

Characters which are safe to use in userinfo, i.e. UNRESERVED plus PUNCTuation

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private static List<NameValuePair>
private static String

Returns:

encoded string
decodeFormFields
(final String
the content to decode, will decode '+' as space
content
,
final String
the charset to use
charset
)

Decode/unescape www-url-form-encoded content.

private static String

Returns:

encoded string
decodeFormFields
(final String
the content to decode, will decode '+' as space
content
,
final Charset
the charset to use
charset
)

Decode/unescape www-url-form-encoded content.

private static String

Returns:

encoded string
encodeFormFields
(final String
the content to encode, will convert space to '+'
content
,
final String
the charset to use
charset
)

Encode/escape www-url-form-encoded content.

private static String

Returns:

encoded string
encodeFormFields
(final String
the content to encode, will convert space to '+'
content
,
final Charset
the charset to use
charset
)

Encode/escape www-url-form-encoded content.

pack-priv static String

Returns:

the encoded string
encPath
(final String
the string to encode, does not convert space to '+'
content
,
final Charset
the charset to use
charset
)

Encode a String using the PATH_SPECIAL set of characters.

pack-priv static String

Returns:

the encoded string
encUric
(final String
the string to encode, does not convert space to '+'
content
,
final Charset
the charset to use
charset
)

Encode a String using the URIC set of characters.

pack-priv static String

Returns:

the encoded string
encUserInfo
(final String
the string to encode, does not convert space to '+'
content
,
final Charset
the charset to use
charset
)

Encode a String using the USERINFO set of characters.

public static String

Returns:

An application/x-www-form-urlencoded string
format
(final List<? extends NameValuePair>
The parameters to include.
parameters
,
final String
The encoding to use.
charset
)

Returns a String that is suitable for use as an application/x-www-form-urlencoded list of parameters in an HTTP PUT or HTTP POST.

public static String

Returns:

An application/x-www-form-urlencoded string
format
(final List<? extends NameValuePair>
The parameters to include.
parameters
,
final char
The parameter separator, by convention, '&' or ';'.
parameterSeparator
,
final String
The encoding to use.
charset
)

Returns a String that is suitable for use as an application/x-www-form-urlencoded list of parameters in an HTTP PUT or HTTP POST.

public static String

Returns:

An application/x-www-form-urlencoded string
format
(final Iterable<? extends NameValuePair>
The parameters to include.
parameters
,
final Charset
The encoding to use.
charset
)

Returns a String that is suitable for use as an application/x-www-form-urlencoded list of parameters in an HTTP PUT or HTTP POST.

public static String

Returns:

An application/x-www-form-urlencoded string
format
(final Iterable<? extends NameValuePair>
The parameters to include.
parameters
,
final char
The parameter separator, by convention, '&' or ';'.
parameterSeparator
,
final Charset
The encoding to use.
charset
)

Returns a String that is suitable for use as an application/x-www-form-urlencoded list of parameters in an HTTP PUT or HTTP POST.

public static String

Returns:

URI path component
formatSegments
(final Iterable<String>
the segments.
segments
,
final Charset
parameter charset.
charset
)

Returns a string consisting of joint encoded path segments.

public static String

Returns:

URI path component
formatSegments
(final String...
the segments.
segments
)

Returns a string consisting of joint encoded path segments.

public static boolean
isEncoded(final HttpEntity entity)

Returns true if the entity's Content-Type header is application/x-www-form-urlencoded.

public static List<NameValuePair>
parse(final URI uri, final String charsetName)

Deprecated 4.5 Use parse(URI, Charset)
public static List<NameValuePair>

Returns:

list of query parameters.
parse
(final URI
input URI.
uri
,
final Charset
parameter charset.
charset
)

Returns a list of NameValuePairs URI query parameters.

public static List<NameValuePair>

Returns:

a list of NameValuePair as built from the URI's query portion.
parse
(final HttpEntity
The entity to parse
entity
)

Returns a list of NameValuePairs as parsed from an HttpEntity.

public static void
parse(final List<NameValuePair>
List to add parameters to.
parameters
,
final Scanner
Input that contains the parameters to parse.
scanner
,
final String
Encoding to use when decoding the parameters.
charset
)

Adds all parameters within the Scanner to the list of parameters, as encoded by encoding.
public static void
parse(final List<NameValuePair>
List to add parameters to.
parameters
,
final Scanner
Input that contains the parameters to parse.
scanner
,
final String
The Pattern string for parameter separators, by convention "[&;]"
parameterSepartorPattern
,
final String
Encoding to use when decoding the parameters.
charset
)

Adds all parameters within the Scanner to the list of parameters, as encoded by encoding.
public static List<NameValuePair>

Returns:

list of query parameters.
parse
(final String
URI query component.
s
,
final Charset
charset to use when decoding the parameters.
charset
)

Returns a list of NameValuePairs URI query parameters.

public static List<NameValuePair>

Returns:

list of query parameters.
parse
(final String
input text.
s
,
final Charset
parameter charset.
charset
,
final char...
parameter separators.
separators
)

Returns a list of NameValuePairs as parsed from the given string using the given character encoding.

public static List<NameValuePair>

Returns:

a list of NameValuePair as built from the URI's query portion.
parse
(final CharArrayBuffer
text to parse.
buf
,
final Charset
Encoding to use when decoding the parameters.
charset
,
final char...
element separators.
separators
)

Returns a list of NameValuePairs parameters.

public static List<String>

Returns:

list of segments.
parsePathSegments
(final CharSequence
URI path component.
s
,
final Charset
parameter charset.
charset
)

Returns a list of URI path segments.

public static List<String>

Returns:

list of segments.
parsePathSegments
(final CharSequence
URI path component.
s
)

Returns a list of URI path segments.

pack-priv static List<String>
pack-priv static List<String>
splitSegments(final CharSequence s, final BitSet separators)

private static String

Returns:

encoded string
urlDecode
(final String
the portion to decode
content
,
final Charset
the charset to use
charset
,
final boolean
if true, then convert '+' to space (e.g. for www-url-form-encoded content), otherwise leave as is.
plusAsBlank
)

Decode/unescape a portion of a URL, to use with the query part ensure plusAsBlank is true.

private static String
urlEncode(final String content, final Charset charset, final BitSet safechars, final boolean blankAsPlus)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CONTENT_TYPEback to summary
public static final String CONTENT_TYPE

The default HTML form content type.

NAME_VALUE_SEPARATORback to summary
private static final String NAME_VALUE_SEPARATOR
PATH_SEPARATORback to summary
private static final char PATH_SEPARATOR
PATH_SEPARATORSback to summary
private static final BitSet PATH_SEPARATORS
PATH_SPECIALback to summary
private static final BitSet PATH_SPECIAL
PATHSAFEback to summary
private static final BitSet PATHSAFE

Characters which are safe to use in a path, i.e. UNRESERVED plus PUNCTuation plus / @

PUNCTback to summary
private static final BitSet PUNCT

Punctuation characters: , ; : $ & + =

These are the additional characters allowed by userinfo.

QP_SEP_Aback to summary
private static final char QP_SEP_A
QP_SEP_Sback to summary
private static final char QP_SEP_S
RADIXback to summary
private static final int RADIX
RESERVEDback to summary
private static final BitSet RESERVED

Reserved characters, i.e. ;/?:@&=+$,[]

This list is the same as the reserved list in RFC 2396 as augmented by RFC 2732

UNRESERVEDback to summary
private static final BitSet UNRESERVED

Unreserved characters, i.e. alphanumeric, plus: _ - ! . ~ ' ( ) *

This list is the same as the unreserved list in RFC 2396

URICback to summary
private static final BitSet URIC

Characters which are safe to use in a query or a fragment, i.e. RESERVED plus UNRESERVED

URLENCODERback to summary
private static final BitSet URLENCODER

Safe characters for x-www-form-urlencoded data, as per java.net.URLEncoder and browser behaviour, i.e. alphanumeric plus "-", "_", ".", "*"

USERINFOback to summary
private static final BitSet USERINFO

Characters which are safe to use in userinfo, i.e. UNRESERVED plus PUNCTuation

Constructor Detail

URLEncodedUtilsback to summary
public URLEncodedUtils()

Method Detail

createEmptyListback to summary
private static List<NameValuePair> createEmptyList()
decodeFormFieldsback to summary
private static String decodeFormFields(final String content, final String charset)

Decode/unescape www-url-form-encoded content.

Parameters
content:String

the content to decode, will decode '+' as space

charset:String

the charset to use

Returns:String

encoded string

decodeFormFieldsback to summary
private static String decodeFormFields(final String content, final Charset charset)

Decode/unescape www-url-form-encoded content.

Parameters
content:String

the content to decode, will decode '+' as space

charset:Charset

the charset to use

Returns:String

encoded string

encodeFormFieldsback to summary
private static String encodeFormFields(final String content, final String charset)

Encode/escape www-url-form-encoded content.

Uses the URLENCODER set of characters, rather than the UNRESERVED set; this is for compatibilty with previous releases, URLEncoder.encode() and most browsers.

Parameters
content:String

the content to encode, will convert space to '+'

charset:String

the charset to use

Returns:String

encoded string

encodeFormFieldsback to summary
private static String encodeFormFields(final String content, final Charset charset)

Encode/escape www-url-form-encoded content.

Uses the URLENCODER set of characters, rather than the UNRESERVED set; this is for compatibilty with previous releases, URLEncoder.encode() and most browsers.

Parameters
content:String

the content to encode, will convert space to '+'

charset:Charset

the charset to use

Returns:String

encoded string

encPathback to summary
pack-priv static String encPath(final String content, final Charset charset)

Encode a String using the PATH_SPECIAL set of characters.

Used by URIBuilder to encode path segments.

Parameters
content:String

the string to encode, does not convert space to '+'

charset:Charset

the charset to use

Returns:String

the encoded string

encUricback to summary
pack-priv static String encUric(final String content, final Charset charset)

Encode a String using the URIC set of characters.

Used by URIBuilder to encode the query and fragment segments.

Parameters
content:String

the string to encode, does not convert space to '+'

charset:Charset

the charset to use

Returns:String

the encoded string

encUserInfoback to summary
pack-priv static String encUserInfo(final String content, final Charset charset)

Encode a String using the USERINFO set of characters.

Used by URIBuilder to encode the userinfo segment.

Parameters
content:String

the string to encode, does not convert space to '+'

charset:Charset

the charset to use

Returns:String

the encoded string

formatback to summary
public static String format(final List<? extends NameValuePair> parameters, final String charset)

Returns a String that is suitable for use as an application/x-www-form-urlencoded list of parameters in an HTTP PUT or HTTP POST.

Parameters
parameters:List<? extends NameValuePair>

The parameters to include.

charset:String

The encoding to use.

Returns:String

An application/x-www-form-urlencoded string

formatback to summary
public static String format(final List<? extends NameValuePair> parameters, final char parameterSeparator, final String charset)

Returns a String that is suitable for use as an application/x-www-form-urlencoded list of parameters in an HTTP PUT or HTTP POST.

Parameters
parameters:List<? extends NameValuePair>

The parameters to include.

parameterSeparator:char

The parameter separator, by convention, '&' or ';'.

charset:String

The encoding to use.

Returns:String

An application/x-www-form-urlencoded string

Since
4.3
formatback to summary
public static String format(final Iterable<? extends NameValuePair> parameters, final Charset charset)

Returns a String that is suitable for use as an application/x-www-form-urlencoded list of parameters in an HTTP PUT or HTTP POST.

Parameters
parameters:Iterable<? extends NameValuePair>

The parameters to include.

charset:Charset

The encoding to use.

Returns:String

An application/x-www-form-urlencoded string

Since
4.2
formatback to summary
public static String format(final Iterable<? extends NameValuePair> parameters, final char parameterSeparator, final Charset charset)

Returns a String that is suitable for use as an application/x-www-form-urlencoded list of parameters in an HTTP PUT or HTTP POST.

Parameters
parameters:Iterable<? extends NameValuePair>

The parameters to include.

parameterSeparator:char

The parameter separator, by convention, '&' or ';'.

charset:Charset

The encoding to use.

Returns:String

An application/x-www-form-urlencoded string

Since
4.3
formatSegmentsback to summary
public static String formatSegments(final Iterable<String> segments, final Charset charset)

Returns a string consisting of joint encoded path segments.

Parameters
segments:Iterable<String>

the segments.

charset:Charset

parameter charset.

Returns:String

URI path component

Since
4.5
formatSegmentsback to summary
public static String formatSegments(final String... segments)

Returns a string consisting of joint encoded path segments.

Parameters
segments:String[]

the segments.

Returns:String

URI path component

Since
4.5
isEncodedback to summary
public static boolean isEncoded(final HttpEntity entity)

Returns true if the entity's Content-Type header is application/x-www-form-urlencoded.

parseback to summary
public static List<NameValuePair> parse(final URI uri, final String charsetName)

Deprecated

4.5 Use parse(URI, Charset)

Annotations
@Deprecated
parseback to summary
public static List<NameValuePair> parse(final URI uri, final Charset charset)

Returns a list of NameValuePairs URI query parameters. By convention, '&' and ';' are accepted as parameter separators.

Parameters
uri:URI

input URI.

charset:Charset

parameter charset.

Returns:List<NameValuePair>

list of query parameters.

Since
4.5
parseback to summary
public static List<NameValuePair> parse(final HttpEntity entity) throws IOException

Returns a list of NameValuePairs as parsed from an HttpEntity. The encoding is taken from the entity's Content-Encoding header.

This is typically used while parsing an HTTP POST.

Parameters
entity:HttpEntity

The entity to parse

Returns:List<NameValuePair>

a list of NameValuePair as built from the URI's query portion.

Exceptions
IOException:
If there was an exception getting the entity's data.
parseback to summary
public static void parse(final List<NameValuePair> parameters, final Scanner scanner, final String charset)

Adds all parameters within the Scanner to the list of parameters, as encoded by encoding. For example, a scanner containing the string a=1&b=2&c=3 would add the NameValuePairs a=1, b=2, and c=3 to the list of parameters. By convention, '&' and ';' are accepted as parameter separators.

Parameters
parameters:List<NameValuePair>

List to add parameters to.

scanner:Scanner

Input that contains the parameters to parse.

charset:String

Encoding to use when decoding the parameters.

Annotations
@Deprecated
parseback to summary
public static void parse(final List<NameValuePair> parameters, final Scanner scanner, final String parameterSepartorPattern, final String charset)

Adds all parameters within the Scanner to the list of parameters, as encoded by encoding. For example, a scanner containing the string a=1&b=2&c=3 would add the NameValuePairs a=1, b=2, and c=3 to the list of parameters.

Parameters
parameters:List<NameValuePair>

List to add parameters to.

scanner:Scanner

Input that contains the parameters to parse.

parameterSepartorPattern:String

The Pattern string for parameter separators, by convention "[&;]"

charset:String

Encoding to use when decoding the parameters.

Annotations
@Deprecated
parseback to summary
public static List<NameValuePair> parse(final String s, final Charset charset)

Returns a list of NameValuePairs URI query parameters. By convention, '&' and ';' are accepted as parameter separators.

Parameters
s:String

URI query component.

charset:Charset

charset to use when decoding the parameters.

Returns:List<NameValuePair>

list of query parameters.

Since
4.2
parseback to summary
public static List<NameValuePair> parse(final String s, final Charset charset, final char... separators)

Returns a list of NameValuePairs as parsed from the given string using the given character encoding.

Parameters
s:String

input text.

charset:Charset

parameter charset.

separators:char[]

parameter separators.

Returns:List<NameValuePair>

list of query parameters.

Since
4.3
parseback to summary
public static List<NameValuePair> parse(final CharArrayBuffer buf, final Charset charset, final char... separators)

Returns a list of NameValuePairs parameters.

Parameters
buf:CharArrayBuffer

text to parse.

charset:Charset

Encoding to use when decoding the parameters.

separators:char[]

element separators.

Returns:List<NameValuePair>

a list of NameValuePair as built from the URI's query portion.

Since
4.4
parsePathSegmentsback to summary
public static List<String> parsePathSegments(final CharSequence s, final Charset charset)

Returns a list of URI path segments.

Parameters
s:CharSequence

URI path component.

charset:Charset

parameter charset.

Returns:List<String>

list of segments.

Since
4.5
parsePathSegmentsback to summary
public static List<String> parsePathSegments(final CharSequence s)

Returns a list of URI path segments.

Parameters
s:CharSequence

URI path component.

Returns:List<String>

list of segments.

Since
4.5
splitPathSegmentsback to summary
pack-priv static List<String> splitPathSegments(final CharSequence s)
splitSegmentsback to summary
pack-priv static List<String> splitSegments(final CharSequence s, final BitSet separators)
urlDecodeback to summary
private static String urlDecode(final String content, final Charset charset, final boolean plusAsBlank)

Decode/unescape a portion of a URL, to use with the query part ensure plusAsBlank is true.

Parameters
content:String

the portion to decode

charset:Charset

the charset to use

plusAsBlank:boolean

if true, then convert '+' to space (e.g. for www-url-form-encoded content), otherwise leave as is.

Returns:String

encoded string

urlEncodeback to summary
private static String urlEncode(final String content, final Charset charset, final BitSet safechars, final boolean blankAsPlus)