Package org.htmlunit
Class WebResponse
- java.lang.Object
-
- org.htmlunit.WebResponse
-
- All Implemented Interfaces:
java.io.Serializable
- Direct Known Subclasses:
StringWebResponse,WebResponseWrapper
public class WebResponse extends java.lang.Object implements java.io.SerializableA response from a web server.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static intFORBIDDENForwarder to HttpStatus.SC_FORBIDDEN.static intINTERNAL_SERVER_ERRORForwarder to HttpStatus.SC_INTERNAL_SERVER_ERROR.static intNO_CONTENTForwarder to HttpStatus.SC_NO_CONTENT.static intNOT_FOUNDForwarder to HttpStatus.SC_NOT_FOUND.static intOKForwarder to HttpStatus.SC_OK.
-
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 voidcleanUp()Clean up the response data.voiddefaultCharsetUtf8()Mark this response for using UTF-8 as default charset.java.io.InputStreamgetContentAsStream()Returns the response content as an input stream.java.io.InputStreamgetContentAsStreamWithBomIfApplicable()INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.java.lang.StringgetContentAsString()Returns the response content as a string, using the charset/encoding specified in the server response.java.lang.StringgetContentAsString(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.lang.StringgetContentAsString(java.nio.charset.Charset encoding, boolean ignoreUtf8Bom)INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Returns the response content as a string, using the specified charset, rather than the charset/encoding specified in the server response.java.nio.charset.CharsetgetContentCharset()Returns the content charset for this response, even if no charset was specified explicitly.java.nio.charset.CharsetgetContentCharsetOrNull()Returns the content charset specified explicitly in the header or in the content, ornullif none was specified.longgetContentLength()Returns length of the content data.java.lang.StringgetContentType()Returns the content type returned from the server, e.g.longgetLoadTime()Returns the time it took to load this web response, in milliseconds.java.lang.StringgetProtocolVersion()Deprecated.For internal use only.intgetRawSize()Deprecated.For internal use only.java.util.List<NameValuePair>getResponseHeaders()Returns the response headers as a list ofNameValuePairs.java.lang.StringgetResponseHeaderValue(java.lang.String headerName)Returns the value of the specified response header.intgetStatusCode()Returns the status code that was returned by the server.java.lang.StringgetStatusMessage()Returns the status message that was returned from the server.WebRequestgetWebRequest()Returns the request used to load this response.booleanisSuccess()booleanisSuccessOrUseProxy()booleanisSuccessOrUseProxyOrNotModified()voidsetProtocolVersion(java.lang.String protocolVersion)Deprecated.For internal use only.voidsetRawSize(int size)Deprecated.For internal use only.
-
-
-
Field Detail
-
OK
public static final int OK
Forwarder to HttpStatus.SC_OK.- See Also:
- Constant Field Values
-
FORBIDDEN
public static final int FORBIDDEN
Forwarder to HttpStatus.SC_FORBIDDEN.- See Also:
- Constant Field Values
-
NOT_FOUND
public static final int NOT_FOUND
Forwarder to HttpStatus.SC_NOT_FOUND.- See Also:
- Constant Field Values
-
NO_CONTENT
public static final int NO_CONTENT
Forwarder to HttpStatus.SC_NO_CONTENT.- See Also:
- Constant Field Values
-
INTERNAL_SERVER_ERROR
public static final int INTERNAL_SERVER_ERROR
Forwarder to HttpStatus.SC_INTERNAL_SERVER_ERROR.- See Also:
- Constant Field Values
-
-
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 ofNameValuePairs.- Returns:
- the response headers as a list of
NameValuePairs
-
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,
nullif 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"
-
getContentCharsetOrNull
public java.nio.charset.Charset getContentCharsetOrNull()
Returns the content charset specified explicitly in the header or in the content, ornullif none was specified.- Returns:
- the content charset specified explicitly in the header or in the content,
or
nullif 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 theContent-Typeheader; if not found, it checks the request charset; as a last resort, this method returnsStandardCharsets.ISO_8859_1. If no charset is defined for an xml response, then UTF-8 is used- Returns:
- the content charset for this response
- See Also:
- Character Encoding
-
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
-
getContentAsString
public java.lang.String getContentAsString(java.nio.charset.Charset encoding, boolean ignoreUtf8Bom)INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
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 stringignoreUtf8Bom- if true utf8 bom header will be ignored- 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.IOExceptionReturns 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.IOExceptionINTERNAL 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
public void defaultCharsetUtf8()
Mark 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
-
getRawSize
@Deprecated public int getRawSize()
Deprecated.For internal use only. Will be removed in next release.Returns the raw size of this response.- Returns:
- this response's raw size
-
setRawSize
@Deprecated public void setRawSize(int size)
Deprecated.For internal use only. Will be removed in next release.Sets the raw size of this response.- Parameters:
size- the raw size in bytes
-
getProtocolVersion
@Deprecated public java.lang.String getProtocolVersion()
Deprecated.For internal use only.Returns the protocol version of this response, for example "HTTP/1.1".- Returns:
- the protocol version
-
setProtocolVersion
@Deprecated public void setProtocolVersion(java.lang.String protocolVersion)
Deprecated.For internal use only.Sets the protocol version of this response, for example "HTTP/1.1".- Parameters:
protocolVersion- the protocol version
-
-