public class HTTPServer.Response extends Object implements Closeable
Response class encapsulates a single HTTP response.| 限定符和类型 | 字段和说明 |
|---|---|
protected boolean |
discardBody |
protected OutputStream |
encodedOut |
protected HTTPServer.Headers |
headers |
protected OutputStream |
out |
protected HTTPServer.Request |
req |
protected int |
state |
| 构造器和说明 |
|---|
Response(OutputStream out)
Constructs a Response whose output is written to the given stream.
|
| 限定符和类型 | 方法和说明 |
|---|---|
void |
close()
Closes this response and flushes all output.
|
OutputStream |
getBody()
Returns an output stream into which the response body can be written.
|
HTTPServer.Headers |
getHeaders()
Returns the request headers collection.
|
OutputStream |
getOutputStream()
Returns the underlying output stream to which the response is written.
|
boolean |
headersSent()
Returns whether the response headers were already sent.
|
void |
redirect(String url,
boolean permanent)
Sends a 301 or 302 response, redirecting the client to the given URL.
|
void |
send(int status,
String text)
Sends the full response with the given status, and the given string
as the body.
|
void |
sendBody(InputStream body,
long length,
long[] range)
Sends the response body.
|
void |
sendError(int status)
Sends an error response with the given status and default body.
|
void |
sendError(int status,
String text)
Sends an error response with the given status and detailed message.
|
void |
sendHeaders(int status)
Sends the response headers with the given response status.
|
void |
sendHeaders(int status,
long length,
long lastModified,
String etag,
String contentType,
long[] range)
Sends the response headers, including the given response status
and description, and all response headers.
|
void |
setClientCapabilities(HTTPServer.Request req)
Sets the request which is used in determining the capabilities
supported by the client (e.g. compression, encoding, etc.)
|
void |
setDiscardBody(boolean discardBody)
Sets whether this response's body is discarded or sent.
|
protected OutputStream out
protected OutputStream encodedOut
protected HTTPServer.Headers headers
protected boolean discardBody
protected int state
protected HTTPServer.Request req
public Response(OutputStream out)
out - the stream to which the response is writtenpublic void setDiscardBody(boolean discardBody)
discardBody - specifies whether the body is discarded or notpublic void setClientCapabilities(HTTPServer.Request req)
req - the requestpublic HTTPServer.Headers getHeaders()
public OutputStream getOutputStream()
getBody() instead.public boolean headersSent()
public OutputStream getBody() throws IOException
IOException - if an error occurspublic void close()
throws IOException
close 在接口中 Closeableclose 在接口中 AutoCloseableIOException - if an error occurspublic void sendHeaders(int status)
throws IOException
status - the response statusIOException - if an error occurs or headers were already sentsendHeaders(int, long, long, String, String, long[])public void sendHeaders(int status,
long length,
long lastModified,
String etag,
String contentType,
long[] range)
throws IOException
status - the response statuslength - the response body length, or zero if there is no body,
or negative if there is a body but its length is not yet knownlastModified - the last modified date of the response resource,
or non-positive if unknown. A time in the future will be
replaced with the current system time.etag - the ETag of the response resource, or null if unknown
(see RFC2616#3.11)contentType - the content type of the response resource, or null
if unknown (in which case "application/octet-stream" will be sent)range - the content range that will be sent, or null if the
entire resource will be sentIOException - if an error occurspublic void send(int status,
String text)
throws IOException
escaped) HTML.status - the response statustext - the text body (sent as text/html)IOException - if an error occurspublic void sendError(int status,
String text)
throws IOException
escape method.status - the response statustext - the text body (sent as text/html)IOException - if an error occurspublic void sendError(int status)
throws IOException
status - the response statusIOException - if an error occurspublic void sendBody(InputStream body, long length, long[] range) throws IOException
body - a stream containing the response bodylength - the full length of the response body, or -1 for the whole streamrange - the sub-range within the response body that should be
sent, or null if the entire body should be sentIOException - if an error occurspublic void redirect(String url, boolean permanent) throws IOException
url - the absolute URL to which the client is redirectedpermanent - specifies whether a permanent (301) or
temporary (302) redirect status is sentIOException - if an IO error occurs or url is malformedCopyright © 2022. All rights reserved.