Adding Authorization Header to HTTP POST request in android(Using JSON) - android

My authorization header is "Authorization: TRUEREST username=user &password=pass&apikey=key&class=class". How to put it into HTTPPOST request..?
I am doing it like :
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setHeader("Authorization","TRUEREST");
httppost.setHeader("username","user");
httppost.setHeader("password","pass");
httppost.setHeader("apikey","key");
httppost.setHeader("class","class");
Credentials don't get send. What is wrong in this code??
Kindly help..!!
Thanks in advance.

First, are you sure you need to put all of those in the 'Authorization' header? Second what you are doing is adding 5 different headers with each value, not adding a single 'Authorization' header.
The '&' usually means you need to send those values as POST/GET parameters, but do check your specs.

Related

HTTP Request header issue in Android

I am trying to fire an API in which I need to add two headers i.e token and deviceid. Below is my code to add header:
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(nameValuePairs[0].get(0).getValue().toString());
nameValuePairs[0].remove(0);
ResponseHandler<String> res = new BasicResponseHandler();
// set header
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
httpPost.setHeader("deviceid", "358978060711939");
httpPost.setHeader("token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJHVUlEIjoiYjdlNjZiOWQtMjBjNy00MGI2LTliMzgtOTc3OGQ2OWIwM2E1IiwiRU5USVRZIjoiRUFCTSIsIlVOSVQiOiJVQk0yIiwiUk9MRU5BTUUiOiJTRUxGIiwiSUQiOiIwMDAyMzciLCJBTFRJRCI6IjMzMyIsIlVTRVJfTkFNRSI6IkFuaWwiLCJMT0dJTl9HVUlEX0tFWSI6ImU2NWM2YWEzLTZlNDItNDUyYS1hOGEwLWRlYzRhMGRiNTIxNyIsImlhdCI6MTQ1OTI0ODkyM30.m742d9xd6XlBBjZ3_ODWuoCEdWvSkhPAuNrDee1vi74");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs[0]));
response = httpClient.execute(httpPost, res);
In Response I am getting: org.apache.http.client.HttpResponseException: Not Found
When I replace token with any alpha numeric value then I am getting response with error message. But when I passed actual token value which is mentioned above I am getting exception. Even I tried with addHeader function also but getting same issue. I am unable to understand how to resolve this error.
it's the handler which is causing the error. Don't use.
You should either write your own handler or call execute without a handler.
httpClient.execute(httpPost);//remove the handler
I hope this is helpful. ThankYou
Your token seems to be incorrect. Header set in HTTP protocols has their own restrictions. To check it, just try to send your trim token (ex 30 chars).
If it don't helped, you have incorrect another parameters. Anyway, i would give you advice, to use Retrofit.

Android HttpClient issue setting Content-Length

I tried to create a post with the following...
HttpPost httppost = new HttpPost(URL);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httppost.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE);
String length = String.valueOf(httppost.getEntity().getContentLength());
httppost.setHeader(HTTP.CONTENT_LEN, length); //If commented out it works
but when I try to run the request I get the following error...
10-11 22:05:02.940: W/System.err(4203): org.apache.http.client.ClientProtocolException
I am guessing this is because the content length is wrong.
Apache HttpClient (even its fork shipped with Android) always calculates content length based on the properties of the enclosed HTTP entity. One does not need (and should not) set Content-Length and Transfer-Encoding headers manually.

HttpPost in android not working

I am trying to connect to apache tomcat server using HTTP POST, when i see LOG file of server it showing GET /login/validate_doc.jsp HTTP/1.1" 200 685 ,
which means it is getting a GET request when i am sending using HttpPost and form parameters are not received by server.
my code is below:
HttpPost post_http=null;
post_http=new HttpPost("http://somexx.ac.in/medONmob/validate_doc.jsp");
try
{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username",username));
nameValuePairs.add(new BasicNameValuePair("password",password));
post_http.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Where am i wrong ...??? Help me out please
Try specifying encoding when constructing UrlEncodedFormEntity. By default it is ISO-8859-1.
Also this will make your code future safe
Creating a UrlEncodedFormEntity from a List of NameValuePairs throws a NullPointerException
Given that you are using a post, then you probably are sending data on your request body, Am I right?, then you have to specify the content-type of the data you are sending in the headers, in order to execute a proper post:). For example if I am sending a json in the request body then I should add a header like this:
request.addHeader("content-type", "text/json");
Cheers

how to upload the datas to webserver from android mobile

how to upload the datas to webserver from android mobile.Please provide coding
I think this compiles:
HttpPut request = new HttpPut(<uri>);
request.setEntity(new ByteArrayEntity(<your data>));
HttpResponse response = HttpClient.execute(httpPut);
You might want to use the HttpPost instead of HttpPut and also specify the content type on the request.

Android: Example for using a cookie from HttpPost for HttpGet

I am able to use the example here: http://www.androidsnippets.org/snippets/36/index.html and successfully get the "HTTP/1.1 OK" response for a webesite I am sending the HttpPost along with the user credentials. However, I am unable to use an HttpGet to further browse other pages on this site.
Can anyone please let me know, what's going wrong. I am sorry - I am very new to Java.
My guess would be that when the website gets the Post and logs the user in, it sets cookies on the response to indicate that the user is logged in, and then requires those cookies on subsequent Get's.
You will need to do something like the following (this is borrowed from a bigger app so may not compile right out of the box)
DefaultHttpClient mHttpClient = new DefaultHttpClient();
BasicHttpContext mHttpContext = new BasicHttpContext();
CookieStore mCookieStore = new BasicCookieStore();
mHttpContext.setAttribute(ClientContext.COOKIE_STORE, mCookieStore);
This sets up a cookie store within the HTTP context, and you then use that context on Get's and Post's. For example...
HttpResponse response = mHttpClient.execute(mRequest, mHttpContext);
Under the covers the HTTP client will store cookies from responses, and add them to requests.

Categories

Resources