Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to send http request using http header

Tags:

android

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

like image 315
Kutbi Avatar asked Jun 18 '11 05:06

Kutbi


People also ask

How do I send a header in HTTP request?

To add custom headers to an HTTP request object, use the AddHeader() method. You can use this method multiple times to add multiple headers.

How do I send a HTTP request?

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.

What is HTTP explain HTTP header with example?

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.

How do I set up HTTP headers?

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.


1 Answers

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

like image 70
DArkO Avatar answered Sep 28 '22 02:09

DArkO