Class RequestBody

java.lang.Object
org.miaixz.bus.http.bodys.RequestBody
Direct Known Subclasses:
FormBody, MultipartBody, ProgressBody

public abstract class RequestBody extends Object
内容对象
Since:
Java 17+
Author:
Kimi Liu
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    create(org.miaixz.bus.core.lang.MediaType mediaType, byte[] content)
    发送content的新请求体
    create(org.miaixz.bus.core.lang.MediaType mediaType, byte[] content, int offset, int byteCount)
    发送content的新请求体
    create(org.miaixz.bus.core.lang.MediaType mediaType, File file)
    新的请求体,该请求体传输file的内容
    create(org.miaixz.bus.core.lang.MediaType mediaType, String content)
    返回传输content的新请求体。
    create(org.miaixz.bus.core.lang.MediaType mediaType, org.miaixz.bus.core.io.ByteString content)
    返回发送content的新请求体
    boolean
    A duplex request body is special in how it is transmitted on the network and in the API contract between Http and the application.
    boolean
    Returns true if this body expects at most one call to writeTo(org.miaixz.bus.core.io.sink.BufferSink) and can be transmitted at most once.
    long
    返回调用writeTo(org.miaixz.bus.core.io.sink.BufferSink)时写入sink的字节数,如果该计数未知,则返回-1
    abstract org.miaixz.bus.core.lang.MediaType
     
    abstract void
    writeTo(org.miaixz.bus.core.io.sink.BufferSink sink)
    将此请求的内容写入sink

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • RequestBody

      public RequestBody()
  • Method Details

    • create

      public static RequestBody create(org.miaixz.bus.core.lang.MediaType mediaType, String content)
      返回传输content的新请求体。 如果mediaType是非空且缺少字符集,则使用UTF-8
      Parameters:
      mediaType - 请求类型
      content - 内容
      Returns:
      传输请求体
    • create

      public static RequestBody create(org.miaixz.bus.core.lang.MediaType mediaType, org.miaixz.bus.core.io.ByteString content)
      返回发送content的新请求体
      Parameters:
      mediaType - 请求类型
      content - 内容
      Returns:
      传输请求体
    • create

      public static RequestBody create(org.miaixz.bus.core.lang.MediaType mediaType, byte[] content)
      发送content的新请求体
      Parameters:
      mediaType - 媒体类型
      content - 内容
      Returns:
      传输请求体
    • create

      public static RequestBody create(org.miaixz.bus.core.lang.MediaType mediaType, byte[] content, int offset, int byteCount)
      发送content的新请求体
      Parameters:
      mediaType - 媒体类型
      content - 内容
      offset - 偏移量
      byteCount - 当前大小
      Returns:
      传输请求体
    • create

      public static RequestBody create(org.miaixz.bus.core.lang.MediaType mediaType, File file)
      新的请求体,该请求体传输file的内容
      Parameters:
      mediaType - 请求类型
      file - 文件
      Returns:
      传输请求体
    • mediaType

      public abstract org.miaixz.bus.core.lang.MediaType mediaType()
      Returns:
      返回此主体的媒体类型
    • length

      public long length() throws IOException
      返回调用writeTo(org.miaixz.bus.core.io.sink.BufferSink)时写入sink的字节数,如果该计数未知,则返回-1
      Returns:
      计数信息
      Throws:
      IOException - 异常
    • writeTo

      public abstract void writeTo(org.miaixz.bus.core.io.sink.BufferSink sink) throws IOException
      将此请求的内容写入sink
      Parameters:
      sink - 缓存区
      Throws:
      IOException - 异常信息
    • isDuplex

      public boolean isDuplex()
      A duplex request body is special in how it is transmitted on the network and in the API contract between Http and the application. This method returns false unless it is overridden by a subclass. Duplex Transmission With regular HTTP calls the request always completes sending before the response may begin receiving. With duplex the request and response may be interleaved! That is, request body bytes may be sent after response headers or body bytes have been received. Though any call may be initiated as a duplex call, only web servers that are specially designed for this nonstandard interaction will use it. As of 2019-01, the only widely-used implementation of this pattern is gRPC. Because the encoding of interleaved data is not well-defined for HTTP/1, duplex request bodies may only be used with HTTP/2. Calls to HTTP/1 servers will fail before the HTTP request is transmitted. If you cannot ensure that your client and server both support HTTP/2, do not use this feature. With regular request bodies it is not legal to write bytes to the sink passed to writeTo(org.miaixz.bus.core.io.sink.BufferSink) after that method returns. For duplex requests bodies that condition is lifted. Such writes occur on an application-provided thread and may occur concurrently with reads of the ResponseBody. For duplex request bodies, writeTo(org.miaixz.bus.core.io.sink.BufferSink) should return quickly, possibly by handing off the provided request body to another thread to perform writing.
    • isOneShot

      public boolean isOneShot()
      Returns true if this body expects at most one call to writeTo(org.miaixz.bus.core.io.sink.BufferSink) and can be transmitted at most once. This is typically used when writing the request body is destructive and it is not possible to recreate the request body after it has been sent. This method returns false unless it is overridden by a subclass. By default Http will attempt to retransmit request bodies when the original request fails due to a stale connection, a client timeout (HTTP 408), a satisfied authorization challenge (HTTP 401 and 407), or a retryable server failure (HTTP 503 with a Retry-After: 0 header).