Httpresponse class in java

HttpResponse

The HttpServlet class request processing methods take two parameters.

For instance, here is the signature of the HttpServlet.doGet() method:

protected void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException

In this text I will look at the HttpResponse object.

The purpose of the HttpResponse object is to represent the HTTP response your web application sends back to the browser, in response to the HTTP request the browser send to your web application.

The HttpResponse object has a lot of methods, so I will just cover the most commonly used here. The rest you can read about in the JavaDoc, if you are interested.

Writing HTML

To send HTML back to the browser, you have to obtain the a PrintWriter from the HttpResponse object. Here is how:

PrintWriter writer = response.getWriter(); writer.write("GET/POST response");

Headers

Just like the request object, the HttpRequest can contain HTTP headers. Headers must be set before any data is written to the response. You set a header on the response object like this:

response.setHeader("Header-Name", "Header Value");

As you can see, a response header is a name, value pair.

Content-Type

The Content-Type header is a response header that tells the browser the type of the content you are sending back to it. For instance, the content type for HTML is text/html . Similarly, if what you send back to the browser is plain text, you use the content type text/plain .

Here is how you set the Content-Type header on the HttpResponse object:

response.setHeader("Content-Type", "text/html");

Writing Text

You can write text back to the browser instead of HTML, like this:

response.setHeader("Content-Type", "text/plain"); PrintWriter writer = response.getWriter(); writer.write("This is just plain text");

First the Content-Type header is set to text/plain . Then a plain text string is written to the writer obtained from the response object.

Content-Length

The Content-Length header tells the browser how many bytes your servlet is sending back. If you are sending binary data back you need to set the content length header. Here is how:

response.setHeader("Content-Length", "31642");

Writing Binary Data

You can also write binary data back to the browser instead of text. For instance, you can send an image back, a PDF file or a Flash file or something like that.

Again, you will first have to set the Content-Type header to the type matching the data you are sending back. For instance, the content type for a PNG image is image/png .

You can search for «mime types» in your favourite search engine to find a list of mime types (content types), so you can find the mime type for the content you are sending back.

In order to write binary data back to the browser you cannot use the Writer obtained from response.getWriter() . Afterall, Writer ‘s are intended for text.

Instead you have to use the OutputStream obtained from the response.getOutputStream() method. Here is how:

OutputStream outputStream = response.getOutputStream(); outputStream.write(. );

Redirecting to a Different URL

You can redirect the browser to a different URL from your servlet. You cannot send any data back to the browser when redirecting. Here is how you redirect:

response.sendRedirect("http://jenkov.com");

Источник

Httpresponse class in java

An HTTP response. An HttpResponse is not created directly, but rather returned as a result of sending an HttpRequest . An HttpResponse is made available when the response status code and headers have been received, and typically after the response body has also been completely received. Whether or not the HttpResponse is made available before the response body has been completely received depends on the BodyHandler provided when sending the HttpRequest . This class provides methods for accessing the response status code, headers, the response body, and the HttpRequest corresponding to this response. The following is an example of retrieving a response as a String:

 HttpResponse response = client .send(request, BodyHandlers.ofString()); 

The class BodyHandlers provides implementations of many common response handlers. Alternatively, a custom BodyHandler implementation can be used.

Nested Class Summary

Implementations of BodyHandler that implement various useful handlers, such as handling the response body as a String, or streaming the response body to a file.

Implementations of BodySubscriber that implement various useful subscribers, such as converting the response body bytes into a String, or streaming the bytes to a file.

Initial response information supplied to a BodyHandler when a response is initially received and before the body is processed.

Method Summary

Method Detail

statusCode

request

Returns the HttpRequest corresponding to this response. The returned HttpRequest may not be the initiating request provided when sending. For example, if the initiating request was redirected, then the request returned by this method will have the redirected URI, which will be different from the initiating request URI.

previousResponse

OptionalHttpResponseT>> previousResponse()

Returns an Optional containing the previous intermediate response if one was received. An intermediate response is one that is received as a result of redirection or authentication. If no previous response was received then an empty Optional is returned.

headers

body

Returns the body. Depending on the type of T , the returned body may represent the body after it was read (such as byte[] , or String , or Path ) or it may represent an object with which the body is read, such as an InputStream . If this HttpResponse was returned from an invocation of previousResponse() then this method returns null

sslSession

Returns an Optional containing the SSLSession in effect for this response. Returns an empty Optional if this is not a HTTPS response.

uri

Returns the URI that the response was received from. This may be different from the request URI if redirection occurred.

version

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.

Источник

Http Response Class

Gets a new HttpResponse object wrapping this response with its content buffered into memory.

Closes the response content stream, if any.

Get the publisher emitting response content chunks.

Gets the BinaryData that represents the body of the response.

Gets the response content as a byte[] .

Gets the response content as an InputStream .

Gets the response content as a String .

Gets the response content as a String .

Lookup a response header with the provider HttpHeaderName.

Lookup a response header with the provided name.

Gets the HttpRequest which resulted in this response.

Get the response status code.

Transfers body bytes to the WritableByteChannel .

Transfers body bytes to the AsynchronousByteChannel .

Methods inherited from java.lang.Object

Constructor Details

HttpResponse

protected HttpResponse(HttpRequest request)

Method Details

buffer

public HttpResponse buffer()

Gets a new HttpResponse object wrapping this response with its content buffered into memory.

close

Closes the response content stream, if any.

getBody

public abstract Flux getBody()

Get the publisher emitting response content chunks.

Returns a stream of the response’s body content. Emissions may occur on Reactor threads which should not be blocked. Blocking should be avoided as much as possible/practical in reactive programming but if you do use methods like block() on the stream then be sure to use publishOn before the blocking call.

getBodyAsBinaryData

public BinaryData getBodyAsBinaryData()

Gets the BinaryData that represents the body of the response.

Subclasses should override this method.

getBodyAsByteArray

public abstract Mono getBodyAsByteArray()

Gets the response content as a byte[] .

getBodyAsInputStream

public Mono getBodyAsInputStream()

Gets the response content as an InputStream .

getBodyAsString

public abstract Mono getBodyAsString()

Gets the response content as a String .

By default, this method will inspect the response body for containing a byte order mark (BOM) to determine the encoding of the string (UTF-8, UTF-16, etc.). If a BOM isn’t found this will default to using UTF-8 as the encoding, if a specific encoding is required use getBodyAsString(Charset charset).

getBodyAsString

public abstract Mono getBodyAsString(Charset charset)

Gets the response content as a String .

getHeaderValue

public String getHeaderValue(HttpHeaderName headerName)

Lookup a response header with the provider HttpHeaderName.

getHeaderValue

@Deprecated
public abstract String getHeaderValue(String name)

Lookup a response header with the provided name.

getHeaders

public abstract HttpHeaders getHeaders()

getRequest

public final HttpRequest getRequest()

Gets the HttpRequest which resulted in this response.

getStatusCode

public abstract int getStatusCode()

Get the response status code.

writeBodyTo

public void writeBodyTo(WritableByteChannel channel)

Transfers body bytes to the WritableByteChannel .

writeBodyToAsync

public Mono writeBodyToAsync(AsynchronousByteChannel channel)

Transfers body bytes to the AsynchronousByteChannel .

Источник

Http Response Class

Gets a new HttpResponse object wrapping this response with its content buffered into memory.

Closes the response content stream, if any.

Get the publisher emitting response content chunks.

Gets the BinaryData that represents the body of the response.

Gets the response content as a byte[] .

Gets the response content as an InputStream .

Gets the response content as a String .

Gets the response content as a String .

Lookup a response header with the provider HttpHeaderName.

Lookup a response header with the provided name.

Gets the HttpRequest which resulted in this response.

Get the response status code.

Transfers body bytes to the WritableByteChannel .

Transfers body bytes to the AsynchronousByteChannel .

Methods inherited from java.lang.Object

Constructor Details

HttpResponse

protected HttpResponse(HttpRequest request)

Method Details

buffer

public HttpResponse buffer()

Gets a new HttpResponse object wrapping this response with its content buffered into memory.

close

Closes the response content stream, if any.

getBody

public abstract Flux getBody()

Get the publisher emitting response content chunks.

Returns a stream of the response’s body content. Emissions may occur on Reactor threads which should not be blocked. Blocking should be avoided as much as possible/practical in reactive programming but if you do use methods like block() on the stream then be sure to use publishOn before the blocking call.

getBodyAsBinaryData

public BinaryData getBodyAsBinaryData()

Gets the BinaryData that represents the body of the response.

Subclasses should override this method.

getBodyAsByteArray

public abstract Mono getBodyAsByteArray()

Gets the response content as a byte[] .

getBodyAsInputStream

public Mono getBodyAsInputStream()

Gets the response content as an InputStream .

getBodyAsString

public abstract Mono getBodyAsString()

Gets the response content as a String .

By default, this method will inspect the response body for containing a byte order mark (BOM) to determine the encoding of the string (UTF-8, UTF-16, etc.). If a BOM isn’t found this will default to using UTF-8 as the encoding, if a specific encoding is required use getBodyAsString(Charset charset).

getBodyAsString

public abstract Mono getBodyAsString(Charset charset)

Gets the response content as a String .

getHeaderValue

public String getHeaderValue(HttpHeaderName headerName)

Lookup a response header with the provider HttpHeaderName.

getHeaderValue

@Deprecated
public abstract String getHeaderValue(String name)

Lookup a response header with the provided name.

getHeaders

public abstract HttpHeaders getHeaders()

getRequest

public final HttpRequest getRequest()

Gets the HttpRequest which resulted in this response.

getStatusCode

public abstract int getStatusCode()

Get the response status code.

writeBodyTo

public void writeBodyTo(WritableByteChannel channel)

Transfers body bytes to the WritableByteChannel .

writeBodyToAsync

public Mono writeBodyToAsync(AsynchronousByteChannel channel)

Transfers body bytes to the AsynchronousByteChannel .

Источник

Читайте также:  Toshiba om net firmware php
Оцените статью