UrlRetriever2.java Illustrates how the URL class can simplify communication to an HTTP server.
import java.net.*;
import java.io.*;
/** Read a remote file using the standard URL class
* instead of connecting explicitly to the HTTP server.
*
* Taken from Core Web Programming from
* Prentice Hall and Sun Microsystems Press,
* .
* © 2001 Marty Hall and Larry Brown;
* may be freely used or adapted.
*/
public class UrlRetriever2 {
public static void main(String[] args) {
checkUsage(args);
try {
URL url = new URL(args[0]);
BufferedReader in = new BufferedReader(
new InputStreamReader(url.openStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println("> " + line);
}
in.close();
} catch(MalformedURLException mue) { // URL constructor
System.out.println(args[0] + "is an invalid URL: " + mue);
} catch(IOException ioe) { // Stream constructors
System.out.println("IOException: " + ioe);
}
}
private static void checkUsage(String[] args) {
if (args.length != 1) {
System.out.println("Usage: UrlRetriever2 ");
System.exit(-1);
}
}
}
Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017 From: http://sitestree.com/?p=10232
Categories:Programming Code Examples, Java/J2EE/J2ME, Network Programming
Tags:Java/J2EE/J2MENetwork Programming
Post Data:2017-01-02 16:04:28
Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada
