okhttp3.Request.body()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(8.9k)|赞(0)|评价(0)|浏览(542)

本文整理了Java中okhttp3.Request.body方法的一些代码示例,展示了Request.body的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Request.body方法的具体详情如下:
包路径:okhttp3.Request
类名称:Request
方法名:body

Request.body介绍

暂无

代码示例

代码示例来源:origin: square/okhttp

private boolean requestIsUnrepeatable(IOException e, Request userRequest) {
 return userRequest.body() instanceof UnrepeatableRequestBody
   || e instanceof FileNotFoundException;
}

代码示例来源:origin: com.squareup.okhttp3/okhttp

private boolean requestIsUnrepeatable(IOException e, Request userRequest) {
 return userRequest.body() instanceof UnrepeatableRequestBody
   || e instanceof FileNotFoundException;
}

代码示例来源:origin: square/okhttp

CacheHttpURLConnection(Response response) {
 super(response.request().url().url());
 this.request = response.request();
 this.response = response;
 // Configure URLConnection inherited fields.
 this.connected = true;
 this.doOutput = request.body() != null;
 this.doInput = true;
 this.useCaches = true;
 // Configure HttpUrlConnection inherited fields.
 this.method = request.method();
}

代码示例来源:origin: square/okhttp

@Override public Response intercept(Chain chain) throws IOException {
 Request originalRequest = chain.request();
 if (originalRequest.body() == null || originalRequest.header("Content-Encoding") != null) {
  return chain.proceed(originalRequest);
 }
 Request compressedRequest = originalRequest.newBuilder()
   .header("Content-Encoding", "gzip")
   .method(originalRequest.method(), gzip(originalRequest.body()))
   .build();
 return chain.proceed(compressedRequest);
}

代码示例来源:origin: stackoverflow.com

OkHttpClient okClient = new OkHttpClient();
okClient.interceptors().add(new Interceptor() {
   @Override
   public Response intercept(Interceptor.Chain chain) throws IOException {
     Request original = chain.request();
     // Request customization: add request headers
     Request.Builder requestBuilder = original.newBuilder()
         .header("Authorization", token)
         .method(original.method(), original.body());
     Request request = requestBuilder.build();
     return chain.proceed(request);
   }
});

代码示例来源:origin: stackoverflow.com

OkHttpClient okClient = new OkHttpClient.Builder()
     .addInterceptor(
       new Interceptor() {
        @Override
        public Response intercept(Interceptor.Chain chain) throws IOException {
           Request original = chain.request();
           // Request customization: add request headers
           Request.Builder requestBuilder = original.newBuilder()
               .header("Authorization", token)
               .method(original.method(), original.body());
           Request request = requestBuilder.build();
           return chain.proceed(request);
         }
       })
     .build();

代码示例来源:origin: stackoverflow.com

private static String bodyToString(final Request request){

  try {
    final Request copy = request.newBuilder().build();
    final Buffer buffer = new Buffer();
    copy.body().writeTo(buffer);
    return buffer.readUtf8();
  } catch (final IOException e) {
    return "did not work";
  }
}

代码示例来源:origin: amitshekhariitbhu/Fast-Android-Networking

@Override
public Response intercept(Chain chain) throws IOException {
  Request originalRequest = chain.request();
  if (originalRequest.body() == null || originalRequest.header("Content-Encoding") != null) {
    return chain.proceed(originalRequest);
  }
  Request compressedRequest = originalRequest.newBuilder()
      .header("Content-Encoding", "gzip")
      .method(originalRequest.method(), forceContentLength(gzip(originalRequest.body())))
      .build();
  return chain.proceed(compressedRequest);
}

代码示例来源:origin: SonarSource/sonarqube

private Response followPostRedirect(Response response, PostRequest postRequest) {
 String location = response.header("Location");
 if (location == null) {
  throw new IllegalStateException(format("Missing HTTP header 'Location' in redirect of %s", response.request().url()));
 }
 HttpUrl url = response.request().url().resolve(location);
 // Don't follow redirects to unsupported protocols.
 if (url == null) {
  throw new IllegalStateException(format("Unsupported protocol in redirect of %s to %s", response.request().url(), location));
 }
 Request.Builder redirectRequest = response.request().newBuilder();
 redirectRequest.post(response.request().body());
 response.body().close();
 return doCall(prepareOkHttpClient(noRedirectOkHttpClient, postRequest), redirectRequest.url(url).build());
}

代码示例来源:origin: square/okhttp

if (request.body() instanceof OutputStreamRequestBody) {
 OutputStreamRequestBody requestBody = (OutputStreamRequestBody) request.body();
 request = requestBody.prepareToSendRequest(request);

代码示例来源:origin: square/okhttp

networkInterceptor.proceed();
OutputStreamRequestBody requestBody = (OutputStreamRequestBody) call.request().body();
if (requestBody != null) requestBody.outputStream().close();

代码示例来源:origin: jeasonlzy/okhttp-OkGo

private void bodyToString(Request request) {
    try {
      Request copy = request.newBuilder().build();
      RequestBody body = copy.body();
      if (body == null) return;
      Buffer buffer = new Buffer();
      body.writeTo(buffer);
      Charset charset = getCharset(body.contentType());
      log("\tbody:" + buffer.readString(charset));
    } catch (Exception e) {
      OkLogger.printStackTrace(e);
    }
  }
}

代码示例来源:origin: square/okhttp

Request.Builder requestBuilder = userRequest.newBuilder();
RequestBody body = userRequest.body();
if (body != null) {
 MediaType contentType = body.contentType();

代码示例来源:origin: facebook/stetho

@Nullable
@Override
public byte[] body() throws IOException {
 RequestBody body = mRequest.body();
 if (body == null) {
  return null;
 }
 OutputStream out = mRequestBodyHelper.createBodySink(firstHeaderValue("Content-Encoding"));
 BufferedSink bufferedSink = Okio.buffer(Okio.sink(out));
 try {
  body.writeTo(bufferedSink);
 } finally {
  bufferedSink.close();
 }
 return mRequestBodyHelper.getDisplayBody();
}

代码示例来源:origin: square/okhttp

@Override public void writeRequestHeaders(Request request) throws IOException {
 if (stream != null) return;
 boolean hasRequestBody = request.body() != null;
 List<Header> requestHeaders = http2HeadersList(request);
 stream = connection.newStream(requestHeaders, hasRequestBody);
 // We may have been asked to cancel while creating the new stream and sending the request
 // headers, but there was still no stream to close.
 if (canceled) {
  stream.closeLater(ErrorCode.CANCEL);
  throw new IOException("Canceled");
 }
 stream.readTimeout().timeout(chain.readTimeoutMillis(), TimeUnit.MILLISECONDS);
 stream.writeTimeout().timeout(chain.writeTimeoutMillis(), TimeUnit.MILLISECONDS);
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void upload() throws IOException {
 ArgumentCaptor<Request> requestCaptor = ArgumentCaptor.forClass(Request.class);
 settings.setProperty(SONAR_TELEMETRY_URL.getKey(), TELEMETRY_URL);
 underTest.start();
 underTest.upload(JSON);
 verify(okHttpClient).newCall(requestCaptor.capture());
 Request request = requestCaptor.getValue();
 assertThat(request.method()).isEqualTo("POST");
 assertThat(request.body().contentType()).isEqualTo(MediaType.parse("application/json; charset=utf-8"));
 Buffer body = new Buffer();
 request.body().writeTo(body);
 assertThat(body.readUtf8()).isEqualTo(JSON);
 assertThat(request.url().toString()).isEqualTo(TELEMETRY_URL);
}

代码示例来源:origin: SonarSource/sonarqube

@Test
 public void opt_out() throws IOException {
  ArgumentCaptor<Request> requestCaptor = ArgumentCaptor.forClass(Request.class);
  settings.setProperty(SONAR_TELEMETRY_URL.getKey(), TELEMETRY_URL);
  underTest.start();

  underTest.optOut(JSON);

  verify(okHttpClient).newCall(requestCaptor.capture());
  Request request = requestCaptor.getValue();
  assertThat(request.method()).isEqualTo("DELETE");
  assertThat(request.body().contentType()).isEqualTo(MediaType.parse("application/json; charset=utf-8"));
  Buffer body = new Buffer();
  request.body().writeTo(body);
  assertThat(body.readUtf8()).isEqualTo(JSON);
  assertThat(request.url().toString()).isEqualTo(TELEMETRY_URL);
 }
}

代码示例来源:origin: square/okhttp

@Override public OutputStream getOutputStream() throws IOException {
 OutputStreamRequestBody requestBody = (OutputStreamRequestBody) buildCall().request().body();
 if (requestBody == null) {
  throw new ProtocolException("method does not support a request body: " + method);
 }
 // If this request needs to stream bytes to the server, build a physical connection immediately
 // and start streaming those bytes over that connection.
 if (requestBody instanceof StreamedRequestBody) {
  connect();
  networkInterceptor.proceed();
 }
 if (requestBody.isClosed()) {
  throw new ProtocolException("cannot write request body after response has been read");
 }
 return requestBody.outputStream();
}

代码示例来源:origin: web3j/web3j

@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
  Request request = chain.request();
  this.requestBody = request.body();
  okhttp3.Response response = new okhttp3.Response.Builder()
      .request(chain.request())
      .protocol(Protocol.HTTP_2)
      .code(200)
      .message("")
      .build();
  return response;
}

代码示例来源:origin: com.squareup.okhttp3/okhttp

@Override public void writeRequestHeaders(Request request) throws IOException {
 if (stream != null) return;
 boolean hasRequestBody = request.body() != null;
 List<Header> requestHeaders = http2HeadersList(request);
 stream = connection.newStream(requestHeaders, hasRequestBody);
 // We may have been asked to cancel while creating the new stream and sending the request
 // headers, but there was still no stream to close.
 if (canceled) {
  stream.closeLater(ErrorCode.CANCEL);
  throw new IOException("Canceled");
 }
 stream.readTimeout().timeout(chain.readTimeoutMillis(), TimeUnit.MILLISECONDS);
 stream.writeTimeout().timeout(chain.writeTimeoutMillis(), TimeUnit.MILLISECONDS);
}

相关文章