I am using Java to get a chunk of HTML from a web page. Right now I am using a URLConnection with getInputStream() which is loading the whole page and taking a little longer than I would like. Is there anyway for it to load just the chunk i need or to exclude images or anything else that could speed it up. Any help is appreciated. Thank you.
Here is some code:
URL page = new URL("http://www.stackoverflow.com");
URLConnection connection = page.openConnection();
String html = getResponseData(connection);
public static String getResponseData(URLConncetion connection) {
StringBuffer sb = new StringBuffer();
InputStream is = connection.getInputStream();
int count;
while((count=is.read()) != -1){
sb.append((char)count);
}
I think you could try to find the actual data in that while loop, and abort as soon as you have found it.
Side note, your code will only load the HTML. Not the real images. They are not part of the response you get when requesting the page.
UPDATE: You could also buffer your inputstream. It could make the input faster. You can do this as follows