CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-net-sourceforge-htmlunit--htmlunit

A headless browser intended for use in testing web-based applications.

Pending
Overview
Eval results
Files

http.mddocs/

HTTP Communication

HTTP request/response handling with full control over headers, methods, authentication, proxy settings, and connection configuration. Essential for API testing, advanced web scraping, and custom HTTP workflows.

Capabilities

WebRequest Class

Configuration for HTTP requests with custom headers, methods, and parameters.

/**
 * HTTP request configuration and parameters
 */
public class WebRequest {
    /** Create GET request for URL */
    public WebRequest(URL url);
    
    /** Create request with specific HTTP method */
    public WebRequest(URL url, HttpMethod submitMethod);
    
    /** Create request with accept headers */
    public WebRequest(URL url, String acceptHeader, String acceptEncodingHeader);
    
    /** Get request URL */
    public URL getUrl();
    
    /** Set request URL */
    public void setUrl(URL url);
    
    /** Get HTTP method */
    public HttpMethod getHttpMethod();
    
    /** Set HTTP method */
    public void setHttpMethod(HttpMethod method);
    
    /** Get request body content */
    public String getRequestBody();
    
    /** Set request body content */
    public void setRequestBody(String requestBody);
    
    /** Set request body with encoding */
    public void setRequestBody(String requestBody, String charset);
    
    /** Get request parameters */
    public List<NameValuePair> getParameters();
    
    /** Set request parameters */
    public void setParameters(List<NameValuePair> parameters);
    
    /** Add request parameter */
    public void setRequestParameter(String name, String value);
    
    /** Get additional headers */
    public Map<String, String> getAdditionalHeaders();
    
    /** Set additional header */
    public void setAdditionalHeader(String name, String value);
    
    /** Remove header */
    public void removeAdditionalHeader(String name);
    
    /** Get form encoding type */
    public FormEncodingType getEncodingType();
    
    /** Set form encoding type */
    public void setEncodingType(FormEncodingType encodingType);
    
    /** Get authentication credentials */
    public Credentials getCredentials();
    
    /** Set authentication credentials */
    public void setCredentials(Credentials credentials);
    
    /** Get proxy host */
    public String getProxyHost();
    
    /** Set proxy host */
    public void setProxyHost(String proxyHost);
    
    /** Get proxy port */
    public int getProxyPort();
    
    /** Set proxy port */
    public void setProxyPort(int proxyPort);
    
    /** Get proxy credentials */
    public Credentials getProxyCredentials();
    
    /** Set proxy credentials */
    public void setProxyCredentials(Credentials credentials);
    
    /** Get connection timeout */
    public int getTimeout();
    
    /** Set connection timeout in milliseconds */
    public void setTimeout(int timeout);
    
    /** Get character encoding */
    public Charset getCharset();
    
    /** Set character encoding */
    public void setCharset(Charset charset);
    
    /** Check if request follows redirects */
    public boolean isFollowRedirects();
    
    /** Set whether to follow redirects */
    public void setFollowRedirects(boolean followRedirects);
    
    /** Get maximum redirect count */
    public int getMaxRedirectCount();
    
    /** Set maximum redirect count */
    public void setMaxRedirectCount(int maxRedirectCount);
}

Usage Examples:

import com.gargoylesoftware.htmlunit.*;
import com.gargoylesoftware.htmlunit.util.NameValuePair;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

try (WebClient webClient = new WebClient()) {
    // Simple GET request
    WebRequest getRequest = new WebRequest(new URL("https://api.example.com/data"));
    getRequest.setAdditionalHeader("Accept", "application/json");
    getRequest.setAdditionalHeader("Authorization", "Bearer token123");
    
    Page response = webClient.getPage(getRequest);
    
    // POST request with JSON data
    WebRequest postRequest = new WebRequest(new URL("https://api.example.com/users"), HttpMethod.POST);
    postRequest.setAdditionalHeader("Content-Type", "application/json");
    postRequest.setRequestBody("{\"name\":\"John\",\"email\":\"john@example.com\"}");
    
    Page postResponse = webClient.getPage(postRequest);
    
    // Form data POST request
    WebRequest formRequest = new WebRequest(new URL("https://example.com/submit"), HttpMethod.POST);
    List<NameValuePair> parameters = new ArrayList<>();
    parameters.add(new NameValuePair("username", "john"));
    parameters.add(new NameValuePair("password", "secret"));
    formRequest.setParameters(parameters);
    formRequest.setEncodingType(FormEncodingType.URL_ENCODED);
    
    Page formResponse = webClient.getPage(formRequest);
}

WebResponse Class

HTTP response data and metadata access.

/**
 * HTTP response with content and metadata
 */
public class WebResponse {
    /** Get HTTP status code */
    public int getStatusCode();
    
    /** Get HTTP status message */
    public String getStatusMessage();
    
    /** Get response content as string */
    public String getContentAsString();
    
    /** Get response content with specific encoding */
    public String getContentAsString(Charset charset);
    
    /** Get response content as input stream */
    public InputStream getContentAsStream() throws IOException;
    
    /** Get response content as byte array */
    public byte[] getContentAsBytes();
    
    /** Get all response headers */
    public List<NameValuePair> getResponseHeaders();
    
    /** Get specific response header value */
    public String getResponseHeaderValue(String headerName);
    
    /** Get all values for header (for multi-value headers) */
    public List<String> getResponseHeaderValues(String headerName);
    
    /** Get response content type */
    public String getContentType();
    
    /** Get response character encoding */
    public Charset getContentCharset();
    
    /** Get response content length */
    public long getContentLength();
    
    /** Get response load time in milliseconds */
    public long getLoadTime();
    
    /** Get original request that generated response */
    public WebRequest getWebRequest();
    
    /** Get response data wrapper */
    public WebResponseData getResponseData();
    
    /** Get response URL (may differ from request due to redirects) */
    public URL getWebRequest().getUrl();
}

Usage Examples:

// Examine response details
WebResponse response = page.getWebResponse();

System.out.println("Status: " + response.getStatusCode() + " " + response.getStatusMessage());
System.out.println("Content-Type: " + response.getContentType());
System.out.println("Content-Length: " + response.getContentLength());
System.out.println("Load Time: " + response.getLoadTime() + "ms");

// Get response headers
List<NameValuePair> headers = response.getResponseHeaders();
for (NameValuePair header : headers) {
    System.out.println(header.getName() + ": " + header.getValue());
}

// Get specific headers
String server = response.getResponseHeaderValue("Server");
String setCookie = response.getResponseHeaderValue("Set-Cookie");

// Different content access methods
String textContent = response.getContentAsString();
byte[] binaryContent = response.getContentAsBytes();
InputStream streamContent = response.getContentAsStream();

HttpMethod Enum

HTTP method constants for request configuration.

/**
 * HTTP method enumeration
 */
public enum HttpMethod {
    /** OPTIONS method for preflight requests */
    OPTIONS,
    
    /** GET method for retrieving data */
    GET,
    
    /** HEAD method for headers only */
    HEAD,
    
    /** POST method for sending data */
    POST,
    
    /** PUT method for updating resources */
    PUT,
    
    /** DELETE method for removing resources */
    DELETE,
    
    /** TRACE method for debugging */
    TRACE,
    
    /** PATCH method for partial updates */
    PATCH;
}

WebConnection Interface

Interface for HTTP connection handling and custom implementations.

/**
 * Interface for HTTP connection handling
 */
public interface WebConnection {
    /** Execute HTTP request and return response */
    WebResponse getResponse(WebRequest request) throws IOException;
    
    /** Close connection and cleanup resources */
    void close() throws IOException;
}

HttpWebConnection Class

Default HTTP connection implementation with Apache HttpClient.

/**
 * Default HTTP connection implementation using Apache HttpClient
 */
public class HttpWebConnection implements WebConnection {
    /** Create connection with default settings */
    public HttpWebConnection(WebClient webClient);
    
    /** Execute HTTP request */
    public WebResponse getResponse(WebRequest request) throws IOException;
    
    /** Get underlying HTTP client */
    public CloseableHttpClient getHttpClient();
    
    /** Set connection timeout */
    public void setTimeout(int timeout);
    
    /** Get connection timeout */
    public int getTimeout();
    
    /** Set socket timeout */
    public void setSocketTimeout(int socketTimeout);
    
    /** Get socket timeout */
    public int getSocketTimeout();
    
    /** Set connection request timeout */
    public void setConnectionRequestTimeout(int connectionRequestTimeout);
    
    /** Get connection request timeout */
    public int getConnectionRequestTimeout();
    
    /** Set maximum connections per route */
    public void setMaxConnectionsPerHost(int maxConnectionsPerHost);
    
    /** Get maximum connections per route */
    public int getMaxConnectionsPerHost();
    
    /** Set total maximum connections */
    public void setMaxTotalConnections(int maxTotalConnections);
    
    /** Get total maximum connections */
    public int getMaxTotalConnections();
    
    /** Set cookie store */
    public void setCookieStore(CookieStore cookieStore);
    
    /** Get cookie store */
    public CookieStore getCookieStore();
    
    /** Set credentials provider */
    public void setCredentialsProvider(CredentialsProvider credentialsProvider);
    
    /** Get credentials provider */
    public CredentialsProvider getCredentialsProvider();
    
    /** Close connection */
    public void close() throws IOException;
}

MockWebConnection Class

Mock HTTP connection for testing and simulation.

/**
 * Mock HTTP connection for testing and simulation
 */
public class MockWebConnection implements WebConnection {
    /** Create mock connection */
    public MockWebConnection();
    
    /** Set response for specific URL */
    public void setResponse(URL url, String content);
    
    /** Set response with status code */
    public void setResponse(URL url, String content, int statusCode, String statusMessage, String contentType, Charset charset);
    
    /** Set WebResponse for URL */
    public void setResponse(URL url, WebResponse response);
    
    /** Set default response for any unmatched URL */
    public void setDefaultResponse(String content);
    
    /** Set default response with details */
    public void setDefaultResponse(String content, int statusCode, String statusMessage, String contentType);
    
    /** Set default WebResponse */
    public void setDefaultResponse(WebResponse response);
    
    /** Get response for request */
    public WebResponse getResponse(WebRequest request) throws IOException;
    
    /** Get requested URLs in order */
    public List<URL> getRequestedUrls();
    
    /** Get last requested URL */
    public URL getLastRequestedUrl();
    
    /** Get request count for URL */
    public int getRequestCount(URL url);
    
    /** Clear all requests and responses */
    public void reset();
    
    /** Close connection */
    public void close() throws IOException;
}

Usage Examples:

// Mock HTTP responses for testing
MockWebConnection mockConnection = new MockWebConnection();

// Set up mock responses
mockConnection.setResponse(new URL("https://api.example.com/users"), 
    "[{\"id\":1,\"name\":\"John\"},{\"id\":2,\"name\":\"Jane\"}]",
    200, "OK", "application/json", StandardCharsets.UTF_8);

mockConnection.setResponse(new URL("https://api.example.com/error"), 
    "{\"error\":\"Not found\"}", 404, "Not Found", "application/json", StandardCharsets.UTF_8);

// Default response for unmatched URLs
mockConnection.setDefaultResponse("Page not found", 404, "Not Found", "text/plain");

// Use mock connection
webClient.setWebConnection(mockConnection);

// Make requests - will return mocked responses
HtmlPage page = webClient.getPage("https://api.example.com/users");
String content = page.getWebResponse().getContentAsString();

// Check what was requested
List<URL> requestedUrls = mockConnection.getRequestedUrls();
System.out.println("Requested URLs: " + requestedUrls);

Authentication Classes

/**
 * Authentication credentials
 */
public class UsernamePasswordCredentials implements Credentials {
    /** Create credentials with username and password */
    public UsernamePasswordCredentials(String userName, String password);
    
    /** Create credentials with domain */
    public UsernamePasswordCredentials(String userName, String password, String workstation, String domain);
    
    /** Get username */
    public String getUserName();
    
    /** Get password */
    public String getPassword();
    
    /** Get workstation */
    public String getWorkstation();
    
    /** Get domain */
    public String getDomain();
}

/**
 * Default credentials provider
 */
public class DefaultCredentialsProvider implements CredentialsProvider {
    /** Create empty credentials provider */
    public DefaultCredentialsProvider();
    
    /** Add credentials for host and realm */
    public void addCredentials(String userName, String password, String host, int port, String realm);
    
    /** Add credentials for host */
    public void addCredentials(String userName, String password, String host, int port);
    
    /** Add credentials for realm */
    public void addCredentials(String userName, String password, String realm);
    
    /** Add general credentials */
    public void addCredentials(String userName, String password);
    
    /** Remove credentials */
    public void removeCredentials(AuthScope authScope);
    
    /** Clear all credentials */
    public void clear();
    
    /** Get credentials for scope */
    public Credentials getCredentials(AuthScope authScope);
}

Proxy Configuration

/**
 * Proxy configuration
 */
public class ProxyConfig {
    /** Create proxy configuration */
    public ProxyConfig(String proxyHost, int proxyPort);
    
    /** Create proxy with authentication */
    public ProxyConfig(String proxyHost, int proxyPort, String username, String password);
    
    /** Create proxy with scheme */
    public ProxyConfig(String proxyHost, int proxyPort, String scheme);
    
    /** Get proxy host */
    public String getProxyHost();
    
    /** Get proxy port */
    public int getProxyPort();
    
    /** Get proxy scheme */
    public String getProxyScheme();
    
    /** Get proxy username */
    public String getProxyUsername();
    
    /** Get proxy password */
    public String getProxyPassword();
    
    /** Check if proxy auto-detect is enabled */
    public boolean isProxyAutoDetect();
    
    /** Set proxy auto-detect */
    public void setProxyAutoDetect(boolean proxyAutoDetect);
    
    /** Get SOCKS proxy settings */
    public boolean isSocksProxy();
    
    /** Set SOCKS proxy */
    public void setSocksProxy(boolean socksProxy);
}

HTTP Status Constants

/**
 * Common HTTP status codes
 */
public static final int HTTP_OK = 200;
public static final int HTTP_CREATED = 201;
public static final int HTTP_ACCEPTED = 202;
public static final int HTTP_NO_CONTENT = 204;

public static final int HTTP_MOVED_PERMANENTLY = 301;
public static final int HTTP_FOUND = 302;
public static final int HTTP_SEE_OTHER = 303;
public static final int HTTP_NOT_MODIFIED = 304;
public static final int HTTP_TEMPORARY_REDIRECT = 307;
public static final int HTTP_PERMANENT_REDIRECT = 308;

public static final int HTTP_BAD_REQUEST = 400;
public static final int HTTP_UNAUTHORIZED = 401;
public static final int HTTP_FORBIDDEN = 403;
public static final int HTTP_NOT_FOUND = 404;
public static final int HTTP_METHOD_NOT_ALLOWED = 405;
public static final int HTTP_CONFLICT = 409;
public static final int HTTP_GONE = 410;

public static final int HTTP_INTERNAL_SERVER_ERROR = 500;
public static final int HTTP_NOT_IMPLEMENTED = 501;
public static final int HTTP_BAD_GATEWAY = 502;
public static final int HTTP_SERVICE_UNAVAILABLE = 503;
public static final int HTTP_GATEWAY_TIMEOUT = 504;

SSL Configuration

/**
 * SSL configuration for HTTPS connections
 */
public class SSLContextBuilder {
    /** Create SSL context builder */
    public static SSLContextBuilder create();
    
    /** Load trust material from keystore */
    public SSLContextBuilder loadTrustMaterial(KeyStore truststore, TrustStrategy trustStrategy);
    
    /** Load key material */
    public SSLContextBuilder loadKeyMaterial(KeyStore keystore, char[] keyPassword);
    
    /** Build SSL context */
    public SSLContext build();
}

/**
 * Trust all certificates strategy (for testing only)
 */
public class TrustAllStrategy implements TrustStrategy {
    /** Accept all certificates */
    public boolean isTrusted(X509Certificate[] chain, String authType);
}

Usage Examples:

// Custom SSL configuration (accept all certificates - testing only)
webClient.getOptions().setUseInsecureSSL(true);

// Or with custom SSL context
SSLContext sslContext = SSLContextBuilder.create()
    .loadTrustMaterial(null, new TrustAllStrategy())
    .build();

// Authentication example
DefaultCredentialsProvider credentialsProvider = new DefaultCredentialsProvider();
credentialsProvider.addCredentials("username", "password", "example.com", 80, "Protected Area");
webClient.setCredentialsProvider(credentialsProvider);

// Proxy with authentication
ProxyConfig proxyConfig = new ProxyConfig("proxy.example.com", 8080, "proxyuser", "proxypass");
webClient.getOptions().setProxyConfig(proxyConfig);

Install with Tessl CLI

npx tessl i tessl/maven-net-sourceforge-htmlunit--htmlunit

docs

browser-automation.md

cookies.md

forms.md

html-dom.md

http.md

index.md

javascript.md

windows.md

tile.json