Reputation: 964
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);
}
Upvotes: 0
Views: 4241
Reputation: 387
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" );
Upvotes: 0
Reputation: 26809
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");
Upvotes: 3
Reputation: 340723
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.
Upvotes: 5