java - Connect to Website via Cookie (setRequestProperty) -
i want write java application, logs website , navigates through different sites through cookie authentication. have managed log website , retrieve it's code. first problem:
while looping through headerfieldkeys there no "set-cookie" key, there only: date, server, x-powered-by, expires, last-modified, cache-control, cache-control, pragma, x-ua-compatible, x-frame-options, strict-transport-security, vary, connection, transfer-encoding , content-type.
as can see in code tried code retrieve cookies, worked. cookies want navigate link of website, won't work.
package myedks; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.io.printstream; import java.net.cookiehandler; import java.net.cookiemanager; import java.net.cookiepolicy; import java.net.cookiestore; import java.net.httpcookie; import java.net.url; import java.net.urlconnection; import java.util.list; public class spielhelfer { public static httpcookie cookiew; public static string cookiev; @suppresswarnings("deprecation") public static void main(string[] args) { try { // instantiate cookiemanager; // make sure set cookiepolicy cookiemanager manager = new cookiemanager(); manager.setcookiepolicy(cookiepolicy.accept_all); cookiehandler.setdefault(manager); // content urlconnection; // cookies set web site url url = new url("https://www.w-da.net/wws/109580.php?sid=41853050108060779043427902790460"); urlconnection con = url.openconnection(); // activate output con.setdooutput(true); printstream ps = new printstream(con.getoutputstream()); // send parameters site ps.print("login_login=********"); ps.print("&login_password=********"); // have input stream in order send request con.getinputstream(); // cookies underlying // cookiestore cookiestore cookiejar = manager.getcookiestore(); list <httpcookie> cookies = cookiejar.getcookies(); (httpcookie cookie: cookies) { system.out.println("cookiehandler retrieved cookie: " + cookie); cookiew = cookie; string cookiec = cookie.getvalue(); int x = cookiec.length(); cookiev = cookiec; system.out.println("**************************************"); } } catch(exception e) { system.out.println("unable cookie using cookiehandler"); e.printstacktrace(); } system.out.println(cookiew); try { url url2 = new url("https://www.w-da.net/wws/109580.php?sid=41853050108060779043427902790460"); urlconnection con2 = url2.openconnection(); con2.setrequestproperty("cookie", cookiev); con2.connect(); bufferedreader in2 = new bufferedreader(new inputstreamreader(con2.getinputstream())); string line2 = null; while ((line2 = in2.readline()) != null) { system.out.println(line2); } } catch(exception e) { system.out.println("unable set cookie using cookiehandler"); e.printstacktrace(); } } }
i not in java, recommend try use selenium webdriver. because navigate on websites in more higher way. contain nice api needed function. because write in way long , hard.
Comments
Post a Comment