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
To add custom headers to an HTTP request object, use the AddHeader() method. You can use this method multiple times to add multiple headers.
The most common HTTP request methods have a call shortcut (such as http. get and http. post), but you can make any type of HTTP request by setting the call field to http. request and specifying the type of request using the method field.
An HTTP header is a field of an HTTP request or response that passes additional context and metadata about the request or response. For example, a request message can use headers to indicate it's preferred media formats, while a response can use header to indicate the media format of the returned body.
In the web site pane, double-click HTTP Response Headers in the IIS section. In the actions pane, select Add. In the Name box, type the custom HTTP header name. In the Value box, type the custom HTTP header value.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With