Java Examples for com.squareup.okhttp.Authenticator
The following java examples will help you to understand the usage of com.squareup.okhttp.Authenticator. These source code samples are taken from different open source projects.
Example 1
| Project: send-notification-master File: PushbulletNotifier.java View source code |
@Override
public Notifier init() {
if (client != null) {
return this;
}
client = new OkHttpClient();
client.setAuthenticator(new Authenticator() {
@Override
public Request authenticate(Proxy proxy, Response response) throws IOException {
String credentials = Credentials.basic(configuration.key(), "");
if (credentials.equals(response.request().header("Authorization"))) {
// If we already failed with these credentials, don't retry.
return null;
}
return response.request().newBuilder().header("Authorization", credentials).build();
}
@Override
public Request authenticateProxy(Proxy proxy, Response response) throws IOException {
return authenticate(proxy, response);
}
});
return this;
}Example 2
| Project: tomahawk-android-master File: NetworkUtils.java View source code |
/**
* Does a HTTP or HTTPS request
*
* @param method the method that should be used ("GET" or "POST"), defaults to "GET"
* (optional)
* @param urlString the complete url string to do the request with
* @param extraHeaders extra headers that should be added to the request (optional)
* @param username the username for HTTP Basic Auth (optional)
* @param password the password for HTTP Basic Auth (optional)
* @param data the body data included in POST requests (optional)
* @param followRedirects whether or not to follow redirects (also defines what is being
* returned)
* @param cookieManager the {@link CookieManager} that should be used for this request
* @return a HttpURLConnection
*/
public static Response httpRequest(String method, String urlString, Map<String, String> extraHeaders, final String username, final String password, String data, boolean followRedirects, CookieManager cookieManager) throws IOException {
OkHttpClient client = new OkHttpClient();
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
client.networkInterceptors().add(loggingInterceptor);
if (cookieManager != null) {
client.setCookieHandler(cookieManager);
}
//Set time-outs
client.setConnectTimeout(15000, TimeUnit.MILLISECONDS);
client.setReadTimeout(15000, TimeUnit.MILLISECONDS);
client.setFollowRedirects(followRedirects);
// Configure HTTP Basic Auth if available
if (username != null && password != null) {
client.setAuthenticator(new com.squareup.okhttp.Authenticator() {
@Override
public Request authenticate(Proxy proxy, Response response) throws IOException {
String credential = Credentials.basic(username, password);
return response.request().newBuilder().header("Authorization", credential).build();
}
@Override
public Request authenticateProxy(Proxy proxy, Response response) throws IOException {
return null;
}
});
}
// Create request for remote resource.
Request.Builder builder = new Request.Builder().url(urlString);
// Add headers if available
if (extraHeaders != null) {
for (String key : extraHeaders.keySet()) {
builder.addHeader(key, extraHeaders.get(key));
}
}
// Properly set up the request method. Default to GET
if (method != null) {
method = method.toUpperCase();
}
if (method == null || method.equals("GET")) {
builder.get();
} else {
MediaType mediaType = MEDIA_TYPE_FORM;
if (extraHeaders != null) {
String contentType = extraHeaders.get("Content-Type");
if (contentType != null) {
mediaType = MediaType.parse(contentType);
}
}
RequestBody requestBody = null;
if (data != null) {
requestBody = RequestBody.create(mediaType, data);
}
builder.method(method, requestBody);
}
// Build and execute the request and retrieve the response.
Request request = builder.build();
return client.newCall(request).execute();
}Example 3
| Project: android-smsmms-master File: MmsHttpClient.java View source code |
/**
* Open an HTTP connection
*
* TODO: The following code is borrowed from android.net.Network.openConnection
* Once that method supports proxy, we should use that instead
* Also we should remove the associated HostResolver and ConnectionPool from
* MmsNetworkManager
*
* @param url The URL to connect to
* @param proxy The proxy to use
* @return The opened HttpURLConnection
* @throws MalformedURLException If URL is malformed
*/
private HttpURLConnection openConnection(URL url, final Proxy proxy) throws MalformedURLException {
final String protocol = url.getProtocol();
OkHttpClient okHttpClient;
if (protocol.equals("http")) {
okHttpClient = new OkHttpClient();
okHttpClient.setFollowRedirects(false);
okHttpClient.setProtocols(Arrays.asList(Protocol.HTTP_1_1));
okHttpClient.setProxySelector(new ProxySelector() {
@Override
public List<Proxy> select(URI uri) {
if (proxy != null) {
return Arrays.asList(proxy);
} else {
return new ArrayList<Proxy>();
}
}
@Override
public void connectFailed(URI uri, SocketAddress address, IOException failure) {
}
});
okHttpClient.setAuthenticator(new com.squareup.okhttp.Authenticator() {
@Override
public Request authenticate(Proxy proxy, Response response) throws IOException {
return null;
}
@Override
public Request authenticateProxy(Proxy proxy, Response response) throws IOException {
return null;
}
});
okHttpClient.setConnectionSpecs(Arrays.asList(ConnectionSpec.CLEARTEXT));
okHttpClient.setConnectionPool(new ConnectionPool(3, 60000));
okHttpClient.setSocketFactory(SocketFactory.getDefault());
Internal.instance.setNetwork(okHttpClient, mHostResolver);
if (proxy != null) {
okHttpClient.setProxy(proxy);
}
return new HttpURLConnectionImpl(url, okHttpClient);
} else if (protocol.equals("https")) {
okHttpClient = new OkHttpClient();
okHttpClient.setProtocols(Arrays.asList(Protocol.HTTP_1_1));
HostnameVerifier verifier = HttpsURLConnection.getDefaultHostnameVerifier();
okHttpClient.setHostnameVerifier(verifier);
okHttpClient.setSslSocketFactory(HttpsURLConnection.getDefaultSSLSocketFactory());
okHttpClient.setProxySelector(new ProxySelector() {
@Override
public List<Proxy> select(URI uri) {
return Arrays.asList(proxy);
}
@Override
public void connectFailed(URI uri, SocketAddress address, IOException failure) {
}
});
okHttpClient.setAuthenticator(new com.squareup.okhttp.Authenticator() {
@Override
public Request authenticate(Proxy proxy, Response response) throws IOException {
return null;
}
@Override
public Request authenticateProxy(Proxy proxy, Response response) throws IOException {
return null;
}
});
okHttpClient.setConnectionSpecs(Arrays.asList(ConnectionSpec.CLEARTEXT));
okHttpClient.setConnectionPool(new ConnectionPool(3, 60000));
Internal.instance.setNetwork(okHttpClient, mHostResolver);
return new HttpsURLConnectionImpl(url, okHttpClient);
} else {
throw new MalformedURLException("Invalid URL or unrecognized protocol " + protocol);
}
}Example 4
| Project: js-android-sdk-master File: Server.java View source code |
private void configureAuthenticator(OkHttpClient client, AuthStrategy authStrategy) {
Authenticator recoverableAuthenticator = new RecoverableAuthenticator(authStrategy, mCredentials);
Authenticator authenticator = recoverableAuthenticator;
if (mAuthenticationPolicy == AuthPolicy.FAIL_FAST) {
authenticator = new SingleTimeAuthenticator(recoverableAuthenticator);
}
client.setAuthenticator(authenticator);
}Example 5
| Project: Kore-master File: HostConnection.java View source code |
/**
* Initializes this class OkHttpClient
*/
public OkHttpClient getOkHttpClient() {
if (httpClient == null) {
httpClient = new OkHttpClient();
httpClient.setConnectTimeout(connectTimeout, TimeUnit.MILLISECONDS);
httpClient.setAuthenticator(new Authenticator() {
@Override
public Request authenticate(Proxy proxy, Response response) throws IOException {
if (TextUtils.isEmpty(hostInfo.getUsername()))
return null;
String credential = Credentials.basic(hostInfo.getUsername(), hostInfo.getPassword());
return response.request().newBuilder().header("Authorization", credential).build();
}
@Override
public Request authenticateProxy(Proxy proxy, Response response) throws IOException {
return null;
}
});
}
return httpClient;
}Example 6
| Project: nifi-master File: MultiAuthenticator.java View source code |
public Builder with(String scheme, Authenticator authenticator) {
registry.put(scheme, authenticator);
return this;
}