cookies - authentication with Java and apache HttpClient 4.5.1 -
my problem is, don't get, how log in java , apache httpcomponents (httpclient v4.5.1) specific site: site im trying log in. have username (test_admin) , password (testing) log in think not enough , need more. think has field security_token see when make request uri, dont know how keep or how save , afterwards. there hidden input field name login-ticket, dont know what's either. want login, because need see courses , add new ones. after trying several code implementations im stick code:
public static void setget(closeablehttpclient httpclient) throws unsupportedoperationexception, ioexception { httpget httpget = new httpget("http://demo.studip.de/dispatch.php/admin/courses"); closeablehttpresponse httpresponse = httpclient.execute(httpget); system.out.println("get response status:: " + httpresponse.getstatusline().getstatuscode()); showentity(httpresponse,httpresponse.getentity()); } public static httpentity setparam(int count, string[] params, string[] values) { list<namevaluepair> formparams = new arraylist<namevaluepair>(); (int = 0; < count; i++) { formparams.add(new basicnamevaluepair(params[i],values[i])); system.out.println("paramater------------------> "+params[i]+" values-------------> "+values[i]); } urlencodedformentity entity = new urlencodedformentity(formparams, consts.utf_8); return entity; } public static void setpost(httpclient httpc) throws clientprotocolexception, ioexception { httppost httppost = new httppost("http://demo.studip.de/dispatch.php/admin/courses"); //string[] params = {"loginname", "password"}; //string[] values = {"test_admin", "testing"}; //httpentity entity = setparam(2, params, values ); httpresponse response = httpc.execute(httppost); system.out.println("post response status:: " + response.getstatusline().getstatuscode()); showentity(response, response.getentity()); } public static void showentity(httpresponse httpresp, httpentity httpclient) throws ioexception { httpclient = httpresp.getentity(); if (httpclient != null) httpclient = new bufferedhttpentity(httpclient); system.out.print(entityutils.tostring(httpclient)); } public static void main(string[] args) throws interruptedexception, ioexception { credentialsprovider credentialsprovider = new basiccredentialsprovider(); credentialsprovider.setcredentials(authscope.any, new usernamepasswordcredentials("test_admin", "testing")); closeablehttpclient hc = httpclientbuilder.create().setdefaultcredentialsprovider(credentialsprovider).build(); setget(hc); // httpclient httpclient = httpclients.createdefault(); setpost(hc); setget(hc); }
the problem ist everytime same answer server see login page in response, server asks me login username , password.
which code server 401,403,301,302 or 200?
Comments
Post a Comment