Skip to content Skip to sidebar Skip to footer

How Can I Send Http Basic Authentication Headers In Android?

I am not sure how to send HTTP Auth headers. I have the following HttpClient to get requests, but not sure how I can send requests? public class RestClient extends AsyncTask

Solution 1:

This is covered in the HttpClient documentation and in their sample code.

Solution 2:

Maybe the documentation of HttpClient can help: link

Solution 3:

Since Android compiles HttpClient 4.0.x instead of 3.x, below snippet is for your reference.

if (authState.getAuthScheme() == null) {
        AuthScopeauthScope=newAuHttpRequestInterceptorpreemptiveAuth=newHttpRequestInterceptor() {
    publicvoidprocess(final HttpRequest request, final HttpContext context)throws HttpException, IOException {
        AuthStateauthState= (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
        CredentialsProvidercredsProvider= (CredentialsProvider) context.getAttribute(
                ClientContext.CREDS_PROVIDER);
        HttpHosttargetHost= (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);thScope(targetHost.getHostName(), targetHost.getPort());
        Credentialscreds= credsProvider.getCredentials(authScope);
        if (creds != null) {
            authState.setAuthScheme(newBasicScheme());
            authState.setCredentials(creds);
        }
    }
}    
};
DefaultHttpClienthttpclient=newDefaultHttpClient();
httpclient.addRequestInterceptor(preemptiveAuth, 0);

Post a Comment for "How Can I Send Http Basic Authentication Headers In Android?"