jsoup - Java send POST? -


i try send post data website. "login" not work. dont know why. there quite lot of ways login website without browser-ui. "best" method login here ? uses jsoup , "normal" httpurlconnection. selenium works fine, slow =(

import com.gargoylesoftware.htmlunit.util.cookie; import org.apache.commons.lang3.stringutils; import org.jsoup.*; import org.jsoup.nodes.document; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.io.outputstream; import java.net.*; import java.util.list; import java.util.map;  public class postget {          private static final string user_agent = "mozilla/5.0";          private static final string get_url = "https://de.metin2.gameforge.com/";          private static final string post_url = "https://de.metin2.gameforge.com:443/user/login?__token=";//+token          private static final string post_params = "username=username"+"password=password";          static final string cookies_header = "set-cookie";         public static void main(string[] args) throws ioexception {              string var = sendget();             string var1 =var.substring(0,var.indexof(";"));             string var2 =var.substring(var.indexof(";")+1,var.indexof(":"));             string token =var.substring(var.indexof(":")+1);             //sendpost(token,cookie);             jsoup(cookie(),token);         }         private static string sendget() throws ioexception {             url obj = new url(get_url);             httpurlconnection con = (httpurlconnection) obj.openconnection();             con.setrequestmethod("get");             con.setrequestproperty("user-agent", user_agent);             int responsecode = con.getresponsecode();             if (responsecode == httpurlconnection.http_ok) { // success                 bufferedreader in = new bufferedreader(new inputstreamreader(                         con.getinputstream()));                 string inputline;                 stringbuffer response = new stringbuffer();                  while ((inputline = in.readline()) != null) {                     response.append(inputline);                 }                 in.close();                 string veri1=response.substring(response.indexof("verify-v1")+20,response.indexof("verify-v1")+63);                 string veri2=response.substring(response.indexof("verify-v1")+104,response.indexof("verify-v1")+147);                 string token=response.substring(response.indexof("token")+6,response.indexof("token")+38);                 return (veri1+";"+veri2+":"+token);             } else {                 system.out.println("get request not worked");                 return " ";             }         }         private static string cookie() throws ioexception{             string realcookie="";             url obj = new url(get_url);             httpurlconnection connection = (httpurlconnection) obj.openconnection();              java.net.cookiemanager mscookiemanager = new java.net.cookiemanager();              map<string, list<string>> headerfields = connection.getheaderfields();             list<string> cookiesheader = headerfields.get(cookies_header);              if (cookiesheader != null) {                 (string cookie : cookiesheader) {                     mscookiemanager.getcookiestore().add(null,httpcookie.parse(cookie).get(0));                 }             }             list<httpcookie> cookiess = mscookiemanager.getcookiestore().getcookies();              if (cookiess != null) {                 if (cookiess.size() > 0) {                     (httpcookie cookie : cookiess) {                         realcookie = cookie.tostring().substring(cookie.tostring().indexof("=")+1);                         return(realcookie);                     }                 }             }             return(realcookie);         }         private static void sendpost(string token,cookiemanager mscookiemanager) throws ioexception {             url obj = new url(post_url+token);             httpurlconnection con = (httpurlconnection) obj.openconnection();             con.setrequestmethod("post");             con.setrequestproperty("user-agent", user_agent);              if (mscookiemanager != null) {                  list<httpcookie> cookies = mscookiemanager.getcookiestore().getcookies();                  if (cookies != null) {                     if (cookies.size() > 0) {                         (httpcookie cookie : cookies) {                             string realcookie = cookie.tostring().substring(cookie.tostring().indexof("=")+1);                         }                         con.setrequestproperty("cookie", stringutils.join(cookies, ";"));                     }                 }             }              // post - start             con.setdooutput(true);             outputstream os = con.getoutputstream();             os.write(post_params.getbytes());             os.flush();             os.close();             // post - end              int responsecode = con.getresponsecode();              if (responsecode == httpurlconnection.http_ok) { //success                 bufferedreader in = new bufferedreader(new inputstreamreader(                         con.getinputstream()));                 string inputline;                 stringbuffer response = new stringbuffer();                  while ((inputline = in.readline()) != null) {                     response.append(inputline);                 }                 in.close();                  // print result                 if(response.indexof("form-login")>0){                     system.out.println("nope");                 }                 if(response.indexof("logout")>0){                     system.out.println("yes");                 }              } else {                 system.out.println("post request not worked");             }         }         private static void jsoup(string cookie,string token) throws ioexception{            document response = jsoup.connect("https://de.metin2.gameforge.com/")                      .referrer("https://de.metin2.gameforge.com/main/index?__token=5"+token)                     .cookie("cookie","gf-locale=de_de; _ga=ga1.2.404625572.1501231885; __auc=a02de56115d8864ad2bd7ad8120; pc_idt=anu-cnegkmzu8casbqahiu1crlxeeud1ka6nvjxwqo4svvalisqsfpyzft8dxhkcrhrb_u2ffdqnd-2vsa377nnvgjkrkn-3qzi5q3lxbzgnbbmieir4zyncddpbjcug9cvpsu4gp-cvu53xhrq6_mwp9toyndjcqrvipw; sid="+cookie+"; __utma=96667401.404625572.1501231885.1503225538.1503232069.15; __utmc=96667401; __utmz=96667401.1501247623.3.2.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided)")                     //.data("x-devtools-emulate-network-conditions-client-id","c9bbb769-df80-47ff-8e25-e582de026ecc")                     .useragent("mozilla")                     .data("username", "username")                     .data("password", "password")                     .post();                 system.out.println(response);             }         }     } 

the var1 , var1 unnecessary. first idea send post.

here picture if login-form: , of httpclient login-form

enter image description here


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -