Package org.htmlunit
Class WebRequest
- java.lang.Object
-
- org.htmlunit.WebRequest
-
- All Implemented Interfaces:
java.io.Serializable
public class WebRequest extends java.lang.Object implements java.io.SerializableParameter object for making web requests.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classWebRequest.HttpHint
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<java.lang.String,java.lang.String>additionalHeaders_private java.nio.charset.Charsetcharset_private org.apache.http.auth.Credentialscredentials_private java.nio.charset.CharsetdefaultResponseContentCharset_private static java.util.regex.PatternDOT_DOT_PATTERNprivate static java.util.regex.PatternDOT_PATTERNprivate FormEncodingTypeencodingType_private java.util.Set<WebRequest.HttpHint>httpHints_private HttpMethodhttpMethod_private booleanisSocksProxy_private java.lang.StringproxyHost_private intproxyPort_private java.lang.StringproxyScheme_private static java.util.regex.PatternREMOVE_DOTS_PATTERNprivate java.lang.StringrequestBody_private java.util.List<NameValuePair>requestParameters_private inttimeout_private java.lang.Stringurl_private org.apache.http.auth.CredentialsurlCredentials_
-
Constructor Summary
Constructors Constructor Description WebRequest(java.net.URL url)Instantiates aWebRequestfor the specified URL.WebRequest(java.net.URL url, java.lang.String acceptHeader, java.lang.String acceptEncodingHeader)Instantiates aWebRequestfor the specified URL.WebRequest(java.net.URL url, java.nio.charset.Charset charset, java.net.URL refererUrl)Instantiates aWebRequestfor the specified URL.WebRequest(java.net.URL url, HttpMethod submitMethod)Instantiates aWebRequestfor the specified URL using the specified HTTP submit method.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddHint(WebRequest.HttpHint hint)private static java.net.URLbuildUrlWithNewPath(java.net.URL url, java.lang.String newPath)java.lang.StringgetAdditionalHeader(java.lang.String name)Returns the header value associated with this name.java.util.Map<java.lang.String,java.lang.String>getAdditionalHeaders()Returns the additional HTTP headers to use.java.nio.charset.CharsetgetCharset()Returns the character set to use to perform the request.org.apache.http.auth.CredentialsgetCredentials()Returns the credentials to use.java.nio.charset.CharsetgetDefaultResponseContentCharset()FormEncodingTypegetEncodingType()Returns the form encoding type to use.HttpMethodgetHttpMethod()Returns the HTTP submit method to use.java.util.List<NameValuePair>getParameters()Retrieves the request parameters in use.java.lang.StringgetProxyHost()Returns the proxy host to use.intgetProxyPort()Returns the proxy port to use.java.lang.StringgetProxyScheme()Returns the proxy scheme to use.java.lang.StringgetRequestBody()Returns the body content to be submitted if this is aPOSTrequest.java.util.List<NameValuePair>getRequestParameters()INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Retrieves the request parameters to use.intgetTimeout()java.net.URLgetUrl()Returns the target URL.org.apache.http.auth.CredentialsgetUrlCredentials()Returns the credentials to use.booleanhasHint(WebRequest.HttpHint hint)booleanisAdditionalHeader(java.lang.String name)Returns whether the specified header name is already included in the additional HTTP headers.booleanisSocksProxy()Returns whether SOCKS proxy or not.static WebRequestnewAboutBlankRequest()private static java.util.List<NameValuePair>normalize(java.util.List<NameValuePair> pairs)private voidreadObject(java.io.ObjectInputStream ois)voidremoveAdditionalHeader(java.lang.String name)Removed the specified name/value pair from the additional HTTP headers.private static java.lang.StringremoveDots(java.lang.String path)voidsetAdditionalHeader(java.lang.String name, java.lang.String value)Sets the specified name/value pair in the additional HTTP headers.voidsetAdditionalHeaders(java.util.Map<java.lang.String,java.lang.String> additionalHeaders)Sets the additional HTTP headers to use.voidsetCharset(java.nio.charset.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(java.nio.charset.Charset defaultResponseContentCharset)Sets the default character set to use for the response when it does not specify one.voidsetEncodingType(FormEncodingType encodingType)Sets the form encoding type to use.voidsetHttpMethod(HttpMethod submitMethod)Sets the HTTP submit method to use.voidsetProxyHost(java.lang.String proxyHost)Sets the proxy host to use.voidsetProxyPort(int proxyPort)Sets the proxy port to use.voidsetProxyScheme(java.lang.String proxyScheme)Sets the proxy scheme to use.voidsetRefererlHeader(java.net.URL url)Sets the referer HTTP header - only if the provided url is valid.voidsetRequestBody(java.lang.String requestBody)Sets the body content to be submitted if this is aPOST,PUTorPATCHrequest.voidsetRequestParameters(java.util.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.voidsetUrl(java.net.URL url)Sets the target URL.java.lang.StringtoString()Returns a string representation of this object.private voidwriteObject(java.io.ObjectOutputStream oos)
-
-
-
Field Detail
-
DOT_PATTERN
private static final java.util.regex.Pattern DOT_PATTERN
-
DOT_DOT_PATTERN
private static final java.util.regex.Pattern DOT_DOT_PATTERN
-
REMOVE_DOTS_PATTERN
private static final java.util.regex.Pattern REMOVE_DOTS_PATTERN
-
url_
private java.lang.String url_
-
proxyHost_
private java.lang.String proxyHost_
-
proxyPort_
private int proxyPort_
-
proxyScheme_
private java.lang.String proxyScheme_
-
isSocksProxy_
private boolean isSocksProxy_
-
httpMethod_
private HttpMethod httpMethod_
-
encodingType_
private FormEncodingType encodingType_
-
additionalHeaders_
private java.util.Map<java.lang.String,java.lang.String> additionalHeaders_
-
urlCredentials_
private org.apache.http.auth.Credentials urlCredentials_
-
credentials_
private org.apache.http.auth.Credentials credentials_
-
timeout_
private int timeout_
-
httpHints_
private transient java.util.Set<WebRequest.HttpHint> httpHints_
-
charset_
private transient java.nio.charset.Charset charset_
-
defaultResponseContentCharset_
private transient java.nio.charset.Charset defaultResponseContentCharset_
-
requestParameters_
private java.util.List<NameValuePair> requestParameters_
-
requestBody_
private java.lang.String requestBody_
-
-
Constructor Detail
-
WebRequest
public WebRequest(java.net.URL url, java.lang.String acceptHeader, java.lang.String acceptEncodingHeader)Instantiates aWebRequestfor the specified URL.- Parameters:
url- the target URLacceptHeader- the accept header to useacceptEncodingHeader- the accept encoding header to use
-
WebRequest
public WebRequest(java.net.URL url, java.nio.charset.Charset charset, java.net.URL refererUrl)Instantiates aWebRequestfor the specified URL.- Parameters:
url- the target URLcharset- the charset to userefererUrl- the url be used by the referer header
-
WebRequest
public WebRequest(java.net.URL url)
Instantiates aWebRequestfor the specified URL.- Parameters:
url- the target URL
-
WebRequest
public WebRequest(java.net.URL url, HttpMethod submitMethod)Instantiates aWebRequestfor the specified URL using the specified HTTP submit method.- Parameters:
url- the target URLsubmitMethod- the HTTP submit method to use
-
-
Method Detail
-
newAboutBlankRequest
public static WebRequest newAboutBlankRequest()
- Returns:
- a new request for about:blank
-
getUrl
public java.net.URL getUrl()
Returns the target URL.- Returns:
- the target URL
-
setUrl
public void setUrl(java.net.URL url)
Sets the target URL. The URL may be simplified if needed (for instance eliminating irrelevant path portions like "/./").- Parameters:
url- the target URL
-
removeDots
private static java.lang.String removeDots(java.lang.String path)
-
buildUrlWithNewPath
private static java.net.URL buildUrlWithNewPath(java.net.URL url, java.lang.String newPath)
-
getProxyHost
public java.lang.String getProxyHost()
Returns the proxy host to use.- Returns:
- the proxy host to use
-
setProxyHost
public void setProxyHost(java.lang.String proxyHost)
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
public java.lang.String getProxyScheme()
Returns the proxy scheme to use.- Returns:
- the proxy scheme to use
-
setProxyScheme
public void setProxyScheme(java.lang.String proxyScheme)
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
-
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
-
setTimeout
public void setTimeout(int timeout)
Sets the timeout to use.- Parameters:
timeout- the timeout to use
-
getEncodingType
public FormEncodingType getEncodingType()
Returns the form encoding type to use.- Returns:
- the form encoding type to use
-
setEncodingType
public void setEncodingType(FormEncodingType encodingType)
Sets the form encoding type to use.- Parameters:
encodingType- the form encoding type to use
-
getParameters
public java.util.List<NameValuePair> getParameters()
Retrieves the request parameters in use. Similar to the servlet api this will work depending on the request type and check the url parameters and the body. The value is also normalized - null is converted to an empty string.- Returns:
- the request parameters to use
-
normalize
private static java.util.List<NameValuePair> normalize(java.util.List<NameValuePair> pairs)
-
getRequestParameters
public java.util.List<NameValuePair> getRequestParameters()
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Retrieves 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
public void setRequestParameters(java.util.List<NameValuePair> requestParameters) throws java.lang.RuntimeException
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:
java.lang.RuntimeException- if the request body has already been set
-
getRequestBody
public java.lang.String 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 to be submitted if this is a
POSTrequest
-
setRequestBody
public void setRequestBody(java.lang.String requestBody) throws java.lang.RuntimeExceptionSets the body content to be submitted if this is aPOST,PUTorPATCHrequest. Other request types result inRuntimeException. Should not be used in combination withrequest parameters.- Parameters:
requestBody- the body content to be submitted if this is aPOST,PUTorPATCHrequest- Throws:
java.lang.RuntimeException- if the request parameters have already been set or this is not aPOST,PUTorPATCHrequest.
-
getHttpMethod
public HttpMethod getHttpMethod()
Returns the HTTP submit method to use.- Returns:
- the HTTP submit method to use
-
setHttpMethod
public void setHttpMethod(HttpMethod submitMethod)
Sets the HTTP submit method to use.- Parameters:
submitMethod- the HTTP submit method to use
-
getAdditionalHeaders
public java.util.Map<java.lang.String,java.lang.String> getAdditionalHeaders()
Returns the additional HTTP headers to use.- Returns:
- the additional HTTP headers to use
-
setAdditionalHeaders
public void setAdditionalHeaders(java.util.Map<java.lang.String,java.lang.String> additionalHeaders)
Sets the additional HTTP headers to use.- Parameters:
additionalHeaders- the additional HTTP headers to use
-
isAdditionalHeader
public boolean isAdditionalHeader(java.lang.String name)
Returns whether the specified header name is already included in the additional HTTP headers.- Parameters:
name- the name of the additional HTTP header- Returns:
- true if the specified header name is included in the additional HTTP headers
-
getAdditionalHeader
public java.lang.String getAdditionalHeader(java.lang.String name)
Returns the header value associated with this name.- Parameters:
name- the name of the additional HTTP header- Returns:
- the value or null
-
setRefererlHeader
public void setRefererlHeader(java.net.URL url)
Sets the referer HTTP header - only if the provided url is valid.- Parameters:
url- the url for the referer HTTP header
-
setAdditionalHeader
public void setAdditionalHeader(java.lang.String name, java.lang.String value)Sets the specified name/value pair in the additional HTTP headers.- Parameters:
name- the name of the additional HTTP headervalue- the value of the additional HTTP header
-
removeAdditionalHeader
public void removeAdditionalHeader(java.lang.String name)
Removed the specified name/value pair from the additional HTTP headers.- Parameters:
name- the name of the additional HTTP header
-
getUrlCredentials
public org.apache.http.auth.Credentials getUrlCredentials()
Returns the credentials to use.- Returns:
- the credentials if set as part of the url
-
getCredentials
public org.apache.http.auth.Credentials getCredentials()
Returns the credentials to use.- Returns:
- the credentials if set from the external builder
-
setCredentials
public void setCredentials(org.apache.http.auth.Credentials credentials)
Sets the credentials to use.- Parameters:
credentials- the credentials to use
-
getCharset
public java.nio.charset.Charset getCharset()
Returns the character set to use to perform the request.- Returns:
- the character set to use to perform the request
-
setCharset
public void setCharset(java.nio.charset.Charset charset)
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
public java.nio.charset.Charset getDefaultResponseContentCharset()
- Returns:
- the default character set to use for the response when it does not specify one.
-
setDefaultResponseContentCharset
public void setDefaultResponseContentCharset(java.nio.charset.Charset defaultResponseContentCharset)
Sets the default character set to use for the response when it does not specify one.Unless set, the default is
StandardCharsets.UTF_8.- Parameters:
defaultResponseContentCharset- the default character set of the response
-
hasHint
public boolean hasHint(WebRequest.HttpHint hint)
-
addHint
public void addHint(WebRequest.HttpHint hint)
-
toString
public java.lang.String toString()
Returns a string representation of this object.- Overrides:
toStringin classjava.lang.Object- Returns:
- a string representation of this object
-
writeObject
private void writeObject(java.io.ObjectOutputStream oos) throws java.io.IOException- Throws:
java.io.IOException
-
readObject
private void readObject(java.io.ObjectInputStream ois) throws java.lang.ClassNotFoundException, java.io.IOException- Throws:
java.lang.ClassNotFoundExceptionjava.io.IOException
-
-