---
title: URLSessionStreamTask
framework: foundation
role: symbol
role_heading: Class
path: foundation/urlsessionstreamtask
---

# URLSessionStreamTask

A URL session task that is stream-based.

## Declaration

```swift
class URLSessionStreamTask
```

## Overview

Overview URLSessionStreamTask is a concrete subclass of URLSessionTask. Many of the methods in the URLSessionStreamTask class are documented in URLSessionTask. The URLSessionStreamTask class provides an interface a TCP/IP connection created via URLSession. Tasks may be created from an URLSession using the streamTask(withHostName:port:) and streamTask(with:) methods. They may also be created as a result of an URLSessionDataTask being upgraded via the HTTP Upgrade: response header and appropriate use of the httpShouldUsePipelining option of URLSessionConfiguration. note: See RFC 2817 and RFC 6455 for information about the Upgrade: header. A 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 objects, you can create InputStream and OutputStream objects from an URLSessionStreamTask object by calling the captureStreams() method. note: watchOS supports URLSessionStreamTask for specific use cases. For more details, see TN3135: Low-level networking on watchOS.

## Topics

### Reading and writing data

- [readData(ofMinLength:maxLength:timeout:completionHandler:)](foundation/urlsessionstreamtask/readdata(ofminlength:maxlength:timeout:completionhandler:).md)
- [write(_:timeout:completionHandler:)](foundation/urlsessionstreamtask/write(_:timeout:completionhandler:).md)

### Capturing streams

- [captureStreams()](foundation/urlsessionstreamtask/capturestreams().md)

### Closing read and write sockets

- [closeRead()](foundation/urlsessionstreamtask/closeread().md)
- [closeWrite()](foundation/urlsessionstreamtask/closewrite().md)

### Starting and stopping secure connections

- [startSecureConnection()](foundation/urlsessionstreamtask/startsecureconnection().md)
- [stopSecureConnection()](foundation/urlsessionstreamtask/stopsecureconnection().md)

### Initializers

- [init()](foundation/urlsessionstreamtask/init().md)

### Type Methods

- [new()](foundation/urlsessionstreamtask/new().md)

## Relationships

### Inherits From

- [URLSessionTask](foundation/urlsessiontask.md)

### Conforms To

- [CVarArg](swift/cvararg.md)
- [CustomDebugStringConvertible](swift/customdebugstringconvertible.md)
- [CustomStringConvertible](swift/customstringconvertible.md)
- [Equatable](swift/equatable.md)
- [Hashable](swift/hashable.md)
- [NSCopying](foundation/nscopying.md)
- [NSObjectProtocol](objectivec/nsobjectprotocol.md)
- [ProgressReporting](foundation/progressreporting.md)
- [Sendable](swift/sendable.md)
- [SendableMetatype](swift/sendablemetatype.md)

## See Also

### Adding stream tasks to a session

- [streamTask(withHostName:port:)](foundation/urlsession/streamtask(withhostname:port:).md)
- [streamTask(with:)](foundation/urlsession/streamtask(with:).md)
- [URLSessionStreamDelegate](foundation/urlsessionstreamdelegate.md)
