java - HttpURLConnection 403 response after 200 -


ok i'm working on app 2 activities.

let's call 1st 1 loginactivity appears on top of mainactivity. also, have class senderreceiver extends asynctask takes care of connection https server.

the 1st time call senderreceiver loginactivity, works fine & 200 response & i'm able use json retrieved. after that, loginactivity finishes & returns result mainactivity. when know i'm logged in & can proceed.

now whenever execute senderreceiver other information, 403 response java.io.filenotfoundexception: https://url_of_my_server. if call login again, works.

below senderreceiver code snippet connects server.

i'm connecting same server on ios version of app & never have issues subsequent calls after login.

now 1st time using https connections & such, i'm not sure how works on android must missing or doing wrong.

@override protected boolean doinbackground(object... params) {     frag = (fragment)params[0]; // later     addedparams = (list<namevaluepair>)params[1];     postpage = (string)params[2];      inputstream = null;      try {         url url = new url(constants.kwebservice);         httpurlconnection conn = (httpurlconnection) url.openconnection();          conn.setusecaches(false);         conn.setreadtimeout(15000);         conn.setconnecttimeout(20000);         conn.setrequestmethod("post");         conn.setdooutput(true);         //conn.setdoinput(true);          conn.setrequestproperty("content_type", "application/json");         conn.setrequestproperty("x-requested-with", "xmlhttprequest");          string base64encodedcredentials = "basic " + base64.encodetostring((constants.khttpsuser+":"+constants.khttpspass).getbytes(), base64.no_wrap);         conn.setrequestproperty("authorization", base64encodedcredentials);          string param = "page="+postpage+"&";         for(int = 0; < addedparams.size(); i++) {             string , = (i < addedparams.size()-1)? "&": "";             param += addedparams.get(i).getname()+"="+ urlencoder.encode(addedparams.get(i).getvalue(), "utf-8")+and;         }         conn.setfixedlengthstreamingmode(param.getbytes().length);          printwriter out = new printwriter(conn.getoutputstream());         out.print(param);         out.close();          // starts query         conn.connect();         int responsecode = conn.getresponsecode();         = conn.getinputstream();          // convert inputstream string         responsestr = constants.inputstreamtostring(is).tostring();         return true;         // makes sure inputstream closed after app         // finished using it.     } catch (protocolexception pe) {         constants.log("protocol exception:"+pe.getmessage());     } catch (ioexception io) {         constants.log("io exception:"+io.getmessage());         io.printstacktrace();     } {         if (is != null) {             try {                 is.close();             } catch (ioexception io) {                 constants.log("is.close io exception:"+io.getmessage());             }         }     }     return false; } 

fixed it!

all had set cookie header with:

conn.setrequestproperty("cookie", constants.kcookies); 

& retrieve cookie after response in order set with:

list<string> cookies = conn.getheaderfields().get("set-cookie"); if(cookies != null) {     constants.kcookies = cookies.get(0); } 

that way in initial httpurlconnection connections (i.e:login) cookie empty & not needed. once connection successful (after "login") receives cookie & sets it.

after that, subsequent connection sends cookie part of header.

the updated method below:

@override protected boolean doinbackground(object... params) {     frag = (fragment)params[0];     addedparams = (list<namevaluepair>)params[1];     postpage = (string)params[2];      inputstream = null;      try {         url url = new url(constants.kwebservice);         httpurlconnection conn = (httpurlconnection) url.openconnection();          conn.setusecaches(false);         conn.setreadtimeout(15000);         conn.setconnecttimeout(20000);         conn.setrequestmethod("post");         conn.setdooutput(true);         //conn.setdoinput(true);          conn.setrequestproperty("content_type", "application/json");         conn.setrequestproperty("x-requested-with", "xmlhttprequest");          string base64encodedcredentials = "basic " + base64.encodetostring((constants.khttpsuser+":"+constants.khttpspass).getbytes(), base64.no_wrap);         conn.setrequestproperty("authorization", base64encodedcredentials);          // cookie constants file & set it, constants.kcookies static string         conn.setrequestproperty("cookie", constants.kcookies);          string param = "page="+postpage+"&";         for(int = 0; < addedparams.size(); i++) {             string , = (i < addedparams.size()-1)? "&": "";             param += addedparams.get(i).getname()+"="+ urlencoder.encode(addedparams.get(i).getvalue(), "utf-8")+and;         }         conn.setfixedlengthstreamingmode(param.getbytes().length);          printwriter out = new printwriter(conn.getoutputstream());         out.print(param);         out.close();          // starts query         conn.connect();         int responsecode = conn.getresponsecode();         constants.log("the response code is: " + responsecode);         = conn.getinputstream();          // retrieve cookie response & if not null save constants.kcookies         list<string> cookies = conn.getheaderfields().get("set-cookie");         if(cookies != null) {             constants.kcookies = cookies.get(0);         }          // convert inputstream string         responsestr = constants.inputstreamtostring(is).tostring();//readit(is, len);         return true;         // makes sure inputstream closed after app         // finished using it.     } catch (protocolexception pe) {         constants.log("protocol exception:"+pe.getmessage());     } catch (ioexception io) {         constants.log("io exception:"+io.getmessage());         io.printstacktrace();     } {         if (is != null) {             try {                 is.close();             } catch (ioexception io) {                 constants.log("is.close io exception:"+io.getmessage());             }         }     }      return false; } 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -