java.lang.Object
org.htmlunit.WebResponse
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
StringWebResponse,WebResponseWrapper
A response from a web server.
- Author:
- Mike Bowler, Brad Clarke, Noboru Sinohara, Marc Guillemot, Ahmed Ashour, Ronald Brill, Lai Quang Duong
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionWebResponse(WebResponseData responseData, URL url, HttpMethod requestMethod, long loadTime) Constructs a web response.WebResponse(WebResponseData responseData, WebRequest request, long loadTime) Constructs a web response. -
Method Summary
Modifier and TypeMethodDescriptionvoidcleanUp()Clean up the response data.Returns the reason for blocking or null.Returns the response content as an input stream.INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.Returns the response content as a string, using the charset/encoding specified in the server response.getContentAsString(Charset encoding) Returns the response content as a string, using the specified charset, rather than the charset/encoding specified in the server response.Returns the content charset for this response, even if no charset was specified explicitly.longReturns length of the content data.Returns the response content type.Returns the content charset specified explicitly in theContent-Typeheader ornullif none was specified.longReturns the time it took to load this web response, in milliseconds.Returns the response headers.getResponseHeaderValue(String headerName) Returns the value of the specified response header.intReturns the HTTP status code.Returns the HTTP status message.Returns the request used to load this response.booleanReturns whether the response has a successful HTTP status code.booleanReturns whether the response has a successful HTTP status code.booleanReturns whether the response has a successful HTTP status code.voidmarkAsBlocked(String blockReason) Sets the wasBlocked state to true.booleanReturns whether the request was blocked.booleanReturns whether the charset of the previous call togetContentCharset()was "tentative".
-
Constructor Details
-
WebResponse
Constructs a web response.- Parameters:
responseData- the response dataurl- Where this response came fromrequestMethod- the method used to get this responseloadTime- How long the response took to be sent
-
WebResponse
Constructs a web response.- Parameters:
responseData- the response datarequest- the request used to get this responseloadTime- How long the response took to be sent
-
-
Method Details
-
getWebRequest
Returns the request used to load this response.- Returns:
- the request used to load this response
-
getResponseHeaders
Returns the response headers.- Returns:
- the response headers as a list of
NameValuePairs
-
getResponseHeaderValue
Returns the value of the specified response header.- Parameters:
headerName- the name of the header whose value is to be returned- Returns:
- the header value,
nullif no response header exists with this name
-
getStatusCode
public int getStatusCode()Returns the HTTP status code.- Returns:
- the status code that was returned by the server
-
getStatusMessage
Returns the HTTP status message.- Returns:
- the status message that was returned from the server
-
getContentType
Returns the response content type.- Returns:
- the content type returned from the server, e.g. "text/html"
-
getHeaderContentCharset
Returns the content charset specified explicitly in theContent-Typeheader ornullif none was specified.- Returns:
- the content charset specified header or
nullif none was specified
-
getContentCharset
Returns the content charset for this response, even if no charset was specified explicitly.This method always returns a valid charset. This method first checks the
Content-Typeheader or in the content BOM for viable charset. If not found, it attempts to determine the charset based on the type of the content. As a last resort, this method returns the value ofWebRequest.getDefaultResponseContentCharset()which isStandardCharsets.UTF_8by default.- Returns:
- the content charset for this response
-
wasContentCharsetTentative
public boolean wasContentCharsetTentative()Returns whether the charset of the previous call togetContentCharset()was "tentative".A charset is classed as "tentative" if its detection is prone to false positive/negatives.
For example, HTML meta-tag sniffing can be fooled by text that looks-like-a-meta-tag inside JavaScript code (false positive) or if the meta-tag is after the first 1024 bytes (false negative).
- Returns:
trueif the charset of the previous call togetContentCharset()was "tentative".- See Also:
-
getContentAsString
Returns the response content as a string, using the charset/encoding specified in the server response.- Returns:
- the response content as a string, using the charset/encoding specified in the server response or null if the content retrieval was failing
-
getContentAsString
Returns the response content as a string, using the specified charset, rather than the charset/encoding specified in the server response. If there is a bom header the charset parameter will be overwritten by the bom.- Parameters:
encoding- the charset/encoding to use to convert the response content into a string- Returns:
- the response content as a string or null if the content retrieval was failing
-
getContentLength
public long getContentLength()Returns length of the content data.- Returns:
- the length
-
getContentAsStream
Returns the response content as an input stream.- Returns:
- the response content as an input stream
- Throws:
IOException- in case of IOProblems
-
getContentAsStreamWithBomIfApplicable
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.- Returns:
- the associated InputStream wrapped with a bom input stream if applicable
- Throws:
IOException- in case of IO problems
-
getLoadTime
public long getLoadTime()Returns the time it took to load this web response, in milliseconds.- Returns:
- the time it took to load this web response, in milliseconds
-
cleanUp
public void cleanUp()Clean up the response data. -
isSuccess
public boolean isSuccess()Returns whether the response has a successful HTTP status code.- Returns:
trueif the status code is in the 2xx range
-
isSuccessOrUseProxy
public boolean isSuccessOrUseProxy()Returns whether the response has a successful HTTP status code.- Returns:
trueif the status code is in the 2xx range or 305
-
isSuccessOrUseProxyOrNotModified
public boolean isSuccessOrUseProxyOrNotModified()Returns whether the response has a successful HTTP status code.- Returns:
trueif the status code is in the 2xx range or 305
-
wasBlocked
public boolean wasBlocked()Returns whether the request was blocked.- Returns:
trueif the request was blocked
-
getBlockReason
Returns the reason for blocking or null.- Returns:
- the reason for blocking or null
-
markAsBlocked
Sets the wasBlocked state to true.- Parameters:
blockReason- the reason
-