Send a post login from java code and get cookie authentication -
i need login iis web-server.
after login, need send http post lot of data.
however, don't know how save cookies login , reuse them subsequent http calls.
my code
private final static logger logger = logger.getlogger(httpurlconnectionexample.class); private static final string certificate_client = "/src/main/resources/clientcert.jks"; private static final string javax_net_ssl_key_store_password = "javax.net.ssl.keystorepassword"; private static final string javax_net_ssl_key_store = "javax.net.ssl.keystore"; private static propertiesmanagement management; private static final string login_page = "https://alloggiatiweb.poliziadistato.it/alloggiatiweb/login.aspx"; private static final string cookies_header = "set-cookie"; private static cookiemanager mscookiemanager = new cookiemanager(); private static final string internal_page = "https://alloggiatiweb.poliziadistato.it/alloggiatiweb/analisi.aspx"; private httpsurlconnection conn; private static void setcertificatessl() throws filenotfoundexception, keystoreexception, ioexception, nosuchalgorithmexception, certificateexception { fileinputstream fin = new fileinputstream(management.getfilepath()); keystore ks = keystore.getinstance(management.getsslprotocol()); ks.load(fin, management.getpassword().tochararray()); system.setproperty(javax_net_ssl_key_store, certificate_client); system.setproperty(javax_net_ssl_key_store_password, management.getpassword()); } public static void main(string[] args) throws exception { management = new propertiesmanagement(); setcertificatessl(); httpurlconnectionexample http = new httpurlconnectionexample(); http.login(); } private void getinternalpage() throws exception { url obj = new url(internal_page); conn = (httpsurlconnection) obj.openconnection(); conn.setreadtimeout(10000); conn.setconnecttimeout(15000); conn.setrequestmethod("get"); conn.setdoinput(true); conn.connect(); logger.info("[get on " + obj + " response code-message]" + conn.getresponsecode() + " - " + conn.getresponsemessage()); bufferedreader in = new bufferedreader(new inputstreamreader(conn.getinputstream())); string inputline; stringbuffer response = new stringbuffer(); while ((inputline = in.readline()) != null) { response.append(inputline); } in.close(); logger.info(response.tostring()); } private void login() throws exception { url obj = new url(login_page); conn = (httpsurlconnection) obj.openconnection(); conn.setreadtimeout(10000); conn.setconnecttimeout(15000); conn.setrequestmethod("post"); conn.setdooutput(true); list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("txtutente", management.getusername())); params.add(new basicnamevaluepair("txtpwd", management.getpassword())); outputstream os = conn.getoutputstream(); bufferedwriter writer = new bufferedwriter(new outputstreamwriter(os, "utf-8")); writer.write(getquery(params)); writer.flush(); writer.close(); os.close(); conn.connect(); map<string, list<string>> headerfields = conn.getheaderfields(); list<string> cookiesheader = headerfields.get(cookies_header); if (cookiesheader != null) { (string cookie : cookiesheader) { mscookiemanager.getcookiestore().add(null, httpcookie.parse(cookie).get(0)); } } logger.info("[post on " + obj + " response code-message]" + conn.getresponsecode() + " - " + conn.getresponsemessage()); getinternalpage(); conn.disconnect(); } private string getquery(list<namevaluepair> params) throws unsupportedencodingexception { stringbuilder result = new stringbuilder(); boolean first = true; (namevaluepair pair : params) { if (first) first = false; else result.append("&"); result.append(urlencoder.encode(pair.getname(), "utf-8")); result.append("="); result.append(urlencoder.encode(pair.getvalue(), "utf-8")); } return result.tostring(); }
and response
info (httpurlconnectionexample.java:125) [19 apr 2016 15:08:51,013] - [post on https://alloggiatiweb.poliziadistato.it/alloggiatiweb/login.aspx response code-message]200 - ok info (httpurlconnectionexample.java:83) [19 apr 2016 15:08:51,461] - [get on https://alloggiatiweb.poliziadistato.it/alloggiatiweb/analisi.aspx response code-message]200 - ok info (httpurlconnectionexample.java:94) [19 apr 2016 15:08:51,464] - <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transit .....
but page print aren't internal page it's login page
Comments
Post a Comment