How can I send HTTP Basic Authentication headers in Android?

前端 未结 3 1481
滥情空心
滥情空心 2020-12-18 11:07

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 Rest         


        
相关标签:
3条回答
  • 2020-12-18 11:14

    Maybe the documentation of HttpClient can help: link

    0 讨论(0)
  • 2020-12-18 11:20

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

    0 讨论(0)
  • 2020-12-18 11:22

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

        if (authState.getAuthScheme() == null) {
            AuthScope authScope = new Au    HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() {
        public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
            AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
            CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(
                    ClientContext.CREDS_PROVIDER);
            HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);thScope(targetHost.getHostName(), targetHost.getPort());
            Credentials creds = credsProvider.getCredentials(authScope);
            if (creds != null) {
                authState.setAuthScheme(new BasicScheme());
                authState.setCredentials(creds);
            }
        }
    }    
    };
    DefaultHttpClient httpclient = new DefaultHttpClient();
    httpclient.addRequestInterceptor(preemptiveAuth, 0);
    
    0 讨论(0)
提交回复
热议问题