how to login into website using java code? -


i want write code login websites java.

here code :

package login;  import java.net.*; import java.io.*;  public class connecttourl {      // variables hold url object , connection url.     private static url urlobj;     private static urlconnection connect;      public static void main(string[] args) {         try {             cookiemanager cmanager = new cookiemanager();             cookiehandler.setdefault(cmanager);             // establish url , open connection it. set output mode.             urlobj = new url("https://accounts.google.com/servicelogin?service=mail&continue=https://mail.google.com/mail/#identifier");             connect = urlobj.openconnection();             connect.setdooutput(true);           }         catch (malformedurlexception ex) {             system.out.println("the url specified unable parsed or uses invalid protocol. please try again.");             system.exit(1);          }         catch (exception ex) {             system.out.println("an exception occurred. " + ex.getmessage());             system.exit(1);         }          try {             // create buffered writer urlconnection's output stream , write our forms parameters.             bufferedwriter writer = new bufferedwriter(new outputstreamwriter(connect.getoutputstream()));             writer.write("email=myemail@gmail.com&passwd=123456&submit=login");             writer.close();              // establish buffered reader read urlconnection's input stream.             bufferedreader reader = new bufferedreader(new inputstreamreader(connect.getinputstream()));              string lineread = "";              // read available lines of data url , print them screen.             while ((lineread = reader.readline()) != null) {                 system.out.println(lineread);             }              reader.close();         }         catch (exception ex) {             system.out.println("there error reading or writing url: " + ex.getmessage());         }     } } 

i have tried code on facebook , gmail problem didn't work.

it keep telling me cookies not enabled. (i have used chrome browser , enabled).

is there other ways achieve this?

if goal login web site. better solution use selenium web driver.

it has api creating modern drivers instances. , operate web elements.

code example:

import org.openqa.selenium.by; import org.openqa.selenium.webdriver; import org.openqa.selenium.webelement; import org.openqa.selenium.htmlunit.htmlunitdriver;

public class example  {     public static void main(string[] args) {         // create new instance of html unit driver         // notice remainder of code relies on interface,          // not implementation.         webdriver driver = new htmlunitdriver();          // , use visit google         driver.get("http://www.google.com");          // find text input element name         webelement element = driver.findelement(by.name("q"));          // enter search         element.sendkeys("cheese!");          // submit form. webdriver find form element         element.submit();          // check title of page         system.out.println("page title is: " + driver.gettitle());          driver.quit();     } } 

also has solution how manage cookies - cookies

just @ documentation how configure driver instances , manage web elements. preferred way use page object pattern.

update:

for getting location web page doesn't have id or name attributes can done using xpath expressions. useful can firefox extensions like:

and use concisely , short xpath functions.

for example:

<table>     <tr>         <td>             <p>some text here 1</p>         </td>     </tr>     <tr>         <td>             <p>some text here 2</p>         </td>     </tr>     <tr>         <td>             <p>some text here 3</p>         </td>     </tr> </table> 

for getting text some text here 2 able use following xpath:

//tr[2]/td/p

if know text static able use contains():

//p[contains(text(), 'some text here 2')]

for checking if xpath unique @ page best use console.
how described here how verify xpath expression


Comments