2

I cannot get the Basic Authentication to work on HTTP GET. The getCredentialsProvider does not exist, then I look around and tried HttpBuilder but that do not exist either. I run the Android Sdk update still no luck. I spend three hours looking around and tried every one I found but there was always some part that didn't exist.

HttpClient httpclient = new DefaultHttpClient();
String username = "User";
String password = "Pass";
Credentials credentials = new UsernamePasswordCredentials(username , password );
httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY,credentials);

Thanks

1
  • I dont know the answer, but you should NOT NOT NOT use Basic Athentication. It is highly insecure. Any device can break that. Use Digest instead! Commented Sep 15, 2014 at 7:58

3 Answers 3

3

You use httpConnection, an uri is something like http://www.google.com, but as Kim HJ said, its really insecure, so use it only for testing or in a highly secure network. Otherwise your credentials are public domain ;-)

URL url = new URL(uri);
HttpURLConnection httpRequest = (HttpURLConnection) url.openConnection();
httpRequest.setRequestMethod("GET");
httpRequest.setDoInput(true);
String authString = username + ":" + password;
byte[] authEncBytes = android.util.Base64.encode(authString.getBytes(), android.util.Base64.DEFAULT);
String authStringEnc = new String(authEncBytes);
httpsRequest.addRequestProperty("Authorization", "Basic "+ authStringEnc);
Sign up to request clarification or add additional context in comments.

1 Comment

when I try to get the result it is empty httpRequest.connect(); reader = new BufferedReader(new InputStreamReader(httpRequest.getInputStream()));
1
Its worked for me.

Authenticator.setDefault(new Authenticator(){
    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication("xxx","xxx1234".toCharArray());
}});

HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(Integer.parseInt(context.getResources().getString(R.string.connection_timeout)));
connection.setUseCaches(false);
connection.connect();
HttpURLConnection httpConnection  =  connection;
int responseCode  =  httpConnection.getResponseCode();
if (responseCode   ==   HttpURLConnection.HTTP_OK) {
    InputStream in  =  httpConnection.getInputStream();
}

Comments

1

There are 2 ways to do HTTP basic authentication:

  1. Add in Header
HttpUriRequest request = new HttpGet(YOUR_URL);
String credentials = YOUR_USERNAME + ":" + YOUR_PASSWORD; 
String base64EncodedCredentials = Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
request.addHeader("Authorization", "Basic " + base64EncodedCredentials);
  1. Add credentials to Credentials Provider.
defaultHttpClient.getCredentialsProvider().setCredentials(
            new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
            new UsernamePasswordCredentials(
                    HTTPS_BASIC_AUTH_USERNAME,
                    HTTPS_BASIC_AUTH_PASWORD));

Hope this might help you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.