8

Thanks in advance...

i m using this code for set http header in http request for authenticate url..

but i think some what is mising thats why i could not get response...

response still comes like "authoriazation required"...

httpParameters = new BasicHttpParams();
String auth = android.util.Base64.encodeToString(
    ("[email protected]" + ":" + "test2323" + ":" + "zitec"
    + ":" + "7716099f468cc71670b68cf4b3ba545c5760baa7")
    .getBytes("UTF-8"), android.util.Base64.NO_WRAP);

HttpConnectionParams.setSoTimeout(httpParameters, 0);
client = new DefaultHttpClient(httpParameters);
String getURL = "URL here";
get = new HttpGet(getURL);
get.addHeader("Authorization", "Basic "+ auth);
// get.addHeader("X-ZFWS-Accept", "text/json");

HttpResponse responseGet = client.execute(get);
HttpEntity resEntityGet = responseGet.getEntity();
if (resEntityGet != null) {
    //do something with the response
    //Toast.makeText(Login.this.getApplicationContext(),EntityUtils.toString(resEntityGet), Toast.LENGTH_SHORT).show();
    String s = EntityUtils.toString(resEntityGet);
    tv1.setText(s);
}
}catch(Exception e){
}

plssss help me as soon as possible

1 Answer 1

8

Yor code is fine. you have the http client setup right and the header added right. but. your basic authentication is wrong. the header key (Authentication) is right but the value should be Basic + Base64.encode(username+":"+pass)

also the alternative to that is the folowing code:

 httpclient.getCredentialsProvider().setCredentials(
                new AuthScope(targetHost.getHostName(), targetHost.getPort()),
                new UsernamePasswordCredentials("username", "password"));

The target Hostname is probably null and the port -1.

but this one you wont be able to use if you are using url connection so a header is also acceptable.

EDIT:

HERE IS YOUR ISSUE:

String auth = android.util.Base64.encodeToString(
    ("[email protected]" + ":" + "test2323" + ":" + "zitec"
    + ":" + "7716099f468cc71670b68cf4b3ba545c5760baa7")
    .getBytes("UTF-8"), android.util.Base64.NO_WRAP);

What are all theese concatenations??

Basic authentication means adding a base64 encoded Authorization header which consisnt of the word "Basic " + Base64.encodeToString("yourUsername"+":"+"yourPassword)

The alternative is adding the method i pasted at the top about the credentials provider the same way

Sign up to request clarification or add additional context in comments.

3 Comments

i am not getting can you describe? please
thanks ..now its working good..but can u please help me that how can i do the same thing with httppost method..coz in one of my link i have to pass the parameters using post method.so using Base64 how can i do the same using POST..
you can just add a StringBody part to the multipart request. it will still come up in the post. you can add as much additional params as you wish like that and there are a couple different Body types you can add as parts.

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.