Class HttpRequestUtils


  • public class HttpRequestUtils
    extends java.lang.Object
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.Map<java.lang.String,​java.util.List<java.lang.String>> getCookieHeaders​(java.net.CookieManager cookieManager, java.net.URL url)
      Get the cookie headers for a URL.
      static java.util.Map<java.lang.String,​java.util.List<java.lang.String>> mergeHeaders​(java.util.Map<java.lang.String,​java.util.List<java.lang.String>>... maps)
      Merge multiple headers into one map.
      static java.time.Instant parseHttpDate​(java.lang.String httpDate)
      Parse a HTTP date.
      static java.lang.Long parseSecondsOrHttpDate​(java.lang.String value)
      Parse an HTTP date or seconds string as milliseconds until the date.
      static byte[] readBody​(java.net.HttpURLConnection connection)
      Read the body of a connection.
      static byte[] readFromStream​(java.io.InputStream is)
      Read the body of a connection.
      static void setHeaders​(java.net.HttpURLConnection connection, java.util.Map<java.lang.String,​java.util.List<java.lang.String>> headers)
      Set the headers for a connection.
      static void updateCookies​(java.net.CookieManager cookieManager, java.net.URL url, java.util.Map<java.lang.String,​java.util.List<java.lang.String>> headers)
      Update the cookies for a URL.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • HttpRequestUtils

        public HttpRequestUtils()
    • Method Detail

      • mergeHeaders

        @SafeVarargs
        public static java.util.Map<java.lang.String,​java.util.List<java.lang.String>> mergeHeaders​(java.util.Map<java.lang.String,​java.util.List<java.lang.String>>... maps)
        Merge multiple headers into one map.
        Parameters:
        maps - The headers to merge
        Returns:
        The merged headers
      • getCookieHeaders

        public static java.util.Map<java.lang.String,​java.util.List<java.lang.String>> getCookieHeaders​(@Nullable
                                                                                                              java.net.CookieManager cookieManager,
                                                                                                              java.net.URL url)
                                                                                                       throws java.io.IOException
        Get the cookie headers for a URL.
        Parameters:
        cookieManager - The cookie manager to use
        url - The URL to get the cookies for
        Returns:
        The cookie headers
        Throws:
        java.io.IOException - If an I/O error occurs
      • updateCookies

        public static void updateCookies​(@Nullable
                                         java.net.CookieManager cookieManager,
                                         java.net.URL url,
                                         java.util.Map<java.lang.String,​java.util.List<java.lang.String>> headers)
                                  throws java.io.IOException
        Update the cookies for a URL.
        Parameters:
        cookieManager - The cookie manager to use
        url - The URL to update the cookies for
        headers - The headers to update the cookies from
        Throws:
        java.io.IOException - If an I/O error occurs
      • setHeaders

        public static void setHeaders​(java.net.HttpURLConnection connection,
                                      java.util.Map<java.lang.String,​java.util.List<java.lang.String>> headers)
        Set the headers for a connection.
        Parameters:
        connection - The connection to set the headers for
        headers - The headers to set
      • readBody

        public static byte[] readBody​(java.net.HttpURLConnection connection)
                               throws java.io.IOException
        Read the body of a connection.
        Parameters:
        connection - The connection to read the body from
        Returns:
        The body of the connection
        Throws:
        java.io.IOException - If an I/O error occurs
      • readFromStream

        public static byte[] readFromStream​(@WillNotClose
                                            java.io.InputStream is)
                                     throws java.io.IOException
        Read the body of a connection.
        Parameters:
        is - The input stream to read the body from
        Returns:
        The body of the connection
        Throws:
        java.io.IOException - If an I/O error occurs
      • parseHttpDate

        public static java.time.Instant parseHttpDate​(java.lang.String httpDate)
                                               throws java.time.format.DateTimeParseException
        Parse a HTTP date.
        Parameters:
        httpDate - The HTTP date to parse
        Returns:
        The parsed date
        Throws:
        java.time.format.DateTimeParseException - If the date could not be parsed
      • parseSecondsOrHttpDate

        @Nullable
        public static java.lang.Long parseSecondsOrHttpDate​(java.lang.String value)
        Parse an HTTP date or seconds string as milliseconds until the date.
        Parameters:
        value - The value to parse
        Returns:
        The parsed value in milliseconds