Class URLEncodedUtils

java.lang.Object
org.apache.http.client.utils.URLEncodedUtils

public class URLEncodedUtils extends Object
A collection of utilities for encoding URLs.
Since:
4.0
  • Field Details

    • CONTENT_TYPE

      public static final String 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

      private static final String NAME_VALUE_SEPARATOR
      See Also:
    • PATH_SEPARATOR

      private static final char PATH_SEPARATOR
      See Also:
    • PATH_SEPARATORS

      private static final BitSet PATH_SEPARATORS
    • UNRESERVED

      private static final BitSet UNRESERVED
      Unreserved characters, i.e. alphanumeric, plus: _ - ! . ~ ' ( ) *

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

    • PUNCT

      private static final BitSet PUNCT
      Punctuation characters: , ; : $ invalid input: '&' + =

      These are the additional characters allowed by userinfo.

    • USERINFO

      private static final BitSet USERINFO
      Characters which are safe to use in userinfo, i.e. UNRESERVED plus PUNCTuation
    • PATHSAFE

      private static final BitSet PATHSAFE
      Characters which are safe to use in a path, i.e. UNRESERVED plus PUNCTuation plus / @
    • URIC

      private static final BitSet URIC
      Characters which are safe to use in a query or a fragment, i.e. RESERVED plus UNRESERVED
    • RESERVED

      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

    • URLENCODER

      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 "-", "_", ".", "*"
    • PATH_SPECIAL

      private static final BitSet PATH_SPECIAL
    • RADIX

      private static final int RADIX
      See Also:
  • Constructor Details

    • URLEncodedUtils

      public URLEncodedUtils()
  • Method Details

    • parse

      @Deprecated public static List<org.apache.http.NameValuePair> parse(URI uri, String charsetName)
      Deprecated.
    • parse

      public static List<org.apache.http.NameValuePair> parse(URI uri, Charset charset)
      Returns a list of NameValuePairs 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 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 - 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 is application/x-www-form-urlencoded.
    • parse

      @Deprecated public static void parse(List<org.apache.http.NameValuePair> parameters, Scanner scanner, 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 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)
      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 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

      public static List<org.apache.http.NameValuePair> parse(String s, Charset charset)
      Returns a list of NameValuePairs 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 of NameValuePairs 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 of NameValuePairs 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

      static List<String> splitSegments(CharSequence s, BitSet separators)
    • splitPathSegments

      static List<String> splitPathSegments(CharSequence s)
    • parsePathSegments

      public static List<String> parsePathSegments(CharSequence s, Charset charset)
      Returns a list of URI path segments.
      Parameters:
      s - URI path component.
      charset - parameter charset.
      Returns:
      list of segments.
      Since:
      4.5
    • parsePathSegments

      public static List<String> parsePathSegments(CharSequence s)
      Returns a list of URI path segments.
      Parameters:
      s - URI path component.
      Returns:
      list of segments.
      Since:
      4.5
    • formatSegments

      public static String formatSegments(Iterable<String> segments, Charset charset)
      Returns a string consisting of joint encoded path segments.
      Parameters:
      segments - the segments.
      charset - parameter charset.
      Returns:
      URI path component
      Since:
      4.5
    • formatSegments

      public static String formatSegments(String... segments)
      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 an application/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 an application/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 an application/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 an application/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

      private static List<org.apache.http.NameValuePair> createEmptyList()
    • urlEncode

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

      private static String urlDecode(String content, Charset charset, boolean plusAsBlank)
      Decode/unescape a portion of a URL, to use with the query part ensure plusAsBlank is true.
      Parameters:
      content - the portion to decode
      charset - the charset to use
      plusAsBlank - if true, then convert '+' to space (e.g. for www-url-form-encoded content), otherwise leave as is.
      Returns:
      encoded string
    • decodeFormFields

      private static String decodeFormFields(String content, String charset)
      Decode/unescape www-url-form-encoded content.
      Parameters:
      content - the content to decode, will decode '+' as space
      charset - the charset to use
      Returns:
      encoded string
    • decodeFormFields

      private static String decodeFormFields(String content, Charset charset)
      Decode/unescape www-url-form-encoded content.
      Parameters:
      content - the content to decode, will decode '+' as space
      charset - the charset to use
      Returns:
      encoded string
    • encodeFormFields

      private static String encodeFormFields(String content, 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 - the content to encode, will convert space to '+'
      charset - the charset to use
      Returns:
      encoded string
    • encodeFormFields

      private static String encodeFormFields(String content, 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 - the content to encode, will convert space to '+'
      charset - the charset to use
      Returns:
      encoded string
    • encUserInfo

      static String encUserInfo(String content, Charset charset)
      Encode a String using the USERINFO 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

      static String encUric(String content, Charset charset)
      Encode a String using the URIC 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

      static String encPath(String content, Charset charset)
      Encode a String using the PATH_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