Module bus.http

Class Httpv

java.lang.Object
org.miaixz.bus.http.Httpv

public class Httpv extends Object
A high-level HTTP client that provides a fluent and expressive API for making HTTP requests. It is built on top of Httpd and adds features like pre-processing, task management, and simplified request creation.
Since:
Java 17+
Author:
Kimi Liu
  • Constructor Details

    • Httpv

      public Httpv()
      Default constructor.
    • Httpv

      public Httpv(Httpv.Builder builder)
      Constructs an Httpv instance with the configuration from the given builder.
      Parameters:
      builder - The builder to configure this instance.
  • Method Details

    • builder

      public static Httpv.Builder builder()
      Creates a new builder for constructing an Httpv instance.
      Returns:
      a new Httpv.Builder.
    • async

      public CoverHttp.Async async(String url)
      Starts an asynchronous HTTP request.
      Parameters:
      url - The URL for the request. Can be a relative or absolute path.
      Returns:
      An CoverHttp.Async instance for building and executing the request.
    • sync

      public CoverHttp.Sync sync(String url)
      Starts a synchronous HTTP request.
      Parameters:
      url - The URL for the request. Can be a relative or absolute path.
      Returns:
      A CoverHttp.Sync instance for building and executing the request.
    • webSocket

      public CoverCall.Client webSocket(String url)
      Starts a WebSocket connection.
      Parameters:
      url - The URL for the WebSocket. Can be a relative or absolute path.
      Returns:
      A CoverCall.Client instance for establishing the WebSocket connection.
    • cancel

      public int cancel(String tag)
      Cancels all ongoing requests that are associated with the given tag.
      Parameters:
      tag - The tag to identify which requests to cancel.
      Returns:
      The number of requests that were successfully canceled.
    • cancelAll

      public void cancelAll()
      Cancels all ongoing requests and clears all tagged tasks.
    • request

      public NewCall request(Request request)
      Creates a new call for the given request using the underlying Httpd client.
      Parameters:
      request - The request to execute.
      Returns:
      A new NewCall.
    • webSocket

      public WebSocket webSocket(Request request, WebSocketListener listener)
      Creates a new WebSocket connection using the underlying Httpd client.
      Parameters:
      request - The WebSocket request.
      listener - The listener for WebSocket events.
      Returns:
      A new WebSocket.
    • httpd

      public Httpd httpd()
      Gets the underlying Httpd client.
      Returns:
      The Httpd instance.
    • preprocTimeoutMillis

      public int preprocTimeoutMillis()
      Calculates the timeout for pre-processing tasks in milliseconds.
      Returns:
      The pre-processing timeout in milliseconds.
    • getTagTaskCount

      public int getTagTaskCount()
      Gets the current number of tagged tasks.
      Returns:
      The number of tagged tasks.
    • addTagTask

      public Httpv.TagTask addTagTask(String tag, Cancelable canceler, CoverHttp<?> task)
      Adds a new tagged task to the list of ongoing tasks.
      Parameters:
      tag - The tag for the task.
      canceler - The object that can cancel the task.
      task - The task itself.
      Returns:
      The newly created Httpv.TagTask.
    • removeTagTask

      public void removeTagTask(CoverHttp<?> task)
      Removes a tagged task from the list of ongoing tasks.
      Parameters:
      task - The task to remove.
    • contentType

      public org.miaixz.bus.core.lang.MediaType contentType(String type)
      Gets the MediaType for a given type string based on the configured media types.
      Parameters:
      type - The type string (e.g., "png", "json").
      Returns:
      The corresponding MediaType, or application/octet-stream if not found.
    • executor

      public CoverTasks.Executor executor()
      Gets the executor for handling asynchronous tasks.
      Returns:
      The CoverTasks.Executor.
    • preprocess

      public void preprocess(CoverHttp<? extends CoverHttp<?>> coverHttp, Runnable request, boolean skipPreproc, boolean skipSerialPreproc)
      Executes the pre-processing chain for a given request.
      Parameters:
      coverHttp - The HTTP task being processed.
      request - The final request action to run after pre-processing.
      skipPreproc - Whether to skip all pre-processors.
      skipSerialPreproc - Whether to skip serial pre-processors.
    • newBuilder

      public Httpv.Builder newBuilder()
      Creates a new builder initialized with the configuration of this Httpv instance.
      Returns:
      A new Httpv.Builder.
    • baseUrl

      public String baseUrl()
      Gets the base URL.
      Returns:
      The base URL.
    • mediaTypes

      public Map<String,String> mediaTypes()
      Gets the map of media types.
      Returns:
      The map of media types.
    • preprocessors

      public Preprocessor[] preprocessors()
      Gets the array of preprocessors.
      Returns:
      The array of preprocessors.
    • tagTasks

      public List<Httpv.TagTask> tagTasks()
      Gets the list of tagged tasks.
      Returns:
      The list of tagged tasks.
    • preprocTimeoutTimes

      public int preprocTimeoutTimes()
      Gets the pre-processing timeout multiplier.
      Returns:
      The pre-processing timeout multiplier.
    • charset

      public Charset charset()
      Gets the default charset.
      Returns:
      The default charset.
    • bodyType

      public String bodyType()
      Gets the default body type.
      Returns:
      The default body type.