0

I want to add authentication header to my request. I'm using DefaultHttpClient from Apache httpclient 4.0.

I found that's done this way:

URI uri = new URI("http://www.bla.bla/folder/");

String host = uri.getHost();
int port = uri.getPort();

httpClient.getCredentialsProvider().setCredentials(
        new AuthScope(host, port, AuthScope.ANY_SCHEME),
        new UsernamePasswordCredentials("myuser", "mypassword")
        );

This is executed and even with the debugger I see some credentials variable of the httpClient are set at the moment of doing the request. But I inspect web traffic with Charles and there's no authentication header.

Content of vars:

host: www.bla.bla

port: -1

Btw. I enabled Charles as a proxy to see the headers of the request, with:

HttpHost proxy = new HttpHost("127.0.0.1", 8888, "http");
httpParameters.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

I think that should not be altering my headers, would make no sense for webproxy... anyways if I disable the proxy stuff it also doesn't work (although I can't see the content of the header but I suppose it's the same reason).

Also tried using a request interceptor like described in Softhinker.com's post here: How can I send HTTP Basic Authentication headers in Android?

And I get exactly the same request, without authentification header.

What am I doing wrong?

Thanks in advance.

Community
  • 1
  • 1
User
  • 31,811
  • 40
  • 131
  • 232
  • Both the explanation and solution are covered in this thread... http://stackoverflow.com/questions/18108783/apache-httpclient-doesnt-set-basic-authentication-credentials –  May 07 '14 at 16:19

1 Answers1

0

I got it working setting the header "manually" in the request.

request.setHeader(new BasicHeader("Authorization", authstring));
User
  • 31,811
  • 40
  • 131
  • 232