i have web server, log in in android application, after loging recive xml user logged field named token.
this token used keep open session during next calls webservice, , works sendidnt token cookie named "acrsession" seems not working because everytime tried check if im logged in (using call named currentuser) returns me forbidden, think isnt working good.
here asynctask class calls server.
public string getfilename() { return filename; } public void setfilename(string filename) { filename = filename; } private string response; private uri uriinfo; private string filename; public webservicetask(int tasktype, context mcontext, string processmessage,string token) { this.tasktype = tasktype; this.mcontext = mcontext; this.processmessage = processmessage; this.token=token; } public void addnamevaluepair(string name, string value) { params.add(new basicnamevaluepair(name, value)); } public void showprogressdialog() { pdlg = new progressdialog(mcontext); pdlg.setmessage(processmessage); pdlg.setprogressdrawable(mcontext.getwallpaper()); pdlg.setprogressstyle(progressdialog.style_spinner); pdlg.setcancelable(false); pdlg.show(); } @override protected void onpreexecute() { //hidekeyboard(); showprogressdialog(); } protected string doinbackground(string... urls) { string url = urls[0]; string result = ""; httpresponse response = doresponse(url); if (response == null) { return result; } else { try { result = inputstreamtostring(response.getentity().getcontent()); } catch (illegalstateexception e) { log.e(tag, e.getlocalizedmessage(), e); } catch (ioexception e) { log.e(tag, e.getlocalizedmessage(), e); } } return result; } @override protected void onpostexecute(string response) { this.response=response; pdlg.dismiss(); } // establish connection , socket (data retrieval) timeouts private httpparams gethttpparams() { httpparams htpp = new basichttpparams(); httpconnectionparams.setconnectiontimeout(htpp, conn_timeout); httpconnectionparams.setsotimeout(htpp, socket_timeout); return htpp; } private httpresponse doresponse(string url) { // use our connection , data timeouts parameters our // defaulthttpclient httpclient httpclient = new defaulthttpclient(gethttpparams()); int responsecode=0; // create local instance of cookie store //cookiestore cookiestore = new basiccookiestore(); // create local http context //httpcontext localcontext = new basichttpcontext(); // bind custom cookie store local context //localcontext.setattribute(clientcontext.cookie_store, cookiestore); //cookiemanager cookiemanager= cookiemanager.getinstance(); this.getlocalcontext(); this.cookiestore.addcookie(new basicclientcookie("acrsession", this.token)); httpresponse response = null; try { switch (tasktype) { case post_task: httppost httppost = new httppost(url); // add parameters httppost.setentity(new urlencodedformentity(params)); int executecount = 0; { pdlg.setmessage("logging in.. ("+(executecount+1)+"/5)"); // execute http post request executecount++; response = httpclient.execute(httppost,localcontext); responsecode = response.getstatusline().getstatuscode(); // if want see response code, can log // out here calling: // log.d("256 design", "statuscode: " + responsecode) } while (executecount < 5 && responsecode == 408); uriinfo = httppost.geturi(); break; case get_task: httpget httpget = new httpget(url); response = httpclient.execute(httpget,localcontext); responsecode = response.getstatusline().getstatuscode(); httpget.getrequestline(); uriinfo = httpget.geturi(); break; case put_task: httpput httpput = new httpput(url); file file = new file(this.filename); inputstreamentity reqentity = new inputstreamentity(new fileinputstream(file), -1); reqentity.setcontenttype("binary/octet-stream"); reqentity.setchunked(true); // send in multiple parts if needed httpput.setentity(reqentity); response = httpclient.execute(httpput,localcontext); responsecode = response.getstatusline().getstatuscode(); httpput.getrequestline(); uriinfo = httpput.geturi(); break; } } catch (exception e) { log.e(tag, e.getlocalizedmessage(), e); } return response; } private string inputstreamtostring(inputstream is) { string line = ""; stringbuilder total = new stringbuilder(); // wrap bufferedreader around inputstream bufferedreader rd = new bufferedreader(new inputstreamreader(is)); try { // read response until end while ((line = rd.readline()) != null) { total.append(line); } } catch (ioexception e) { log.e(tag, e.getlocalizedmessage(), e); } // return full string this.response=total.tostring(); return total.tostring(); } public string getresponse(){ return this.response; } public httpcontext getlocalcontext() { if (localcontext == null) { localcontext = new basichttpcontext(); cookiestore = new basiccookiestore(); localcontext.setattribute(clientcontext.cookie_origin, cookiestore); localcontext.setattribute(clientcontext.cookie_spec, cookiestore); localcontext.setattribute(clientcontext.cookiespec_registry, cookiestore); localcontext.setattribute(clientcontext.cookie_store, cookiestore);// make sure cookies provided server can reused } return localcontext; } plesae tell me im doing bad.
thanks in advance.
well, found solution, ok fortgot set cookie domain , path, onced putted it worked.
now cookie creation looks this:
this.localcontext=this.getlocalcontext(); basicclientcookie cookie = new basicclientcookie("acrsession", this.token); cookie.setdomain(this.domain); cookie.setpath(this.path); this.cookiestore.addcookie(cookie); localcontext.setattribute(clientcontext.cookie_store, this.cookiestore); hope else.
Comments
Post a Comment