How to use java to fill in a web page and pull back the results?
Hey I am relatively new to java, and I am trying to make an application that does the following:
- Sends a request to a live website
- Retrieves the data of that page
For example, assume the following site displays game results, where 'game=500' shows the results for game number 324 of 500 different games. http://www.some-site.com/results.php?game=324
I would like to use a Java program to automatically cycle through the game=1 to game=500, posting to the website and retrieving the results of the page.
What is the best way to do this? Can anyone giv开发者_开发知识库e me a simple example? If I knew the correct java 'key words', I would google for some tutorials on this concept.
Note: the target-page in question is php
URL url;
InputStream is = null;
DataInputStream dis;
String line;
for(int i=1;i<=500;i++){
try {
url = new URL("http://www.some-site.com/results.php?game="+i);
is = url.openStream(); // throws an IOException
dis = new DataInputStream(new BufferedInputStream(is));
while ((line = dis.readLine()) != null) {
//do sth with the datea
}
} catch (MalformedURLException mue) {
mue.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
is.close();
} catch (IOException ioe) {
// nothing to see here
}
}
}
Do something like the answer in this other stackoverflow page
and then you want to use a for loop for loop through pages 1 through 500.
Apache has some really good Java libraries for accessing HTTP. See this for more details.
精彩评论