@@ -0,0 +1,2166 @@
+/*%0A * Copyright (C) 2018 Square, Inc.%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *    http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0Apackage okhttp3.recipes;%0A%0Aimport okhttp3.Credentials;%0Aimport okhttp3.Interceptor;%0Aimport okhttp3.OkHttpClient;%0Aimport okhttp3.Request;%0Aimport okhttp3.Response;%0A%0Aimport java.io.IOException;%0A%0Apublic class PreemptiveAuth %7B%0A  private final OkHttpClient client;%0A%0A  public PreemptiveAuth() %7B%0A    client = new OkHttpClient.Builder()%0A        .addInterceptor(%0A            new BasicAuthInterceptor(%22publicobject.com%22, %22jesse%22, %22password1%22))%0A        .build();%0A  %7D%0A%0A  public void run() throws Exception %7B%0A    Request request = new Request.Builder()%0A        .url(%22https://publicobject.com/secrets/hellosecret.txt%22)%0A        .build();%0A%0A    try (Response response = client.newCall(request).execute()) %7B%0A      if (!response.isSuccessful()) throw new IOException(%22Unexpected code %22 + response);%0A%0A      System.out.println(response.body().string());%0A    %7D%0A  %7D%0A%0A  public static void main(String... args) throws Exception %7B%0A    new PreemptiveAuth().run();%0A  %7D%0A%0A  class BasicAuthInterceptor implements Interceptor %7B%0A    private final String credentials;%0A    private final String host;%0A%0A    BasicAuthInterceptor(String host, String username, String password) %7B%0A      this.credentials = Credentials.basic(username, password);%0A      this.host = host;%0A    %7D%0A%0A    @Override%0A    public Response intercept(Chain chain) throws IOException %7B%0A      Request request = chain.request();%0A      if (request.url().host().equals(host)) %7B%0A        request = request.newBuilder()%0A            .header(%22Authorization%22, credentials)%0A            .build();%0A      %7D%0A      return chain.proceed(request);%0A    %7D%0A  %7D%0A%0A%7D%0A

Add basic auth interceptor recipe (#4336)