CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-htmlunit--htmlunit

A headless browser for Java programs that provides web automation, form handling, JavaScript execution, and DOM manipulation capabilities.

Pending
Overview
Eval results
Files

http.mddocs/

HTTP Request and Response

HTTP communication layer providing request customization, response processing, header management, and connection configuration for complete control over web interactions.

Capabilities

Web Request Creation and Customization

Create and customize HTTP requests with headers, parameters, and request bodies.

public class WebRequest {
    /**
     * Create a GET request to the specified URL
     * @param url the target URL
     */
    public WebRequest(URL url);
    
    /**
     * Create a request with specified URL and HTTP method
     * @param url the target URL
     * @param method the HTTP method to use
     */
    public WebRequest(URL url, HttpMethod method);
    
    /**
     * Get the request URL
     * @return the target URL for this request
     */
    public URL getUrl();
    
    /**
     * Get the HTTP method for this request
     * @return the HttpMethod (GET, POST, etc.)
     */
    public HttpMethod getHttpMethod();
    
    /**
     * Set the request body content
     * @param body the request body as a string
     */
    public void setRequestBody(String body);
    
    /**
     * Get the current request parameters
     * @return List of NameValuePair representing form parameters
     */
    public List<NameValuePair> getRequestParameters();
    
    /**
     * Set the request parameters (for POST forms)
     * @param parameters List of NameValuePair for form data
     */
    public void setRequestParameters(List<NameValuePair> parameters);
    
    /**
     * Get all additional headers for this request
     * @return Map of header names to values
     */
    public Map<String, String> getAdditionalHeaders();
    
    /**
     * Add or set an HTTP header
     * @param name the header name
     * @param value the header value
     */
    public void setAdditionalHeader(String name, String value);
}

Usage Examples:

// Simple GET request
URL url = new URL("https://api.example.com/data");
WebRequest request = new WebRequest(url);

// POST request with JSON body
WebRequest postRequest = new WebRequest(url, HttpMethod.POST);
postRequest.setRequestBody("{\"name\":\"John\",\"age\":30}");
postRequest.setAdditionalHeader("Content-Type", "application/json");
postRequest.setAdditionalHeader("Authorization", "Bearer token123");

// Form POST request
WebRequest formRequest = new WebRequest(url, HttpMethod.POST);
List<NameValuePair> parameters = Arrays.asList(
    new NameValuePair("username", "john"),
    new NameValuePair("password", "secret")
);
formRequest.setRequestParameters(parameters);

// Execute request
Page response = webClient.getPage(request);

Web Response Processing

Access and process HTTP responses including status codes, headers, and content.

public class WebResponse {
    /**
     * Get the HTTP status code
     * @return the status code (e.g., 200, 404, 500)
     */
    public int getStatusCode();
    
    /**
     * Get the HTTP status message
     * @return the status message (e.g., "OK", "Not Found")
     */
    public String getStatusMessage();
    
    /**
     * Get the response content as a string
     * @return the response body decoded as a string
     */
    public String getContentAsString();
    
    /**
     * Get the response content as an input stream
     * @return InputStream for reading response data
     */
    public InputStream getContentAsStream();
    
    /**
     * Get the response content type
     * @return the Content-Type header value
     */
    public String getContentType();
    
    /**
     * Get the character encoding of the response
     * @return the Charset used for text content
     */
    public Charset getContentCharset();
    
    /**
     * Get all response headers
     * @return List of NameValuePair representing all headers
     */
    public List<NameValuePair> getResponseHeaders();
    
    /**
     * Get a specific response header value
     * @param name the header name to look up
     * @return the header value, or null if not present
     */
    public String getResponseHeaderValue(String name);
    
    /**
     * Get the original request that generated this response
     * @return the WebRequest object
     */
    public WebRequest getWebRequest();
}

Usage Examples:

Page page = webClient.getPage("https://api.example.com/data");
WebResponse response = page.getWebResponse();

// Check response status
int statusCode = response.getStatusCode();
String statusMessage = response.getStatusMessage();
if (statusCode != 200) {
    System.err.println("Request failed: " + statusCode + " " + statusMessage);
}

// Access response content
String contentType = response.getContentType();
String content = response.getContentAsString();

// Process headers
String serverHeader = response.getResponseHeaderValue("Server");
List<NameValuePair> allHeaders = response.getResponseHeaders();
for (NameValuePair header : allHeaders) {
    System.out.println(header.getName() + ": " + header.getValue());
}

// Stream large responses
try (InputStream stream = response.getContentAsStream()) {
    // Process stream data
}

// Get original request details
WebRequest originalRequest = response.getWebRequest();
URL requestUrl = originalRequest.getUrl();

HTTP Methods

Enumeration of supported HTTP methods for request creation.

public enum HttpMethod {
    GET,      // Retrieve data
    POST,     // Submit data
    PUT,      // Update/replace resource  
    DELETE,   // Remove resource
    HEAD,     // Get headers only
    OPTIONS,  // Check allowed methods
    TRACE,    // Diagnostic method
    PATCH     // Partial update
}

Usage Examples:

// Different HTTP methods
WebRequest getRequest = new WebRequest(url, HttpMethod.GET);
WebRequest postRequest = new WebRequest(url, HttpMethod.POST);
WebRequest putRequest = new WebRequest(url, HttpMethod.PUT);
WebRequest deleteRequest = new WebRequest(url, HttpMethod.DELETE);

// RESTful API interactions
WebRequest apiGet = new WebRequest(new URL("https://api.example.com/users/123"), HttpMethod.GET);
WebRequest apiCreate = new WebRequest(new URL("https://api.example.com/users"), HttpMethod.POST);
WebRequest apiUpdate = new WebRequest(new URL("https://api.example.com/users/123"), HttpMethod.PUT);
WebRequest apiDelete = new WebRequest(new URL("https://api.example.com/users/123"), HttpMethod.DELETE);

Web Connection Interface

Interface for customizing HTTP connection behavior and implementing custom connection handlers.

public interface WebConnection {
    /**
     * Execute an HTTP request and return the response
     * @param request the WebRequest to execute
     * @return WebResponse containing the server response
     * @throws IOException if the request fails
     */
    WebResponse getResponse(WebRequest request) throws IOException;
}

public class HttpWebConnection implements WebConnection {
    // Standard implementation of WebConnection
    // Used by default in WebClient
}

Usage Examples:

// Use default HTTP connection
WebConnection connection = webClient.getWebConnection();

// Custom connection implementation
WebConnection customConnection = new WebConnection() {
    @Override
    public WebResponse getResponse(WebRequest request) throws IOException {
        // Log all requests
        System.out.println("Requesting: " + request.getUrl());
        
        // Delegate to default implementation
        HttpWebConnection defaultConnection = new HttpWebConnection(webClient);
        return defaultConnection.getResponse(request);
    }
};

// Set custom connection
webClient.setWebConnection(customConnection);

Name-Value Pairs

Utility class for handling HTTP parameters and headers.

public class NameValuePair {
    /**
     * Create a name-value pair
     * @param name the parameter/header name
     * @param value the parameter/header value
     */
    public NameValuePair(String name, String value);
    
    /**
     * Get the name
     * @return the name portion
     */
    public String getName();
    
    /**
     * Get the value
     * @return the value portion
     */
    public String getValue();
}

Usage Examples:

// Create form parameters
List<NameValuePair> formData = Arrays.asList(
    new NameValuePair("username", "john"),
    new NameValuePair("email", "john@example.com"),
    new NameValuePair("age", "30")
);

// Use with POST request
WebRequest postRequest = new WebRequest(url, HttpMethod.POST);
postRequest.setRequestParameters(formData);

// Process response headers
WebResponse response = webClient.getPage(postRequest).getWebResponse();
List<NameValuePair> headers = response.getResponseHeaders();
for (NameValuePair header : headers) {
    if ("Set-Cookie".equalsIgnoreCase(header.getName())) {
        System.out.println("Cookie: " + header.getValue());
    }
}

HTTP Status Handling

Exception handling for HTTP error status codes.

public class FailingHttpStatusCodeException extends RuntimeException {
    /**
     * Get the HTTP status code that caused the exception
     * @return the HTTP status code
     */
    public int getStatusCode();
    
    /**
     * Get the HTTP status message
     * @return the status message from the server
     */
    public String getStatusMessage();
    
    /**
     * Get the complete web response
     * @return the WebResponse that triggered this exception
     */
    public WebResponse getResponse();
}

Usage Examples:

try {
    // This might throw on 4xx/5xx status codes if configured to do so
    HtmlPage page = webClient.getPage("https://example.com/missing-page");
} catch (FailingHttpStatusCodeException e) {
    int statusCode = e.getStatusCode();
    String statusMessage = e.getStatusMessage();
    WebResponse response = e.getResponse();
    
    System.err.println("HTTP Error: " + statusCode + " " + statusMessage);
    System.err.println("Response content: " + response.getContentAsString());
}

// Configure whether to throw exceptions on HTTP errors
webClient.getOptions().setThrowExceptionOnFailingStatusCode(true); // Default is true

Advanced Request Customization

Advanced techniques for customizing HTTP requests.

Usage Examples:

// Complex API request with authentication and custom headers
WebRequest apiRequest = new WebRequest(new URL("https://api.example.com/secure-data"), HttpMethod.POST);

// Add authentication
apiRequest.setAdditionalHeader("Authorization", "Bearer " + authToken);

// Set content type and accept headers
apiRequest.setAdditionalHeader("Content-Type", "application/json");
apiRequest.setAdditionalHeader("Accept", "application/json");

// Add custom headers for API versioning
apiRequest.setAdditionalHeader("API-Version", "2.1");
apiRequest.setAdditionalHeader("User-Agent", "MyApp/1.0 (Java)");

// Set JSON request body
String jsonBody = "{\"query\":\"search term\",\"limit\":50,\"offset\":0}";
apiRequest.setRequestBody(jsonBody);

// Execute and process JSON response
Page responsePage = webClient.getPage(apiRequest);
WebResponse response = responsePage.getWebResponse();

if (response.getStatusCode() == 200) {
    String jsonResponse = response.getContentAsString();
    // Parse JSON response as needed
}

Install with Tessl CLI

npx tessl i tessl/maven-org-htmlunit--htmlunit

docs

cookies.md

exceptions.md

forms.md

http.md

index.md

javascript.md

page-dom.md

web-client.md

windows.md

tile.json