@UnstableApi
public final class HttpUtil


Utility methods for HTTP.

Summary

Public methods

static @Nullable String
buildRangeRequestHeader(long position, long length)

Builds a Range header for the given position and length.

static long
getContentLength(
    @Nullable String contentLengthHeader,
    @Nullable String contentRangeHeader
)

Attempts to parse the length of a response body from the corresponding response headers.

static String
getCookieHeader(
    String url,
    Map<StringList<String>> headers,
    @Nullable CookieHandler cookieHandler
)

Returns the stored cookie header for the given response URL and headers.

static long
getDocumentSize(@Nullable String contentRangeHeader)

Attempts to parse the document size from a Content-Range header.

static void
storeCookiesFromHeaders(
    String url,
    Map<StringList<String>> headers,
    @Nullable CookieHandler cookieHandler
)

Stores the cookie headers from the response in the provided CookieHandler.

Public methods

buildRangeRequestHeader

public static @Nullable String buildRangeRequestHeader(long position, long length)

Builds a Range header for the given position and length.

Parameters
long position

The request position.

long length

The request length, or LENGTH_UNSET if the request is unbounded.

Returns
@Nullable String

The corresponding range header, or null if a header is unnecessary because the whole resource is being requested.

getContentLength

public static long getContentLength(
    @Nullable String contentLengthHeader,
    @Nullable String contentRangeHeader
)

Attempts to parse the length of a response body from the corresponding response headers.

Parameters
@Nullable String contentLengthHeader

The Content-Length header, or null if not set.

@Nullable String contentRangeHeader

The Content-Range header, or null if not set.

Returns
long

The length of the response body, or LENGTH_UNSET if it could not be determined.

getCookieHeader

public static String getCookieHeader(
    String url,
    Map<StringList<String>> headers,
    @Nullable CookieHandler cookieHandler
)

Returns the stored cookie header for the given response URL and headers.

Parameters
String url

The URL of the response.

Map<StringList<String>> headers

The headers of the response.

@Nullable CookieHandler cookieHandler

The CookieHandler to store the cookies in. If null, no cookies will be available.

Returns
String

The cookie header string that can be added to requests with COOKIE.

getDocumentSize

public static long getDocumentSize(@Nullable String contentRangeHeader)

Attempts to parse the document size from a Content-Range header.

Parameters
@Nullable String contentRangeHeader

The Content-Range header, or null if not set.

Returns
long

The document size, or LENGTH_UNSET if it could not be determined.

storeCookiesFromHeaders

public static void storeCookiesFromHeaders(
    String url,
    Map<StringList<String>> headers,
    @Nullable CookieHandler cookieHandler
)

Stores the cookie headers from the response in the provided CookieHandler.

Parameters
String url

The URL of the response.

Map<StringList<String>> headers

The headers of the response.

@Nullable CookieHandler cookieHandler

The CookieHandler to store the cookies in. If null, no cookies will be stored.