Refine search
@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); } }
@Override public Response intercept(Chain chain) throws IOException { return chain.proceed( chain.request() .newBuilder() .addHeader("Authorization", "Client-ID " + ImgurService.CLIENT_ID) .build()); } })
public static Interceptor userAgent(String userAgent) { return chain -> chain.proceed(chain.request().newBuilder() .header(USER_AGENT, userAgent) .build()); }
@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); } }
@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(); } })
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; }
@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); }
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()); }
@Override public Response intercept(Chain chain) throws IOException { Request userAgentRequest = chain.request() .newBuilder() .header("User-Agent", USER_AGENT) .build(); return chain.proceed(userAgentRequest); } })
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()); }
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()); }
@Override public Response intercept(Chain chain) throws IOException { Request build = chain.request().newBuilder() .addHeader("Content-Type", "application/json") .build(); return chain.proceed(build); } };
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); } });
@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); }
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(); }
@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); } }
@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); } }
@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(); } }
/** * 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(); }
@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(); } });