Skip to content

Instantly share code, notes, and snippets.

@kgsnipes
Created April 1, 2014 05:32
Show Gist options
  • Save kgsnipes/9908248 to your computer and use it in GitHub Desktop.
Save kgsnipes/9908248 to your computer and use it in GitHub Desktop.
preemptive http basic authentication in apache http client library
public class BasicAuth
{
public static void main(String a[])throws Exception
{
System.out.println(getHttpResponse("78.101.139.84"));
}
public static String getHttpResponse(final String ip) throws IOException
{
String responseString = null;
final HttpHost targetHost = new HttpHost("examplehost.com", 443, "https");
final CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("username","password"));
final AuthCache authCache = new BasicAuthCache();
authCache.put(targetHost, new BasicScheme());
// Add AuthCache to the execution context
final HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
context.setAuthCache(authCache);
RequestConfig.Builder requestBuilder = RequestConfig.custom();
requestBuilder = requestBuilder.setAuthenticationEnabled(true);
final CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(requestBuilder.build())
.setDefaultCredentialsProvider(credsProvider).build();
try
{
final HttpGet httpget = new HttpGet("url");
System.out.println("Executing request " + httpget.getRequestLine());
final CloseableHttpResponse resp = httpclient.execute(httpget, context);
try
{
System.out.println("----------------------------------------");
System.out.println(resp.getStatusLine());
responseString = EntityUtils.toString(resp.getEntity());
System.out.println("the response string is :" + responseString);
EntityUtils.consume(resp.getEntity());
}
finally
{
resp.close();
}
}
finally
{
httpclient.close();
}
return responseString;
}
}
@jitendrapataskar
Copy link

no use without package detail...

@bigsteve4288
Copy link

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScheme;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.AuthState;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.ClientContext;
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.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

@bigsteve4288
Copy link

package detail listed above

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment