Class HttpSupport


  • public class HttpSupport
    extends Object
    Extra utilities to support usage of HTTP.
    • Method Detail

      • encode

        public static void encode​(StringBuilder urlstr,
                                  String key)
        URL encode a value string into an output buffer.
        Parameters:
        urlstr - the output buffer.
        key - value which must be encoded to protected special characters.
      • urlEncode

        public static String urlEncode​(String url,
                                       boolean keepPathSlash)
        Translates the provided URL into application/x-www-form-urlencoded format.
        Parameters:
        url - The URL to translate.
        keepPathSlash - Whether or not to keep "/" in the URL (i.e. don't translate them to "%2F").
        Returns:
        The translated URL.
        Since:
        5.13
      • response

        public static int response​(HttpConnection c)
                            throws IOException
        Get the HTTP response code from the request.

        Roughly the same as c.getResponseCode() but the ConnectException is translated to be more understandable.

        Parameters:
        c - connection the code should be obtained from.
        Returns:
        r HTTP status code, usually 200 to indicate success. See HttpConnection for other defined constants.
        Throws:
        IOException - communications error prevented obtaining the response code.
        Since:
        3.3
      • response

        public static int response​(HttpURLConnection c)
                            throws IOException
        Get the HTTP response code from the request.

        Roughly the same as c.getResponseCode() but the ConnectException is translated to be more understandable.

        Parameters:
        c - connection the code should be obtained from.
        Returns:
        r HTTP status code, usually 200 to indicate success. See HttpConnection for other defined constants.
        Throws:
        IOException - communications error prevented obtaining the response code.
      • responseHeader

        public static String responseHeader​(HttpConnection c,
                                            String headerName)
                                     throws IOException
        Extract a HTTP header from the response.
        Parameters:
        c - connection the header should be obtained from.
        headerName - the header name
        Returns:
        the header value
        Throws:
        IOException - communications error prevented obtaining the header.
        Since:
        4.7
      • proxyFor

        public static Proxy proxyFor​(ProxySelector proxySelector,
                                     URL u)
                              throws ConnectException
        Determine the proxy server (if any) needed to obtain a URL.
        Parameters:
        proxySelector - proxy support for the caller.
        u - location of the server caller wants to talk to.
        Returns:
        proxy to communicate with the supplied URL.
        Throws:
        ConnectException - the proxy could not be computed as the supplied URL could not be read. This failure should never occur.
      • configureTLS

        public static void configureTLS​(SSLSocket socket)
        Enables all supported TLS protocol versions on the socket given. If system property "https.protocols" is set, only protocols specified there are enabled.

        This is primarily a mechanism to deal with using TLS on IBM JDK. IBM JDK returns sockets that support all TLS protocol versions but have only the one specified in the context enabled. Oracle or OpenJDK return sockets that have all available protocols enabled already, up to the one specified.

        SSLContext.getInstance() OpenJDK IDM JDK
        "TLS" Supported: TLSv1, TLSV1.1, TLSv1.2 (+ TLSv1.3)
        Enabled: TLSv1, TLSV1.1, TLSv1.2 (+ TLSv1.3)
        Supported: TLSv1, TLSV1.1, TLSv1.2
        Enabled: TLSv1
        "TLSv1.2" Supported: TLSv1, TLSV1.1, TLSv1.2
        Enabled: TLSv1, TLSV1.1, TLSv1.2
        Supported: TLSv1, TLSV1.1, TLSv1.2
        Enabled: TLSv1.2
        Parameters:
        socket - to configure
        Since:
        5.7
        See Also:
        Behavior of SSLContext.getInstance("TLS") on IBM JDK, Customizing JSSE about https.protocols
      • scanToken

        public static int scanToken​(String header,
                                    int from)
        Scan a RFC 7230 token as it appears in HTTP headers.
        Parameters:
        header - to scan in
        from - index in header to start scanning at
        Returns:
        the index after the token, that is, on the first non-token character or header.length
        Throws:
        IndexOutOfBoundsException - if from < 0 or from > header.length()
        Since:
        5.10
        See Also:
        RFC 7230, Appendix B: Collected Grammar; "token" production