<!--
{
  "availability" : [
    "iOS: 9.0.0 -",
    "iPadOS: 9.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.11.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/URLSessionStreamTask",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSURLSessionStreamTask"
  },
  "title" : "URLSessionStreamTask"
}
-->

# URLSessionStreamTask

A URL session task that is stream-based.

```
class URLSessionStreamTask
```

## Overview

[`URLSessionStreamTask`](/documentation/Foundation/URLSessionStreamTask) is a concrete subclass of [`URLSessionTask`](/documentation/Foundation/URLSessionTask). Many of the methods in the [`URLSessionStreamTask`](/documentation/Foundation/URLSessionStreamTask) class are documented in [`URLSessionTask`](/documentation/Foundation/URLSessionTask).

The [`URLSessionStreamTask`](/documentation/Foundation/URLSessionStreamTask) class provides an interface a TCP/IP connection created via [`URLSession`](/documentation/Foundation/URLSession). Tasks may be created from an [`URLSession`](/documentation/Foundation/URLSession) using the [`streamTask(withHostName:port:)`](/documentation/Foundation/URLSession/streamTask(withHostName:port:)) and [`streamTask(with:)`](/documentation/Foundation/URLSession/streamTask(with:)) methods. They may also be created as a result of an [`URLSessionDataTask`](/documentation/Foundation/URLSessionDataTask) being upgraded via the HTTP `Upgrade:` response header and appropriate use of the [`httpShouldUsePipelining`](/documentation/Foundation/URLSessionConfiguration/httpShouldUsePipelining) option of [`URLSessionConfiguration`](/documentation/Foundation/URLSessionConfiguration).

> Note:
> See [RFC 2817](https://tools.ietf.org/html/rfc2817) and [RFC 6455](https://tools.ietf.org/html/rfc6455) for information about the `Upgrade:` header.

A [`URLSessionStreamTask`](/documentation/Foundation/URLSessionStreamTask) object performs asynchronous reads and writes, which are enqueued and executed serially, calling a handler upon completion being on the session delegate queue. If the task is canceled, all enqueued reads and writes will call their completion handlers with an appropriate error.

When working with APIs that accept [`Stream`](/documentation/Foundation/Stream) objects, you can create [`InputStream`](/documentation/Foundation/InputStream) and [`OutputStream`](/documentation/Foundation/OutputStream) objects from an [`URLSessionStreamTask`](/documentation/Foundation/URLSessionStreamTask) object by calling the [`captureStreams()`](/documentation/Foundation/URLSessionStreamTask/captureStreams()) method.

> Note:
> watchOS supports ``doc://com.apple.foundation/documentation/Foundation/URLSessionStreamTask`` for specific use cases. For more details, see <doc://com.apple.documentation/documentation/Technotes/tn3135-low-level-networking-on-watchOS>.

## Topics

### Reading and writing data

[`readData(ofMinLength:maxLength:timeout:completionHandler:)`](/documentation/Foundation/URLSessionStreamTask/readData(ofMinLength:maxLength:timeout:completionHandler:))

Asynchronously reads a number of bytes from the stream, and calls a handler upon completion.

[`write(_:timeout:completionHandler:)`](/documentation/Foundation/URLSessionStreamTask/write(_:timeout:completionHandler:))

Asynchronously writes the specified data to the stream, and calls a handler upon completion.

### Capturing streams

[`captureStreams()`](/documentation/Foundation/URLSessionStreamTask/captureStreams())

Completes any already enqueued reads and writes, and then invokes the [`urlSession(_:streamTask:didBecome:outputStream:)`](/documentation/Foundation/URLSessionStreamDelegate/urlSession(_:streamTask:didBecome:outputStream:)) delegate message.

### Closing read and write sockets

[`closeRead()`](/documentation/Foundation/URLSessionStreamTask/closeRead())

Completes any enqueued reads and writes, and then closes the read side of the underlying socket.

[`closeWrite()`](/documentation/Foundation/URLSessionStreamTask/closeWrite())

Completes any enqueued reads and writes, and then closes the write side of the underlying socket.

### Starting and stopping secure connections

[`startSecureConnection()`](/documentation/Foundation/URLSessionStreamTask/startSecureConnection())

Completes any enqueued reads and writes, and establishes a secure connection.

[`stopSecureConnection()`](/documentation/Foundation/URLSessionStreamTask/stopSecureConnection())

Completes any enqueued reads and writes, and closes the secure connection.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)