WebPageUtils.java [src/utils] Revision: default Date:
package utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.SocketException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
/**
* Last Updated: 20-November-2018
* @author Tyler Wible
* @since 20-November-2018
*/
public class WebPageUtils {
/**
* Opens a web connection to USGS and returns the contents of a REST/url search for the specified url
* @param searchUrl a formatted url for requested USGS data
* @return an ArrayList containing the results of the web service search for flow data using the above inputs
* @throws IOException
*/
public static ArrayList<String> downloadWebpage(String searchUrl) throws IOException {
//Open the provided website
URL webpage = new URL(searchUrl);
URLConnection yc = webpage.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
//Read out all of the webpage out into an ArrayList<String>
ArrayList<String> pageData = new ArrayList<>();
String inputLine;
while((inputLine = in.readLine()) != null){
pageData.add(inputLine);
}
in.close();
return pageData;
}
/**
* Opens a web connection to the specified URL and returns the contents of a REST/url search for the specified url
* @param searchUrl a formatted url for requested data
* @return an ArrayList containing the results of the web service search for flow data using the above inputs
* @throws IOException
*/
public static ArrayList<String> downloadWebpage_slowData(String searchUrl) throws IOException {
//Open the provided website
URL webpage = new URL(searchUrl);
URLConnection yc = webpage.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
//Read out all of the webpage out into an ArrayList<String>
ArrayList<String> pageData = new ArrayList<>();
boolean moreLines = true;
while(moreLines){
try{
String inputLine = in.readLine();
if(inputLine != null){
pageData.add(inputLine);
}else{
moreLines = false;
}
}catch(SocketException e){
//There was an issue with the webpage, so quit out
moreLines = false;
}
}
in.close();
return pageData;
}
}