Kutbi
Kutbi

Reputation: 1154

How to set header using http post method

i am currently working on one application.. in that application i have to use get and post both methods.. get method works properly but in post method suddenly i get the response like

   "invalid method.post required"..

my code for that is:

     String list_url = "************************";
    try {

        String appand = TimeStampForAuth.TimeStameMethod(main.this);
        String auth = android.util.Base64.encodeToString(("iphone" + ":" + 
            "2a5a262d5a")
        .getBytes("UTF-8"), android.util.Base64.NO_WRAP);

        DefaultHttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(list_url+appand);

        Log.d("URL_LIST", list_url+appand);

        List nvps = new ArrayList();
        nvps.add(new BasicNameValuePair("login",Login));
        nvps.add(new BasicNameValuePair("password",password));
        nvps.add(new BasicNameValuePair("title",title));
        nvps.add(new BasicNameValuePair("category_id",cat_id));




        UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps,HTTP.UTF_8);
        post.addHeader("Authorization", "Basic " + auth);
        post.setEntity(p_entity);
        HttpResponse response = client.execute(post);
        HttpEntity responseEntity = response.getEntity();
        String s = EntityUtils.toString(responseEntity);
        Log.d("List response", s);

    }
    catch(Exception e){
        System.out.println(e.toString());
    }

in that i am missing somethinf or what that i dont know..is all that thing is valid for post method....

please help me as early as possible...

thanking you.........

Upvotes: 5

Views: 18081

Answers (3)

Rotemmiz
Rotemmiz

Reputation: 7983

use this instead:

   HttpURLConnection connection = (HttpsURLConnection) serviceURL.openConnection();
   connection.setRequestMethod("POST");

Upvotes: -1

dirkboye
dirkboye

Reputation: 156

You could try HttpClient 4.1 for Android: http://code.google.com/p/httpclientandroidlib/ (There are bugs in the HttpClient version 4.0 which Android uses.) You can also debug with it a little more with httpClient.log.enableDebug(true);

If you don't want to use an external library, I'd start debugging the network traffic. With a software called Wireshark you can see and inspect all Http Requests/Responses very easily.

For authorization I personally would use:

httpClient.getCredentialsProvider().setCredentials(
  new AuthScope(hostname, port), 
  new UsernamePasswordCredentials(user, pass));

Upvotes: 1

Jayp
Jayp

Reputation: 822

Try using setHeader() instead of addHeader()

Upvotes: 1

Related Questions