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

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

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

Request.newBuilder介绍

暂无

代码示例

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

@Override public Response intercept(Chain chain) throws IOException {
  Request request = chain.request();
  if (request.url().host().equals(host)) {
   request = request.newBuilder()
     .header("Authorization", credentials)
     .build();
  }
  return chain.proceed(request);
 }
}

代码示例来源:origin: bumptech/glide

@Override
 public Response intercept(Chain chain) throws IOException {
  return chain.proceed(
    chain.request()
      .newBuilder()
      .addHeader("Authorization", "Client-ID " + ImgurService.CLIENT_ID)
      .build());
 }
})

代码示例来源:origin: prestodb/presto

public static Interceptor userAgent(String userAgent)
{
  return chain -> chain.proceed(chain.request().newBuilder()
      .header(USER_AGENT, userAgent)
      .build());
}

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

@Override public okhttp3.Response intercept(Chain chain) throws IOException {
  Request request = chain.request();
  String host = this.host;
  if (host != null) {
   HttpUrl newUrl = request.url().newBuilder()
     .host(host)
     .build();
   request = request.newBuilder()
     .url(newUrl)
     .build();
  }
  return chain.proceed(request);
 }
}

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

@Override public Request authenticate(Route route, Response response) throws IOException {
  if (response.request().header("Authorization") != null) {
   return null; // Give up, we've already attempted to authenticate.
  }
  System.out.println("Authenticating for response: " + response);
  System.out.println("Challenges: " + response.challenges());
  String credential = Credentials.basic("jesse", "password1");
  return response.request().newBuilder()
    .header("Authorization", credential)
    .build();
 }
})

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

private @Nullable Response getCacheOnlyResponse(Request request) {
 if (!post && client.cache() != null) {
  try {
   Request cacheRequest = request.newBuilder().cacheControl(CacheControl.FORCE_CACHE).build();
   Response cacheResponse = client.newCall(cacheRequest).execute();
   if (cacheResponse.code() != 504) {
    return cacheResponse;
   }
  } catch (IOException ioe) {
   // Failures are ignored as we can fallback to the network
   // and hopefully repopulate the cache.
  }
 }
 return null;
}

代码示例来源: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: SonarSource/sonarqube

private Response addHeaders(Interceptor.Chain chain) throws IOException {
 Request.Builder newRequest = chain.request().newBuilder();
 if (userAgent != null) {
  newRequest.header("User-Agent", userAgent);
 }
 if (credentials != null) {
  newRequest.header("Authorization", credentials);
 }
 return chain.proceed(newRequest.build());
}

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

@Override
  public Response intercept(Chain chain) throws IOException
  {
    Request userAgentRequest = chain.request()
      .newBuilder()
      .header("User-Agent", USER_AGENT)
      .build();
    return chain.proceed(userAgentRequest);
  }
})

代码示例来源:origin: prestodb/presto

public static Interceptor basicAuth(String user, String password)
{
  requireNonNull(user, "user is null");
  requireNonNull(password, "password is null");
  if (user.contains(":")) {
    throw new ClientException("Illegal character ':' found in username");
  }
  String credential = Credentials.basic(user, password);
  return chain -> chain.proceed(chain.request().newBuilder()
      .header(AUTHORIZATION, credential)
      .build());
}

代码示例来源:origin: prestodb/presto

public static Interceptor tokenAuth(String accessToken)
{
  requireNonNull(accessToken, "accessToken is null");
  checkArgument(CharMatcher.inRange((char) 33, (char) 126).matchesAllOf(accessToken));
  return chain -> chain.proceed(chain.request().newBuilder()
      .addHeader(AUTHORIZATION, "Bearer " + accessToken)
      .build());
}

代码示例来源:origin: jaydenxiao2016/AndroidFire

@Override
  public Response intercept(Chain chain) throws IOException {
    Request build = chain.request().newBuilder()
        .addHeader("Content-Type", "application/json")
        .build();
    return chain.proceed(build);
  }
};

代码示例来源: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: allure-framework/allure2

@Override
public Response intercept(final Interceptor.Chain chain) throws IOException {
  final Request request = chain.request();
  final Request authenticatedRequest = request.newBuilder()
      .header("Authorization", credentials).build();
  return chain.proceed(authenticatedRequest);
}

代码示例来源:origin: prestodb/presto

private Request authenticate(Request request)
{
  String hostName = request.url().host();
  String principal = makeServicePrincipal(remoteServiceName, hostName, useCanonicalHostname);
  byte[] token = generateToken(principal);
  String credential = format("%s %s", NEGOTIATE, Base64.getEncoder().encodeToString(token));
  return request.newBuilder()
      .header(AUTHORIZATION, credential)
      .build();
}

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

@Override public Response intercept(Chain chain) throws IOException {
  Request request = chain.request();
  Headers newHeaders = request.headers()
    .newBuilder()
    .add("Date", new Date())
    .build();
  Request newRequest = request.newBuilder()
    .headers(newHeaders)
    .build();
  return chain.proceed(newRequest);
 }
}

代码示例来源:origin: HotBitmapGG/bilibili-android-client

@Override
  public Response intercept(Chain chain) throws IOException {
    Request originalRequest = chain.request();
    Request requestWithUserAgent = originalRequest.newBuilder()
        .removeHeader("User-Agent")
        .addHeader("User-Agent", ApiConstants.COMMON_UA_STR)
        .build();
    return chain.proceed(requestWithUserAgent);
  }
}

代码示例来源:origin: apache/nifi

@Nullable
  @Override
  public Request authenticate(Route route, Response response) throws IOException {
    String credential = Credentials.basic(proxyUsername, proxyPassword);
    return response.request()
        .newBuilder()
        .header("Proxy-Authorization", credential)
        .build();
  }
}

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

/**
 * Now that we've buffered the entire request body, update the request headers and the body
 * itself. This happens late to enable HttpURLConnection users to complete the socket connection
 * before sending request body bytes.
 */
@Override public Request prepareToSendRequest(Request request) throws IOException {
 if (request.header("Content-Length") != null) return request;
 outputStream().close();
 contentLength = buffer.size();
 return request.newBuilder()
   .removeHeader("Transfer-Encoding")
   .header("Content-Length", Long.toString(buffer.size()))
   .build();
}

代码示例来源:origin: googlemaps/google-maps-services-java

@Override
 public Request authenticate(Route route, Response response) throws IOException {
  String credential = Credentials.basic(userName, password);
  return response
    .request()
    .newBuilder()
    .header("Proxy-Authorization", credential)
    .build();
 }
});

相关文章