Movatterモバイル変換


[0]ホーム

URL:


menu
  1. Dart
  2. dart:io
  3. HttpResponse class
HttpResponse
description

HttpResponse classabstractinterface

An HTTP response, which returns the headers and datafrom the server to the client in response to an HTTP request.

Every HttpRequest object provides access to the associatedHttpResponseobject through theresponse property.The server sends its response to the client by writing to theHttpResponse object.

Writing the response

This class implementsIOSink.After the header has been set up, the methodsfrom IOSink, such aswriteln(), can be used to writethe body of the HTTP response.Use theclose() method to close the response and send it to the client.

server.listen((HttpRequest request) {  request.response.write('Hello, world!');  request.response.close();});

When one of the IOSink methods is used for thefirst time, the request header is sent. Calling any methods thatchange the header after it is sent throws an exception.

If no "Content-Type" header is set then a default of"text/plain; charset=utf-8" is used and string data written to the IOSinkwill be encoded using UTF-8.

Setting the headers

The HttpResponse object has a number of properties for setting upthe HTTP headers of the response.When writing string data through the IOSink, the encoding usedis determined from the "charset" parameter of the"Content-Type" header.

HttpResponse response = ...response.headers.contentType    = ContentType("application", "json", charset: "utf-8");response.write(...);  // Strings written will be UTF-8 encoded.

If no charset is provided the default of ISO-8859-1 (Latin 1) willbe used.

HttpResponse response = ...response.headers.add(HttpHeaders.contentTypeHeader, "text/plain");response.write(...);  // Strings written will be ISO-8859-1 encoded.

If a charset is provided but it is not recognized, then the "Content-Type"header will include that charset but string data will be encoded usingISO-8859-1 (Latin 1).

Implemented types

Properties

bufferOutputbool
Gets or sets if theHttpResponse should buffer output.
getter/setter pair
connectionInfoHttpConnectionInfo?
Gets information about the client connection. Returnsnull if thesocket is not available.
no setter
contentLengthint
Gets and sets the content length of the response. If the size ofthe response is not known in advance set the content length to-1, which is also the default if not set.
getter/setter pair
cookiesList<Cookie>
Cookies to set in the client (in the 'set-cookie' header).
no setter
deadlineDuration?
Set and get thedeadline for the response. The deadline is timed from thetime it's set. Setting a new deadline will override any previous deadline.When a deadline is exceeded, the response will be closed and any furtherdata ignored.
getter/setter pair
doneFuture
A future that will complete when the consumer closes, or when anerror occurs.
no setterinherited
encodingEncoding
TheEncoding used when writing strings.
getter/setter pairinherited
hashCodeint
The hash code for this object.
no setterinherited
headersHttpHeaders
Returns the response headers.
no setter
persistentConnectionbool
Gets and sets the persistent connection state. The initial valueof this property is the persistent connection state from therequest.
getter/setter pair
reasonPhraseString
The reason phrase for the response.
getter/setter pair
runtimeTypeType
A representation of the runtime type of the object.
no setterinherited
statusCodeint
The status code of the response.
getter/setter pair

Methods

add(List<int>data)→ void
Adds bytedata to the target consumer, ignoringencoding.
inherited
addError(Objecterror, [StackTrace?stackTrace])→ void
Passes the error to the target consumer as an error event.
inherited
addStream(Stream<List<int>>stream)Future
Adds all elements of the givenstream.
inherited
close()Future
Close the target consumer.
inherited
detachSocket({boolwriteHeaders =true})Future<Socket>
Detaches the underlying socket from the HTTP server. When thesocket is detached the HTTP server will no longer perform anyoperations on it.
flush()Future
Returns aFuture that completes once all buffered data is accepted by theunderlyingStreamConsumer.
inherited
noSuchMethod(Invocationinvocation)→ dynamic
Invoked when a nonexistent method or property is accessed.
inherited
redirect(Urilocation, {intstatus =HttpStatus.movedTemporarily})Future
Respond with a redirect tolocation.
toString()String
A string representation of this object.
inherited
write(Object?object)→ void
Convertsobject to a String by invokingObject.toString andadds the encoding of the result to the target consumer.
inherited
writeAll(Iterableobjects, [Stringseparator =""])→ void
Iterates over the givenobjects andwrites them in sequence.
inherited
writeCharCode(intcharCode)→ void
Writes the character ofcharCode.
inherited
writeln([Object?object =""])→ void
Convertsobject to a String by invokingObject.toString andwrites the result tothis, followed by a newline.
inherited

Operators

operator ==(Objectother)bool
The equality operator.
inherited
  1. Dart
  2. dart:io
  3. HttpResponse class
dart:io library

[8]ページ先頭

©2009-2025 Movatter.jp