Package org.htmlunit

Class WebRequest

java.lang.Object
org.htmlunit.WebRequest
All Implemented Interfaces:
Serializable

public class WebRequest extends Object implements Serializable
Parameter object for making web requests.
See Also:
  • Field Details

    • DOT_PATTERN

      private static final Pattern DOT_PATTERN
    • DOT_DOT_PATTERN

      private static final Pattern DOT_DOT_PATTERN
    • REMOVE_DOTS_PATTERN

      private static final Pattern REMOVE_DOTS_PATTERN
    • url_

      private String url_
    • proxyHost_

      private String proxyHost_
    • proxyPort_

      private int proxyPort_
    • proxyScheme_

      private String proxyScheme_
    • isSocksProxy_

      private boolean isSocksProxy_
    • httpMethod_

      private HttpMethod httpMethod_
    • encodingType_

      private FormEncodingType encodingType_
    • additionalHeaders_

      private Map<String,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 Set<WebRequest.HttpHint> httpHints_
    • charset_

      private transient Charset charset_
    • defaultResponseContentCharset_

      private transient Charset defaultResponseContentCharset_
    • requestParameters_

      private List<NameValuePair> requestParameters_
    • requestBody_

      private String requestBody_
  • Constructor Details

    • WebRequest

      public WebRequest(URL url, String acceptHeader, String acceptEncodingHeader)
      Instantiates a WebRequest for the specified URL.
      Parameters:
      url - the target URL
      acceptHeader - the accept header to use
      acceptEncodingHeader - the accept encoding header to use
    • WebRequest

      public WebRequest(URL url, Charset charset, URL refererUrl)
      Instantiates a WebRequest for the specified URL.
      Parameters:
      url - the target URL
      charset - the charset to use
      refererUrl - the url be used by the referer header
    • WebRequest

      public WebRequest(URL url)
      Instantiates a WebRequest for the specified URL.
      Parameters:
      url - the target URL
    • WebRequest

      public WebRequest(URL url, HttpMethod submitMethod)
      Instantiates a WebRequest for the specified URL using the specified HTTP submit method.
      Parameters:
      url - the target URL
      submitMethod - the HTTP submit method to use
  • Method Details

    • newAboutBlankRequest

      public static WebRequest newAboutBlankRequest()
      Returns:
      a new request for about:blank
    • getUrl

      public URL getUrl()
      Returns the target URL.
      Returns:
      the target URL
    • setUrl

      public void setUrl(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 String removeDots(String path)
    • buildUrlWithNewPath

      private static URL buildUrlWithNewPath(URL url, String newPath)
    • getProxyHost

      public String getProxyHost()
      Returns the proxy host to use.
      Returns:
      the proxy host to use
    • setProxyHost

      public void setProxyHost(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 String getProxyScheme()
      Returns the proxy scheme to use.
      Returns:
      the proxy scheme to use
    • setProxyScheme

      public void setProxyScheme(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 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 List<NameValuePair> normalize(List<NameValuePair> pairs)
    • getRequestParameters

      public 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 the URL. Should not be used in combination with the request body.
      Returns:
      the request parameters to use
    • setRequestParameters

      public void setRequestParameters(List<NameValuePair> requestParameters) throws 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 the URL. Should not be used in combination with the request body.
      Parameters:
      requestParameters - the request parameters to use
      Throws:
      RuntimeException - if the request body has already been set
    • getRequestBody

      public String getRequestBody()
      Returns the body content to be submitted if this is a POST request. Ignored for all other request types. Should not be used in combination with request parameters.
      Returns:
      the body content to be submitted if this is a POST request
    • setRequestBody

      public void setRequestBody(String requestBody) throws RuntimeException
      Sets the body content to be submitted if this is a POST, PUT or PATCH request. Other request types result in RuntimeException. Should not be used in combination with request parameters.
      Parameters:
      requestBody - the body content to be submitted if this is a POST, PUT or PATCH request
      Throws:
      RuntimeException - if the request parameters have already been set or this is not a POST, PUT or PATCH request.
    • 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 Map<String,String> getAdditionalHeaders()
      Returns the additional HTTP headers to use.
      Returns:
      the additional HTTP headers to use
    • setAdditionalHeaders

      public void setAdditionalHeaders(Map<String,String> additionalHeaders)
      Sets the additional HTTP headers to use.
      Parameters:
      additionalHeaders - the additional HTTP headers to use
    • isAdditionalHeader

      public boolean isAdditionalHeader(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 String getAdditionalHeader(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(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(String name, String value)
      Sets the specified name/value pair in the additional HTTP headers.
      Parameters:
      name - the name of the additional HTTP header
      value - the value of the additional HTTP header
    • removeAdditionalHeader

      public void removeAdditionalHeader(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 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(Charset charset)
      Sets the character set to use to perform the request. The default value is StandardCharsets.ISO_8859_1.
      Parameters:
      charset - the character set to use to perform the request
    • getDefaultResponseContentCharset

      public Charset getDefaultResponseContentCharset()
      Returns:
      the default character set to use for the response when it does not specify one.
    • setDefaultResponseContentCharset

      public void setDefaultResponseContentCharset(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 String toString()
      Returns a string representation of this object.
      Overrides:
      toString in class Object
      Returns:
      a string representation of this object
    • writeObject

      private void writeObject(ObjectOutputStream oos) throws IOException
      Throws:
      IOException
    • readObject

      private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException
      Throws:
      ClassNotFoundException
      IOException