basic authentication with http post params android -


i doing basic authentication passing username , password , using basicnamevaluepair sending post params response service.

my method:

public stringbuilder callservicehttppost(string username, string password, string type)     {          // create new httpclient , post header         httpclient httpclient = new defaulthttpclient();         httppost httppost = new httppost(webservice + type);            httpresponse response = null;          stringbuilder total = new stringbuilder();          try {              url url = new url(webservice + type);              /*string base64encodedcredentials = base64.encodetostring((username                     + ":" + password).getbytes(), base64.url_safe                     | base64.no_wrap);*/              string base64encodedcredentials = "basic " + base64.encodetostring(                     (username + ":" + password).getbytes(),                     base64.no_wrap);               httppost.setheader("authorization", base64encodedcredentials);              // add data             list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(4);             namevaluepairs.add(new basicnamevaluepair("day", integer.tostring(2)));             namevaluepairs.add(new basicnamevaluepair("emailid", "usertest@gmail.com"));             namevaluepairs.add(new basicnamevaluepair("month", integer.tostring(5)));             namevaluepairs.add(new basicnamevaluepair("year", integer.tostring(2013)));              httppost.setentity(new urlencodedformentity(namevaluepairs));              // execute http post request             response = httpclient.execute(httppost);          } catch (clientprotocolexception e) {             // todo auto-generated catch block         } catch (ioexception e) {             // todo auto-generated catch block         }          httpentity entity = response.getentity();          if (entity != null) {             try {                 inputstream instream = entity.getcontent();                  string line = "";                   // wrap bufferedreader around inputstream                 bufferedreader rd = new bufferedreader(new inputstreamreader(instream));                  // read response until end                 while ((line = rd.readline()) != null) {                      total.append(line);                  }              } catch (illegalstateexception e) {                  e.printstacktrace();             } catch (ioexception e) {                  e.printstacktrace();             }         }           return total;     } 

but getting in total:

 <html><head>   <title>status page</title></head><body><h3>invalid json representation of content</h3><p>you can technical details <a href="http://www.w3.org/protocols/rfc2616/rfc2616-sec10.html#sec10.4.1">here</a>.<br>please continue visit @ our <a href="/">home page</a>.</p></body></html> 

this how it:

public string callservicehttppost(string username, string password, string type)     {          // create new httpclient , post header         httpclient httpclient = new defaulthttpclient();         httppost httppost = new httppost(webservice + type);              string responsebody = "";          httpresponse response = null;          try {              string base64encodedcredentials = "basic " + base64.encodetostring(                      (username + ":" + password).getbytes(),                      base64.no_wrap);               httppost.setheader("authorization", base64encodedcredentials);              httppost.setheader(http.content_type,"application/json");              jsonobject obj = new jsonobject();              obj.put("day", string.valueof(2));              obj.put("emailid", "usertest@gmail.com");             obj.put("month", string.valueof(5));             obj.put("year", string.valueof(2013));                httppost.setentity(new stringentity(obj.tostring(), "utf-8"));              // execute http post request             response = httpclient.execute(httppost);              if (response.getstatusline().getstatuscode() == 200) {                  log.d("response ok", "ok response :/");             } else {                 log.d("response not ok", "something went wrong :/");             }              responsebody = entityutils.tostring(response.getentity());          } catch (clientprotocolexception e) {             e.printstacktrace();         } catch (ioexception e) {             e.printstacktrace();         }         catch (jsonexception e) {             e.printstacktrace();         }          return responsebody;     } 

Comments

Popular posts from this blog

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -