简体   繁体   中英

What is the fastest way to get a HTML Content using java?

I have this, but I was wondering if there is a faster way:

        URL url=new URL(page);
        InputStream is = new BufferedInputStream(url.openConnection().getInputStream());
        BufferedReader in=new BufferedReader(new InputStreamReader(is));
        String tmp="";
        StringBuilder sb=new StringBuilder();
        while((tmp=in.readLine())!=null){
            sb.append(tmp);
        }

Probably network is the biggest overhead, there isn't much you can do on Java code side. But using IOUtils is at least much faster to implement:

String page = IOUtils.toString(url.openConnection().getInputStream());

Remember to close underlying stream.

if you need manipulating with your html, find some library. Like for example jsoup .

jsoup is a Java library for working with real-world HTML. It provides a very convenient API for extracting and manipulating data, using the best of DOM, CSS, and jquery-like methods.

Example:

Document doc = Jsoup.connect("http://en.wikipedia.org/").get();
Elements newsHeadlines = doc.select("#mp-itn b a");

If you're using Apache Commons IO's IOUtils as Tomasz suggests, there's an even simpler method: toString(URL) , or its preferred cousins that take a charset (of course that requires knowing the resource's charset in advance).

String string = IOUtils.toString( new URL( "http://some.url" ));

or

String string = IOUtils.toString( new URL( "http://some.url" ), "US-ASCII" );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM