/* * StringGrabber.java * * Example code. Use it as you will. * */ import java.io.*; import java.net.*; public class StringGrabber { public static void main(String[] args) { //do nothing - avoids compile error } //==================================================================== // In order to call this method from ASP we must make it public //==================================================================== public String GetString(String urlString, String preString, String postString) { // Get the web page from the url defined by urlString String webPageHTML; String DesiredString; try { webPageHTML = GetWebPageFromUrl(urlString); DesiredString = GetStringFromHTML(webPageHTML, preString, postString); } catch (GetStringException e) { return "GetString exception: " + e.getMessage(); } catch (Exception e1) { // Catch any other exception that might occur return "Other exception: " + e1.getMessage(); } return DesiredString; } //==================================================================== // Worker methods -- these can be private since we're not // calling them from ASP //==================================================================== //==================================================================== // GetWebPageFromUrl() //==================================================================== private String GetWebPageFromUrl(String urlString) throws GetStringException { // Create a URL object from urlString URL PageURL; try { PageURL = new URL(urlString); } catch (MalformedURLException e) { String msg = "Invalid url: " + urlString; throw new GetStringException(msg); } // Open a connection to the URL URLConnection PageConnection; try { PageConnection = PageURL.openConnection(); } catch (IOException e) { String msg = "Can't open connection to " + urlString; throw new GetStringException(msg); } // Get the InputStream from the URL connection InputStream webPageInputStream; try { webPageInputStream = PageConnection.getInputStream(); } catch (IOException e) { // Could be any server error, but the most likely is 404 String msg = "404 File Not Found: " + urlString; throw new GetStringException(msg); } // Read the web page via the InputStream StringBuffer webPageData = new StringBuffer(32000); int totalBytesRead = 0; boolean moreToRead = true; byte[] readBuf = new byte[4096]; // Read the web page in 4K chunks while (moreToRead) { int numBytesRead = 0; try { numBytesRead = webPageInputStream.read(readBuf); } catch (IOException e) { moreToRead = false; numBytesRead = -1; } if (numBytesRead > 0) { totalBytesRead += numBytesRead; webPageData.append(new String(readBuf, 0, numBytesRead)); } else moreToRead = false; } try { webPageInputStream.close(); } catch (IOException e) { // Ignore any exception that might occur } webPageData.setLength(totalBytesRead); return webPageData.toString(); } //==================================================================== // GetStringFromHTML() //==================================================================== private String GetStringFromHTML(String webPageHTML, String preString, String postString) throws GetStringException { // Look for the preString int preStringLoc = webPageHTML.indexOf(preString); if (preStringLoc == -1) { String msg = "Couldn't find the preString " + preString; throw new GetStringException(msg); } // Found the preString, so look for the postString int postStringLoc = webPageHTML.indexOf(postString, preStringLoc); if (postStringLoc == -1) { String msg = "Couldn't find the postString " + postString; throw new GetStringException(msg); } // The stock price is between the preString and postString String DesiredString = webPageHTML.substring(preStringLoc + preString.length(), postStringLoc); return DesiredString; } } //==================================================================== // // class GetStringException // // Convenience class to make it easy to return errors from our // worker methods. This let's us separate StockGrabber errors // from coding errors (what, coding errors?!). // //==================================================================== class GetStringException extends Exception { public GetStringException (String msg) { super(msg); } }