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 protected]&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?

2
  • What do you want to achieve? Just login to web site? Commented Jan 29, 2016 at 8: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 Commented Jan 29, 2016 at 13:58

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

7 Comments

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
where do you put the username and password for submit?
@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.
@Younes I know that it works fine. You can approve this answer as well.
hwo can i approve your answe??
|
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.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.