Cookies seem disabled (even though they're not!) with HttpURLConnection POST request to website on Android application -
my problem:
i have android application. i'm using httpurlconnection send post request login form on external website. website uses cookies logins , want check if login successful catching 'set-cookie' variable response header of post request. i'm doing class extends android's asynctask class.
the problem html response request states have cookies disables , naturally, don't set-cookie headers because of that.
desired result:
the printed response header fields should include 'set-cookie' header. html should either include table with:
error: 'login failed. both login name , password case sensitive, check caps lock not enabled."
or:
info: 'welcome! logged in'
depending on username , password given.
actual result:
post request successful, getting header. result of system.out.println:
printing response header... key : null ,value : [http/1.1 200 ok] key : connection ,value : [close] key : content-language ,value : [en] key : content-length ,value : [14233] key : content-type ,value : [text/html;charset=utf-8] key : date ,value : [fri, 10 jul 2015 15:32:17 gmt] key : expires ,value : [sat, 1 jan 2000 00:00:00 gmt] key : server ,value : [zope/(2.13.20, python 2.7.6, linux2) zserver/1.1] key : x-android-received-millis ,value : [1436542369028] key : x-android-response-source ,value : [network 200] key : x-android-sent-millis ,value : [1436542368849] key : x-ua-compatible ,value : [ie=edge,chrome=1] i see no set-cookie variable. unsurprising because in html response, find in 'error' table:
"cookies not enabled. must enable cookies sign in."
this site if manually disable cookies in laptop's webbrowser , try logging in there. can't seem fix.
code:
the asynctask class here:
import android.os.asynctask; import java.io.bufferedreader; import java.io.dataoutputstream; import java.io.inputstreamreader; import java.net.cookiehandler; import java.net.cookiemanager; import java.net.httpurlconnection; import java.net.url; import java.util.list; import java.util.map; public class tutorwebconnectiontask extends asynctask<string, string, void> { private final string user_agent = "mozilla/5.0 (linux; android 5.0.1; samsung gt-i9505 build/lrx22c) applewebkit/537.36 (khtml, gecko) samsungbrowser/2.1 chrome/34.0.1847.76 mobile safari/537.36"; httpurlconnection con; @override protected void doinbackground(string... strings) { try { string url = "http://tutor-web.net/login_form"; url obj = new url(url); httpurlconnection con = (httpurlconnection) obj.openconnection(); cookiehandler.setdefault(new cookiemanager()); //add reuqest header con.setrequestmethod("post"); con.setrequestproperty("user-agent", user_agent); con.setrequestproperty("accept-language", "en-us,en;q=0.5"); string urlparameters = "__ac_username=user&__ac_password=12345"; // send post request con.setdooutput(true); dataoutputstream wr = new dataoutputstream(con.getoutputstream()); wr.writebytes(urlparameters); wr.flush(); wr.close(); map<string, list<string>> map = con.getheaderfields(); system.out.println("printing response header...\n"); (map.entry<string, list<string>> entry : map.entryset()) { system.out.println("key : " + entry.getkey() + " ,value : " + entry.getvalue()); } int responsecode = con.getresponsecode(); system.out.println("\nsending 'post' request url : " + url); system.out.println("post parameters : " + urlparameters); system.out.println("response code : " + responsecode); bufferedreader in = new bufferedreader( new inputstreamreader(con.getinputstream())); string inputline; stringbuffer response = new stringbuffer(); while ((inputline = in.readline()) != null) { if(inputline.contains("enable cookies")) {system.out.println("cookies disabled -> " + inputline);} response.append(inputline); } in.close(); //print result system.out.println(response.tostring()); }catch(exception e) {e.printstacktrace();} return null; } } at press of button on android application, in mainactivity, do:
tutorwebconnectiontask task = new tutorwebconnectiontask(); task.execute(); various things i've tried doing fix problem:
- messing user_agent, keeping mozilla/5.0 or making now, whatsmyuseragent.com gives me when go site on phone. site tells me have cookies enabled!
- using cookiepolicy accept_all.
- making sure both 'accept cookies' , 'accept third party cookies' checked on phone's browsers (chrome , default android browser).
- using httpclient apache class work. didn't work, not when used httpcontext variable saw suggested in similar stackoverflow threads. stopped using httpclient because of thread telling me use httpurlconnection instead: android httpclient persistent cookies
- using cookiestore try , grab cookies through that.
- investigating tutor-web site doing when gives me "you need enable cookies" message. seems try create cookie , if fails, gives message.
- reading varies stackoverflow threads , trying solutions, no results.
none of these work. if has idea how fix this, please help.
the cookie needs set in application prior post, otherwise not seen until second request server.
Comments
Post a Comment