4

I want to write code for login to websites with java.

Here is the code :

package login;

import java.net.*;
import java.io.*;

public class ConnectToURL {

    // Variables to hold the URL object and its connection to that URL.
    private static URL URLObj;
    private static URLConnection connect;

    public static void main(String[] args) {
        try {
            CookieManager cManager = new CookieManager();
            CookieHandler.setDefault(cManager);
            // Establish a URL and open a connection to it. Set it to 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 was unable to be parsed or uses an invalid protocol. Please try again.");
            System.exit(1); 
        }
        catch (Exception ex) {
            System.out.println("An exception occurred. " + ex.getMessage());
            System.exit(1);
        }

        try {
            // Create a buffered writer to the URLConnection's output stream and write our forms parameters.
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connect.getOutputStream()));
            writer.write("Email=myemail@gmail.Com&Passwd=123456&submit=Login");
            writer.close();

            // Now establish a buffered reader to read the URLConnection's input stream.
            BufferedReader reader = new BufferedReader(new InputStreamReader(connect.getInputStream()));

            String lineRead = "";

            // Read all available lines of data from the URL and print them to screen.
            while ((lineRead = reader.readLine()) != null) {
                System.out.println(lineRead);
            }

            reader.close();
        }
        catch (Exception ex) {
            System.out.println("There was an error reading or writing to the URL: " + ex.getMessage());
        }
    }
}

I have tried this code on Facebook and Gmail but the problem is that it didn't work.

It keep telling me that the cookies is not enabled. (I have used chrome browser and they were enabled).

Is there any other ways to achieve this?

catch23
  • 17,519
  • 42
  • 144
  • 217
Ys3
  • 79
  • 1
  • 2
  • 7
  • What do you want to achieve? Just login to web site? – catch23 Jan 29 '16 at 08:51
  • i actually want to do is create a program with java . that allow me to connect to website using my account and get my information . i mean by that the websites that they don't have api like gamezer for example – Ys3 Jan 29 '16 at 13:58

2 Answers2

3

If your goal is just login to some web site, much better solution is to use Selenium Web Driver.

It has API for creating modern drivers instances, and operate with their 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 a new instance of the html unit driver
        // Notice that the remainder of the code relies on the interface, 
        // not the implementation.
        WebDriver driver = new HtmlUnitDriver();

        // And now use this to visit Google
        driver.get("http://www.google.com");

        // Find the text input element by its name
        WebElement element = driver.findElement(By.name("q"));

        // Enter something to search for
        element.sendKeys("Cheese!");

        // Now submit the form. WebDriver will find the form for us from the element
        element.submit();

        // Check the title of the page
        System.out.println("Page title is: " + driver.getTitle());

        driver.quit();
    }
}

Also it has solution how to manage cookies as well - Cookies

Just look at documentation how to configure driver instances and manage web elements, preferred way is to use Page Object pattern.

Update:

For getting location from web page which doesn't have id or name attributes can be done using xpath expressions, very useful for this can be firefox extensions like:

And use concisely and 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 you able to use following xpath:

//tr[2]/td/p

if you know that text is static you able to use contains():

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

For checking if your xpath is unique at this page the best is to use console.
How to do is described here How to verify an XPath expression

Ebraheem Alrabeea
  • 2,130
  • 3
  • 23
  • 42
catch23
  • 17,519
  • 42
  • 144
  • 217
  • what i wont to do is create program with java that allow me to connect to website using my account , and get my information . those websites they don't give us api . like gamezer for example – Ys3 Jan 29 '16 at 09:31
  • where do you put the username and password for submit? – Ys3 Jan 29 '16 at 09:40
  • @Younes Instead of finding search fileld - `driver.findElement(By.name("q"));` you should find on web page login input & password input. Sent appropriate info to them with `sendKeys()` => and click login button. – catch23 Jan 29 '16 at 15:57
  • @Younes I know that it works fine. You can approve this answer as well. – catch23 Jan 30 '16 at 09:25
  • hwo can i approve your answe?? – Ys3 Jan 30 '16 at 16:59
  • men i have last question if you allow me :) i'm using java ++ selenium i have this code for example

    some text here

    some text here

    some text here

    and i need to get the secound tag p . how can i do that?? the text in p tag is not static .
    – Ys3 Jan 30 '16 at 17:00
  • @Younes I updated answer. Here is more info about accepting and voting [How does accepting an answer work?](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – catch23 Jan 30 '16 at 18:00
0

What exactly are you trying to do with this? You are almost certainly better off using something like Selenium web-driver for browser automation tasks, as you piggy back on the work of an existing web-browser to handle things like cookies.

In this case, you're talking about your web browser saying cookies are not enabled, but you're not actually using a web browser, you're sending a connection via your java application.

Will
  • 810
  • 6
  • 21
  • yes my bad . i was using chrome in my code . but i deleted , and i forgot to say that what i actually want to do is create a program with java . that allow me to connect to website using my account and get my information . i mean by that the websites that they don't have api like gamezer for example – Ys3 Jan 29 '16 at 09:29