# HttpUtil

Artifact: [androidx.media3:media3-datasource](https://developer.android.com/jetpack/androidx/releases/media3) [View Source](https://cs.android.com/search?q=file:androidx/media3/datasource/HttpUtil.java+class:androidx.media3.datasource.HttpUtil)

*** ** * ** ***

<br />

```
@UnstableApi
class HttpUtil
```

<br />

*** ** * ** ***

Utility methods for HTTP.

## Summary

| ### Public functions |
|---|---|
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html?` | `https://developer.android.com/reference/kotlin/androidx/media3/datasource/HttpUtil#buildRangeRequestHeader(long,long)(position: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, length: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)` Builds a `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/net/HttpHeaders.html#RANGE` for the given position and length. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/datasource/HttpUtil#getContentLength(java.lang.String,java.lang.String)(contentLengthHeader: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html?, contentRangeHeader: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html?)` Attempts to parse the length of a response body from the corresponding response headers. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!` | `https://developer.android.com/reference/kotlin/androidx/media3/datasource/HttpUtil#getCookieHeader(java.lang.String,java.util.Map<java.lang.String,java.util.List<java.lang.String>>,java.net.CookieHandler)( url: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!, headers: (https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-map/index.html)https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-map/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!, (https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/index.html)https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!>!>!, cookieHandler: https://developer.android.com/reference/java/net/CookieHandler.html? )` Returns the stored cookie header for the given response URL and headers. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/datasource/HttpUtil#getDocumentSize(java.lang.String)(contentRangeHeader: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html?)` Attempts to parse the document size from a `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/net/HttpHeaders.html#CONTENT_RANGE`. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/datasource/HttpUtil#storeCookiesFromHeaders(java.lang.String,java.util.Map<java.lang.String,java.util.List<java.lang.String>>,java.net.CookieHandler)( url: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!, headers: (https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-map/index.html)https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-map/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!, (https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/index.html)https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!>!>!, cookieHandler: https://developer.android.com/reference/java/net/CookieHandler.html? )` Stores the cookie headers from the response in the provided `https://developer.android.com/reference/java/net/CookieHandler.html`. |

## Public functions

### buildRangeRequestHeader

```
java-static fun buildRangeRequestHeader(position: Long, length: Long): String?
```

Builds a `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/net/HttpHeaders.html#RANGE` for the given position and length.

| Parameters |
|---|---|
| `position: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The request position. |
| `length: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The request length, or `https://developer.android.com/reference/kotlin/androidx/media3/common/C#LENGTH_UNSET()` if the request is unbounded. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html?` | The corresponding range header, or `null` if a header is unnecessary because the whole resource is being requested. |

### getContentLength

```
java-static fun getContentLength(contentLengthHeader: String?, contentRangeHeader: String?): Long
```

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

| Parameters |
|---|---|
| `contentLengthHeader: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html?` | The `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/net/HttpHeaders.html#CONTENT_LENGTH`, or `null` if not set. |
| `contentRangeHeader: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html?` | The `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/net/HttpHeaders.html#CONTENT_RANGE`, or ` null` if not set. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The length of the response body, or `https://developer.android.com/reference/kotlin/androidx/media3/common/C#LENGTH_UNSET()` if it could not be determined. |

### getCookieHeader

```
java-static fun getCookieHeader(
    url: String!,
    headers: (Mutable)Map<String!, (Mutable)List<String!>!>!,
    cookieHandler: CookieHandler?
): String!
```

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

| Parameters |
|---|---|
| `url: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!` | The URL of the response. |
| `headers: (https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-map/index.html)https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-map/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!, (https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/index.html)https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!>!>!` | The headers of the response. |
| `cookieHandler: https://developer.android.com/reference/java/net/CookieHandler.html?` | The `https://developer.android.com/reference/java/net/CookieHandler.html` to store the cookies in. If null, no cookies will be available. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!` | The cookie header string that can be added to requests with `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/net/HttpHeaders.html#COOKIE`. |

### getDocumentSize

```
java-static fun getDocumentSize(contentRangeHeader: String?): Long
```

Attempts to parse the document size from a `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/net/HttpHeaders.html#CONTENT_RANGE`.

| Parameters |
|---|---|
| `contentRangeHeader: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html?` | The `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/net/HttpHeaders.html#CONTENT_RANGE`, or ` null` if not set. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The document size, or `https://developer.android.com/reference/kotlin/androidx/media3/common/C#LENGTH_UNSET()` if it could not be determined. |

### storeCookiesFromHeaders

```
java-static fun storeCookiesFromHeaders(
    url: String!,
    headers: (Mutable)Map<String!, (Mutable)List<String!>!>!,
    cookieHandler: CookieHandler?
): Unit
```

Stores the cookie headers from the response in the provided `https://developer.android.com/reference/java/net/CookieHandler.html`.

| Parameters |
|---|---|
| `url: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!` | The URL of the response. |
| `headers: (https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-map/index.html)https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-map/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!, (https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/index.html)https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!>!>!` | The headers of the response. |
| `cookieHandler: https://developer.android.com/reference/java/net/CookieHandler.html?` | The `https://developer.android.com/reference/java/net/CookieHandler.html` to store the cookies in. If null, no cookies will be stored. |