import org.apache.http.HttpHost;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
public class BasicAuthenticationExample {
public static void main(String[] args) throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpHost targetHost = new HttpHost("api.example.com");
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(targetHost.getHostName(), targetHost.getPort()),
new UsernamePasswordCredentials("username", "password"));
AuthCache authCache = new BasicAuthCache();
authCache.put(targetHost, new BasicScheme());
HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
context.setAuthCache(authCache);
HttpGet httpGet = new HttpGet("/resource");
CloseableHttpResponse response = httpClient.execute(targetHost, httpGet, context);
response.close();
httpClient.close();
}
}
import org.apache.http.HttpHost;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.auth.DigestScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
public class DigestAuthenticationExample {
public static void main(String[] args) throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpHost targetHost = new HttpHost("api.example.com");
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(targetHost.getHostName(), targetHost.getPort()),
new UsernamePasswordCredentials("username", "password"));
AuthCache authCache = new BasicAuthCache();
DigestScheme digestScheme = new DigestScheme();
digestAuthCache.put(targetHost, digestScheme);
HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
context.setAuthCache(authCache);
HttpGet httpGet = new HttpGet("/resource");
CloseableHttpResponse response = httpClient.execute(targetHost, httpGet, context);
response.close();
httpClient.close();
}
}
import org.apache.http.HttpHost;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.client.protocol.HttpClientContext;
public class BearerTokenAuthenticationExample {
public static void main(String[] args) throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpHost targetHost = new HttpHost("api.example.com");
HttpClientContext context = HttpClientContext.create();
context.setAuthCache(authCache);
HttpGet httpGet = new HttpGet("/resource");
httpGet.setHeader("Authorization", "Bearer your_token");
CloseableHttpResponse response = httpClient.execute(targetHost, httpGet, context);
response.close();
httpClient.close();
}
}
import org.apache.http.HttpHost;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
public class OAuthAuthenticationExample {
public static void main(String[] args) throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpHost targetHost = new HttpHost("api.example.com");
HttpGet httpGet = new HttpGet("/resource");
httpGet.setHeader("Authorization", "OAuth your_token");
CloseableHttpResponse response = httpClient.execute(targetHost, httpGet);
response.close();
httpClient.close();
}
}