35

Without the use of any external library, what is the simplest way to fetch a website's HTML content into a String?

1

7 Answers 7

48

I'm currently using this:

String content = null;
URLConnection connection = null;
try {
  connection =  new URL("http://www.google.com").openConnection();
  Scanner scanner = new Scanner(connection.getInputStream());
  scanner.useDelimiter("\\Z");
  content = scanner.next();
  scanner.close();
}catch ( Exception ex ) {
    ex.printStackTrace();
}
System.out.println(content);

But not sure if there's a better way.

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

4 Comments

Why "\\Z"? Isn't it an EOF on Windows only? I am just guessing here.
Why do you use "\\Z"? What does it do? I tried without it, it didn't work.
@MaxHusiv I think it's because if you don't specify a delimiter, scanner.next() will just go through the whole HTML character by character, but if you use a delimiter which won't be found in the HTML, scanner.next() returns the whole thing.
What import statements do you need for that to work?
23

This has worked well for me:

URL url = new URL(theURL);
InputStream is = url.openStream();
int ptr = 0;
StringBuffer buffer = new StringBuffer();
while ((ptr = is.read()) != -1) {
    buffer.append((char)ptr);
}

Not sure at to whether the other solution(s) provided are any more efficient or not.

5 Comments

Don't you need to include the following? import java.io.* import java.net.*
Sure, but they're core java so very simple. As for the actual code, the import statements are omitted for clarity.
after while, you should display the buffer's content too! or write a method where you read it!
be sure to close the inputstream
why have you named the variable ptr?
2

I just left this post in your other thread, though what you have above might work as well. I don't think either would be any easier than the other. The Apache packages can be accessed by just using import org.apache.commons.HttpClient at the top of your code.

Edit: Forgot the link ;)

1 Comment

Apparently you also have to install the JAR file :)
2

Whilst not vanilla-Java, I'll offer up a simpler solution. Use Groovy ;-)

String siteContent = new URL("http://www.google.com").text

Comments

1

Well it depends on what you're expecting to do with the fetched html string. If your goal is to do some kind of parsing or any kind of data extracting from the html content, why refrain yourself from using an external library?

Jsoup does the whole job very well without having to write a single regex yourself.

For example, to get the page title ( <head><title>this one</title>... ) you only need these few lines of code:

 String url = "https://www.example.com";
 Document document = Jsoup.connect(url).get();
 String title = document.title();

To use Jsoup you just have to add the dependency to your pom.xml file (make sure to pick the right version for the JDK your running):

    <dependency>
        <groupId>org.jsoup</groupId>
        <artifactId>jsoup</artifactId>
        <version>1.18.1</version>
    </dependency>

With the Jsoup document created on the second line of the above example, you can access any DOM element with css like selectors. For instance, this will print the URLs of every image in the page:

  document.select("img")
    .forEach(element -> System.out.println(element.attr("src")));

You can access the raw html string if you really need to: String rawHtml = document.html();

I am also often tempted not to use any external library, but I am very glad I did it for this one. Straight forward, simple to use and very comprehensive.

Comments

0
 try {
        URL u = new URL("https"+':'+'/'+'/'+"www.Samsung.com"+'/'+"in"+'/');
        URLConnection urlconnect = u.openConnection();
        InputStream stream = urlconnect.getInputStream();
        int i;
        while ((i = stream.read()) != -1) {
            System.out.print((char)i);
        }
    }
    catch (Exception e) {
        System.out.println(e);
    }

Comments

-4

Its not library but a tool named curl generally installed in most of the servers or you can easily install in ubuntu by

sudo apt install curl

Then fetch any html page and store it to your local file like an example

curl https://www.facebook.com/ > fb.html

You will get the home page html.You can run it in your browser as well.

1 Comment

Squints eyes to show shock. This is a Java Question.

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.