- All Implemented Interfaces:
Serializable
- Author:
- Brad Clarke, Hans Donner, Ahmed Ashour, Marc Guillemot, Rodney Gitzel, Ronald Brill, Adam Afeltowicz, Joerg Werner, Michael Lueck, Lai Quang Duong, Kristof Neirynck
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumThe destination of a request, as defined by the Fetch spec ( https://fetch.spec.whatwg.org/#concept-request-destination).static enumThe mode of a request, as defined by the Fetch spec ( https://fetch.spec.whatwg.org/#concept-request-mode).static enumEnum to configure request creation. -
Constructor Summary
ConstructorsConstructorDescriptionWebRequest(URL url) Instantiates aWebRequestfor the specified URL.WebRequest(URL url, String acceptHeader, String acceptEncodingHeader) Instantiates aWebRequestfor the specified URL, setting the Accept and Accept-Encoding headers if provided.WebRequest(URL url, Charset charset, URL refererUrl) Instantiates aWebRequestfor the specified URL, using the given charset and referer.WebRequest(URL url, HttpMethod submitMethod) Instantiates aWebRequestfor the specified URL using the given HTTP submit method. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddHint(WebRequest.HttpHint hint) Enables the givenWebRequest.HttpHintfor this request.getAdditionalHeader(String name) Returns the value of the specified additional HTTP header, ornullif the header is not set.Returns the additional HTTP headers to use.Returns the character set to use to perform the request.org.apache.http.auth.CredentialsReturns the credentials explicitly set viasetCredentials(Credentials), ornullif none were set.Returns the default character set to use for the response when it does not specify one.Returns the form encoding type to use.Returns the destination of this request, used to compute theSec-Fetch-Destheader (and, unless overridden, the defaultSec-Fetch-Mode).Returns the explicit mode override for this request, if any.Returns the HTTP submit method to use.Retrieves the request parameters used.Returns the proxy host to use.intReturns the proxy port to use.Returns the proxy scheme to use.Returns the body content to be submitted if this is aPOSTrequest.Returns the URL of the document or script that initiated this request, used to compute theSec-Fetch-Siteheader.INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Returns the request parameters to use.intReturns the timeout to use.getUrl()Returns the target URL.org.apache.http.auth.CredentialsReturns the credentials extracted from the URL's userinfo component (e.g.booleanhasHint(WebRequest.HttpHint hint) Returns whether the givenWebRequest.HttpHintis currently enabled for this request.booleanisAdditionalHeader(String name) Returns whether the specified header name is already included in the additional HTTP headers.booleanReturns whether SOCKS proxy or not.booleanReturns whether this request is the result of a navigation backed by genuine user activation (e.g. a click on a link, a typed URL, or a form submitted via a click on its submit button) as opposed to one triggered purely by script (e.g.voidmarkAsNavigation(URL requestingUrl, boolean userActivation) Convenience method for the common case of a top-level navigation (an anchor/area click, a form submission, a script-driven location change, ...): setsWebRequest.FetchDestination.DOCUMENT, the initiator URL, and whether the navigation was backed by genuine user activation, all in one call.voidmarkAsNavigation(WebRequest.FetchDestination destination, URL requestingUrl, boolean userActivation) Same asmarkAsNavigation(URL, boolean), but for navigations whose destination isn't a top-levelWebRequest.FetchDestination.DOCUMENT- currently only<iframe>/<frame>loads (WebRequest.FetchDestination.IFRAME/WebRequest.FetchDestination.FRAME).static WebRequestReturns a new request for about:blank.voidremoveAdditionalHeader(String name) Removes the specified header from the additional HTTP headers.voidsetAdditionalHeader(String name, String value) Sets the specified name/value pair in the additional HTTP headers, replacing any existing header with the same name (case-insensitive).voidsetAdditionalHeaders(Map<String, String> additionalHeaders) Sets the additional HTTP headers to use.voidsetCharset(Charset charset) Sets the character set to use to perform the request.voidsetCredentials(org.apache.http.auth.Credentials credentials) Sets the credentials to use.voidsetDefaultResponseContentCharset(Charset defaultResponseContentCharset) Sets the default character set to use when the response does not declare one explicitly.voidsetEncodingType(FormEncodingType encodingType) Sets the form encoding type to use.voidsetFetchDestination(WebRequest.FetchDestination fetchDestination) Sets the destination of this request.voidsetFetchModeOverride(WebRequest.FetchMode fetchMode) Sets an explicit mode override for this request, e.g. for afetch()call using an explicitmodeoption, or a subresource request using thecrossoriginattribute (which forces CORS mode).voidsetHttpMethod(HttpMethod submitMethod) Sets the HTTP submit method to use.voidsetProxyHost(String proxyHost) Sets the proxy host to use.voidsetProxyPort(int proxyPort) Sets the proxy port to use.voidsetProxyScheme(String proxyScheme) Sets the proxy scheme to use.voidsetRefererHeader(URL url) Sets theRefererHTTP header to the external form of the given URL.voidsetRequestBody(String requestBody) Sets the body content to be submitted if this is aPOST,PUTorPATCHrequest.voidsetRequestingUrl(URL requestingUrl) Sets the URL of the document or script that initiated this request.voidsetRequestParameters(List<NameValuePair> requestParameters) INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Sets the request parameters to use.voidsetSocksProxy(boolean isSocksProxy) Sets whether SOCKS proxy or not.voidsetTimeout(int timeout) Sets the timeout to use.voidSets the target URL.voidsetUserActivation(boolean userActivation) Sets whether this request is the result of a navigation backed by genuine user activation.toString()
-
Constructor Details
-
WebRequest
Instantiates aWebRequestfor the specified URL, setting the Accept and Accept-Encoding headers if provided.- Parameters:
url- the target URLacceptHeader- the accept header to useacceptEncodingHeader- the accept encoding header to use
-
WebRequest
Instantiates aWebRequestfor the specified URL, using the given charset and referer.- Parameters:
url- the target URLcharset- the charset to userefererUrl- the url be used by the referer header
-
WebRequest
Instantiates aWebRequestfor the specified URL.- Parameters:
url- the target URL
-
WebRequest
Instantiates aWebRequestfor the specified URL using the given HTTP submit method.- Parameters:
url- the target URLsubmitMethod- the HTTP submit method to use
-
-
Method Details
-
newAboutBlankRequest
Returns a new request for about:blank.- Returns:
- a new request for about:blank
-
getUrl
Returns the target URL.- Returns:
- the target URL
-
setUrl
Sets the target URL. The URL may be simplified if needed (for instance eliminating irrelevant path portions like "/./").- Parameters:
url- the target URL
-
getProxyHost
Returns the proxy host to use.- Returns:
- the proxy host to use
-
setProxyHost
Sets the proxy host to use.- Parameters:
proxyHost- the proxy host to use
-
getProxyPort
public int getProxyPort()Returns the proxy port to use.- Returns:
- the proxy port to use
-
setProxyPort
public void setProxyPort(int proxyPort) Sets the proxy port to use.- Parameters:
proxyPort- the proxy port to use
-
getProxyScheme
Returns the proxy scheme to use.- Returns:
- the proxy scheme to use
-
setProxyScheme
Sets the proxy scheme to use.- Parameters:
proxyScheme- the proxy scheme to use
-
isSocksProxy
public boolean isSocksProxy()Returns whether SOCKS proxy or not. Returns whether SOCKS proxy or not.- Returns:
- whether SOCKS proxy or not
-
setSocksProxy
public void setSocksProxy(boolean isSocksProxy) Sets whether SOCKS proxy or not.- Parameters:
isSocksProxy- whether SOCKS proxy or not
-
getTimeout
public int getTimeout()Returns the timeout to use.- Returns:
- the timeout to use
-
setTimeout
public void setTimeout(int timeout) Sets the timeout to use.- Parameters:
timeout- the timeout to use
-
getEncodingType
Returns the form encoding type to use. Returns the form encoding type to use.- Returns:
- the form encoding type to use
-
setEncodingType
Sets the form encoding type to use.- Parameters:
encodingType- the form encoding type to use
-
getParameters
Retrieves the request parameters used. Similar to the servlet api function getParameterMap() this works depending on the request type and collects the url parameters and the body stuff.
The value is also normalized - null is converted to an empty string.In contrast to the servlet api this creates a separate KeyValuePair for every parameter. This means that pairs with the same name can be part of the list. The servlet api will return a string[] as value for the key in this case.
Additionally this method includes also the uploaded files for multipart post requests.- Returns:
- the request parameters to use
-
getRequestParameters
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Returns the request parameters to use. If set, these request parameters will overwrite any request parameters which may be present in theURL. Should not be used in combination with therequest body.- Returns:
- the request parameters to use
-
setRequestParameters
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Sets the request parameters to use. If set, these request parameters will overwrite any request parameters which may be present in theURL. Should not be used in combination with therequest body.- Parameters:
requestParameters- the request parameters to use- Throws:
RuntimeException- if the request body has already been set
-
getRequestBody
Returns the body content to be submitted if this is aPOSTrequest. Ignored for all other request types. Should not be used in combination withrequest parameters.- Returns:
- the body content, or
nullif not set
-
setRequestBody
Sets the body content to be submitted if this is aPOST,PUTorPATCHrequest. Other request types result inIllegalStateException. Should not be used in combination withrequest parameters.- Parameters:
requestBody- the body content to be submitted if this is aPOST,PUTorPATCHrequest- Throws:
IllegalStateException- if the request parameters have already been set or this is not aPOST,PUTorPATCHrequest.
-
getHttpMethod
Returns the HTTP submit method to use.- Returns:
- the HTTP submit method to use
-
setHttpMethod
Sets the HTTP submit method to use.- Parameters:
submitMethod- the HTTP submit method to use
-
getAdditionalHeaders
Returns the additional HTTP headers to use.- Returns:
- the additional HTTP headers to use
-
setAdditionalHeaders
Sets the additional HTTP headers to use.- Parameters:
additionalHeaders- the additional HTTP headers to use
-
isAdditionalHeader
Returns whether the specified header name is already included in the additional HTTP headers. The comparison is case-insensitive.- Parameters:
name- the header name to look up- Returns:
trueif the header is present;falseotherwise
-
getAdditionalHeader
Returns the value of the specified additional HTTP header, ornullif the header is not set. The name comparison is case-insensitive.- Parameters:
name- the header name to look up- Returns:
- the header value, or
nullif not present
-
setRefererHeader
Sets theRefererHTTP header to the external form of the given URL. Does nothing if the URL isnullor does not use thehttporhttpsscheme.- Parameters:
url- the URL to use as the referer, ornull
-
getFetchDestination
Returns the destination of this request, used to compute theSec-Fetch-Destheader (and, unless overridden, the defaultSec-Fetch-Mode). Defaults toWebRequest.FetchDestination.EMPTY, which is correct for plainXMLHttpRequest/fetch()calls.- Returns:
- the destination of this request
-
setFetchDestination
Sets the destination of this request.- Parameters:
fetchDestination- the destination of this request, ornullto reset toWebRequest.FetchDestination.EMPTY
-
getFetchModeOverride
Returns the explicit mode override for this request, if any. Whennull(the default), the mode is derived from thedestination.- Returns:
- the mode override, or
nullif none was set
-
setFetchModeOverride
Sets an explicit mode override for this request, e.g. for afetch()call using an explicitmodeoption, or a subresource request using thecrossoriginattribute (which forces CORS mode).- Parameters:
fetchMode- the mode to use, ornullto derive it from thedestination
-
isUserActivation
public boolean isUserActivation()Returns whether this request is the result of a navigation backed by genuine user activation (e.g. a click on a link, a typed URL, or a form submitted via a click on its submit button) as opposed to one triggered purely by script (e.g.location.href = ..., a<meta http-equiv="refresh">, or an automatically-loaded<iframe>).Only relevant for requests whose
Sec-Fetch-Modeisnavigate; used to compute the presence of theSec-Fetch-Userheader, which real browsers omit entirely (never send as?0) whenever this isfalse.- Returns:
- whether this request was triggered by a real user gesture
-
setUserActivation
public void setUserActivation(boolean userActivation) Sets whether this request is the result of a navigation backed by genuine user activation.- Parameters:
userActivation- whether this request was triggered by a real user gesture
-
getRequestingUrl
Returns the URL of the document or script that initiated this request, used to compute theSec-Fetch-Siteheader.nullmeans there is no initiator (e.g. a browser-chrome-initiated navigation such as a typed URL or bookmark), which maps toSec-Fetch-Site: none.Note this is tracked separately from the
Refererheader: unlike the referrer, it must not be affected by referrer-policy stripping, sinceSec-Fetch-Sitealways reflects the true relationship between the initiator and the target, even when noRefererheader is sent.- Returns:
- the URL of the initiator, or
nullif there is none
-
setRequestingUrl
Sets the URL of the document or script that initiated this request.- Parameters:
requestingUrl- the URL of the initiator, ornullif there is none
-
setAdditionalHeader
Sets the specified name/value pair in the additional HTTP headers, replacing any existing header with the same name (case-insensitive).- Parameters:
name- the header namevalue- the header value
-
removeAdditionalHeader
Removes the specified header from the additional HTTP headers. The name comparison is case-insensitive. Does nothing if the header is not present.- Parameters:
name- the header name to remove
-
getUrlCredentials
public org.apache.http.auth.Credentials getUrlCredentials()Returns the credentials extracted from the URL's userinfo component (e.g.http://user:secret@host/), ornullif none were present.- Returns:
- the URL-embedded credentials, or
null
-
getCredentials
public org.apache.http.auth.Credentials getCredentials()Returns the credentials explicitly set viasetCredentials(Credentials), ornullif none were set.- Returns:
- the explicitly configured credentials, or
null
-
setCredentials
public void setCredentials(org.apache.http.auth.Credentials credentials) Sets the credentials to use.- Parameters:
credentials- the credentials to use
-
getCharset
Returns the character set to use to perform the request.- Returns:
- the character set to use to perform the request
-
setCharset
Sets the character set to use to perform the request. The default value isStandardCharsets.ISO_8859_1.- Parameters:
charset- the character set to use to perform the request
-
getDefaultResponseContentCharset
Returns the default character set to use for the response when it does not specify one.- Returns:
- the default character set to use for the response when it does not specify one.
-
setDefaultResponseContentCharset
Sets the default character set to use when the response does not declare one explicitly.Unless overridden, the default is
StandardCharsets.ISO_8859_1(per HTTP/1.1).- Parameters:
defaultResponseContentCharset- the fallback charset; must not benull
-
hasHint
Returns whether the givenWebRequest.HttpHintis currently enabled for this request.- Parameters:
hint- the hint to check- Returns:
trueif the hint is enabled;falseotherwise
-
addHint
Enables the givenWebRequest.HttpHintfor this request.- Parameters:
hint- the hint to enable
-
toString
-