httpsurlconnection - How do the Java classes CookieHandler and CookieManager work? -
the way can code programmatically login url using cookiehandler.setdefault(new cookiemanager);
. fine, want understand how cookie being maintained between each new httpsurlconnection
.
can please show how make code below log gmail account without having use cookiehandler.setdefault(new cookiemanager);
? thanks.
**notes:
- substitute own email , password.
-cookiehandler.setdefault(new cookiemanager);
commented out in code below.
public class gmailapp { private list<string> cookies; private httpsurlconnection conn; public static void main(string[] args) throws exception { string url = "https://accounts.google.com/serviceloginauth"; string gmail = "https://mail.google.com/mail/"; gmailapp http = new gmailapp(); // cookiehandler.setdefault(new cookiemanager()); string page = http.getpagecontent(url); string postparams = http.getformparams(page, "myemail@gmail.com", "mypassword"); http.sendpost(url, postparams); string result = http.getpagecontent(gmail); system.out.println(result); } private void sendpost(string url, string postparams) throws exception { url obj = new url(url); conn = (httpsurlconnection) obj.openconnection(); conn.setdooutput(true); dataoutputstream wr = new dataoutputstream(conn.getoutputstream()); wr.writebytes(postparams); wr.flush(); wr.close(); int responsecode = conn.getresponsecode(); system.out.println("\nsending 'post' request url : " + url); system.out.println("post parameters : " + postparams); system.out.println("response code : " + responsecode); bufferedreader in = new bufferedreader(new inputstreamreader(conn.getinputstream())); string inputline; stringbuffer response = new stringbuffer(); while ((inputline = in.readline()) != null) { response.append(inputline); } in.close(); } private string getpagecontent(string url) throws exception { url obj = new url(url); conn = (httpsurlconnection) obj.openconnection(); conn.setrequestmethod("get"); conn.setusecaches(false); if (cookies != null) { (string cookie : this.cookies) { conn.addrequestproperty("cookie", cookie.split(";", 1)[0]); } } int responsecode = conn.getresponsecode(); system.out.println("\nsending 'get' request url : " + url); system.out.println("response code : " + responsecode); bufferedreader in = new bufferedreader(new inputstreamreader(conn.getinputstream())); string inputline; stringbuffer response = new stringbuffer(); while ((inputline = in.readline()) != null) { response.append(inputline); } in.close(); setcookies(conn.getheaderfields().get("set-cookie")); return response.tostring(); } public string getformparams(string html, string username, string password) throws unsupportedencodingexception { system.out.println("extracting form's data..."); document doc = jsoup.parse(html); element loginform = doc.getelementbyid("gaia_loginform"); elements inputelements = loginform.getelementsbytag("input"); list<string> paramlist = new arraylist<string>(); (element inputelement : inputelements) { string key = inputelement.attr("name"); string value = inputelement.attr("value"); if (key.equals("email")) value = username; else if (key.equals("passwd")) value = password; paramlist.add(key + "=" + urlencoder.encode(value, "utf-8")); } stringbuilder result = new stringbuilder(); (string param : paramlist) { if (result.length() == 0) { result.append(param); } else { result.append("&" + param); } } return result.tostring(); } public void setcookies(list<string> cookies) { this.cookies = cookies; } }
when set default cookiehandler
statically via java.net.cookiehandler.setdefault()
, current , futute httpurlconnection
s (via underlying http client classes) check existence of default cookie handler, , finding use retrieve cookies requests, , store new cookies set/returned. provides static cookie-jar long want maintain cookies across requests. sure set desired cookiepolicy
, or java.net.cookiepolicy.accept_original_server
default.
note there's few issues code:
cookie.split(";", 1)[0]
this split won't anything, want cookie.split(";", 2)[0]
(1->2) if want truncate string first semicolon onwards.
also, calling setcookies
after requests - need capture cookies in response post login.
Comments
Post a Comment