0

What I want to do is to open an internet page in my browser (chrome) and get the html source code of the page just opened with my java application.

I don't want to get the source code of an url, I want a program that connects to the browser and gets the html code of the page that is open.

For example, if I open youtube in my browser, I want my application to get the current pages html code (in that case youtube code). Sorry if my english is not very good.

Zong
  • 6,160
  • 5
  • 32
  • 46

2 Answers2

1

You can do this:

import java.util.*;
public static void main(String[] args) {  
    Scanner input = new Scanner(System.in);
    URL url;
    InputStream is = null;
    BufferedReader br;
    String line;

    try {
        String urlInput = input.nextLine();
        url = new URL(urlInput);
        is = url.openStream();  // throws an IOException
        br = new BufferedReader(new InputStreamReader(is));

        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
    } catch (MalformedURLException mue) {
        mue.printStackTrace();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } finally {
        try {
            if (is != null) is.close();
        } catch (IOException ioe) {
            // nothing to see here
        }
    }
}

I got this from here: How do you Programmatically Download a Webpage in Java

Community
  • 1
  • 1
  • ok with this code i open an internet page with the program and download the source code of the url written in the console, but what i want to do is to open (by myself) an internet page, and download the code regardless of the url page, i want that my program connects to the browser window and download the html of the internet page opened in it – Tommaso Salvetti May 01 '16 at 18:29
0

Try this out:

You must pass in the URL as the argument and you'll have the HTML code

  public static void main(String[] args) throws IOException {
      URL u = null;
      try {
          u = new URL(args[0]);
      } catch (MalformedURLException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }

      BufferedReader in = new BufferedReader(new InputStreamReader(u.openStream()));
      String line = null;
      while((line = in.readLine()) != null){
          System.out.print(line);
      }
  }
malteser
  • 485
  • 2
  • 10
  • 27
  • ok with this code i download the source code of the url written in the console, but what i want to do is to open (by myself) an internet page, and download the code regardless of the url page, i want that my program connects to the browser window and download the html of the internet page opened in it – Tommaso Salvetti May 01 '16 at 18:30
  • so you want tohe URL to be sent through chrome, and have a program listening for a request and download the code of that request? I'll think about it a bit... – malteser May 02 '16 at 04:39
  • Check out this link! Maybe you can combine the above with it: [http://stackoverflow.com/questions/16675191/get-full-url-and-query-string-in-servlet-for-both-http-and-https-requests](http://stackoverflow.com/questions/16675191/get-full-url-and-query-string-in-servlet-for-both-http-and-https-requests) – malteser May 02 '16 at 04:49