Package org.htmlunit
Class WebResponse
- java.lang.Object
-
- org.htmlunit.WebResponse
-
- All Implemented Interfaces:
java.io.Serializable
- Direct Known Subclasses:
StringWebResponse
,WebResponseWrapper
,XMLHttpRequest.NetworkErrorWebResponse
public class WebResponse extends java.lang.Object implements java.io.Serializable
A response from a web server.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.String
blockReason_
private static org.apache.commons.io.ByteOrderMark[]
BOM_HEADERS
private long
loadTime_
private static org.apache.commons.logging.Log
LOG
private WebRequest
request_
private WebResponseData
responseData_
private boolean
wasBlocked_
private boolean
wasContentCharsetTentative_
-
Constructor Summary
Constructors Constructor Description WebResponse(WebResponseData responseData, java.net.URL url, HttpMethod requestMethod, long loadTime)
Constructs with all data.WebResponse(WebResponseData responseData, WebRequest request, long loadTime)
Constructs with all data.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
cleanUp()
Clean up the response data.void
defaultCharsetUtf8()
Deprecated.as of version 4.0.0; useWebRequest.setDefaultResponseContentCharset(Charset)
insteadjava.lang.String
getBlockReason()
java.io.InputStream
getContentAsStream()
Returns the response content as an input stream.java.io.InputStream
getContentAsStreamWithBomIfApplicable()
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.java.lang.String
getContentAsString()
Returns the response content as a string, using the charset/encoding specified in the server response.java.lang.String
getContentAsString(java.nio.charset.Charset encoding)
Returns the response content as a string, using the specified charset, rather than the charset/encoding specified in the server response.java.nio.charset.Charset
getContentCharset()
Returns the content charset for this response, even if no charset was specified explicitly.java.nio.charset.Charset
getContentCharsetOrNull()
Deprecated.as of version 4.0.0; usegetContentCharset()
insteadlong
getContentLength()
Returns length of the content data.java.lang.String
getContentType()
Returns the content type returned from the server, e.g.java.nio.charset.Charset
getHeaderContentCharset()
Returns the content charset specified explicitly in theContent-Type
header ornull
if none was specified.long
getLoadTime()
Returns the time it took to load this web response, in milliseconds.java.util.List<NameValuePair>
getResponseHeaders()
Returns the response headers as a list ofNameValuePair
s.java.lang.String
getResponseHeaderValue(java.lang.String headerName)
Returns the value of the specified response header.int
getStatusCode()
Returns the status code that was returned by the server.java.lang.String
getStatusMessage()
Returns the status message that was returned from the server.WebRequest
getWebRequest()
Returns the request used to load this response.boolean
isSuccess()
boolean
isSuccessOrUseProxy()
boolean
isSuccessOrUseProxyOrNotModified()
void
markAsBlocked(java.lang.String blockReason)
Sets the wasBlocked state to true.boolean
wasBlocked()
boolean
wasContentCharsetTentative()
Returns whether the charset of the previous call togetContentCharset()
was "tentative".
-
-
-
Field Detail
-
LOG
private static final org.apache.commons.logging.Log LOG
-
BOM_HEADERS
private static final org.apache.commons.io.ByteOrderMark[] BOM_HEADERS
-
loadTime_
private final long loadTime_
-
responseData_
private final WebResponseData responseData_
-
request_
private final WebRequest request_
-
wasContentCharsetTentative_
private boolean wasContentCharsetTentative_
-
wasBlocked_
private boolean wasBlocked_
-
blockReason_
private java.lang.String blockReason_
-
-
Constructor Detail
-
WebResponse
public WebResponse(WebResponseData responseData, java.net.URL url, HttpMethod requestMethod, long loadTime)
Constructs with all data.- Parameters:
responseData
- Data that was send backurl
- Where this response came fromrequestMethod
- the method used to get this responseloadTime
- How long the response took to be sent
-
WebResponse
public WebResponse(WebResponseData responseData, WebRequest request, long loadTime)
Constructs with all data.- Parameters:
responseData
- Data that was send backrequest
- the request used to get this responseloadTime
- How long the response took to be sent
-
-
Method Detail
-
getWebRequest
public WebRequest getWebRequest()
Returns the request used to load this response.- Returns:
- the request used to load this response
-
getResponseHeaders
public java.util.List<NameValuePair> getResponseHeaders()
Returns the response headers as a list ofNameValuePair
s.- Returns:
- the response headers as a list of
NameValuePair
s
-
getResponseHeaderValue
public java.lang.String getResponseHeaderValue(java.lang.String headerName)
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,
null
if no response header exists with this name
-
getStatusCode
public int getStatusCode()
Returns the status code that was returned by the server.- Returns:
- the status code that was returned by the server
-
getStatusMessage
public java.lang.String getStatusMessage()
Returns the status message that was returned from the server.- Returns:
- the status message that was returned from the server
-
getContentType
public java.lang.String getContentType()
Returns the content type returned from the server, e.g. "text/html".- Returns:
- the content type returned from the server, e.g. "text/html"
-
getHeaderContentCharset
public java.nio.charset.Charset getHeaderContentCharset()
Returns the content charset specified explicitly in theContent-Type
header ornull
if none was specified.- Returns:
- the content charset specified header or
null
if none was specified
-
getContentCharsetOrNull
@Deprecated public java.nio.charset.Charset getContentCharsetOrNull()
Deprecated.as of version 4.0.0; usegetContentCharset()
insteadReturns the content charset specified explicitly in the header or in the content, ornull
if none was specified.- Returns:
- the content charset specified explicitly in the header or in the content,
or
null
if none was specified
-
getContentCharset
public java.nio.charset.Charset 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-Type
header 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_8
by 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:
true
if the charset of the previous call togetContentCharset()
was "tentative".- See Also:
- https://html.spec.whatwg.org/multipage/parsing.html#concept-encoding-confidence
-
getContentAsString
public java.lang.String 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
public java.lang.String getContentAsString(java.nio.charset.Charset encoding)
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
public java.io.InputStream getContentAsStream() throws java.io.IOException
Returns the response content as an input stream.- Returns:
- the response content as an input stream
- Throws:
java.io.IOException
- in case of IOProblems
-
getContentAsStreamWithBomIfApplicable
public java.io.InputStream getContentAsStreamWithBomIfApplicable() throws java.io.IOException
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:
java.io.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.
-
defaultCharsetUtf8
@Deprecated public void defaultCharsetUtf8()
Deprecated.as of version 4.0.0; useWebRequest.setDefaultResponseContentCharset(Charset)
insteadMark this response for using UTF-8 as default charset.
-
isSuccess
public boolean isSuccess()
- Returns:
- true if the 2xx
-
isSuccessOrUseProxy
public boolean isSuccessOrUseProxy()
- Returns:
- true if the 2xx or 305
-
isSuccessOrUseProxyOrNotModified
public boolean isSuccessOrUseProxyOrNotModified()
- Returns:
- true if the 2xx or 305
-
wasBlocked
public boolean wasBlocked()
- Returns:
- true if the request was blocked
-
getBlockReason
public java.lang.String getBlockReason()
- Returns:
- the reason for blocking or null
-
markAsBlocked
public void markAsBlocked(java.lang.String blockReason)
Sets the wasBlocked state to true.- Parameters:
blockReason
- the reason
-
-