|
Network Programming This website demonstrates using wikis as teaching and learning tool. The course instructor is happy to share the teaching materials here with those who find it readable. |
Tutorial /
Tutorial - Java Network Programming - URL ConnectionOverview : This tutorial gives you some programs that use Java URL and URLConnection objects to do something interesting. Activity 1The URL framework is most convenient for dealing with HTTP and FTP resources. However, the URL and URLConnection classes can also be used to send emails. The following Java code can be used to send emails via the URL class. import java.io.*; import java.net.*; public class MailTo { public static void main(String[] args) { System.getProperties().put("mail.host", "smtp.ouhk.edu.hk"); try { URL url = new URL("mailto:sochoy@ouhk.edu.hk"); URLConnection conn = url.openConnection(); PrintStream out = new PrintStream(conn.getOutputStream(), true); out.print("From: findaway.hk@gmail.com"+"\r\n"); out.print("To: sochoy@ouhk.edu.hk"+"\r\n"); out.print("Subject: Works Great!"+"\r\n"); out.print( "Thanks for the example - it works great!"+"\r\n"); out.close(); System.out.println("Message Sent"); } catch (IOException e) { System.out.println("Send Failed"); e.printStackTrace(); } } }
Activity 2: Post data to HTML FormThe following simple program can be used to send comments to a blog site. import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.URL; import java.net.URLConnection; import java.util.Scanner; public class Post2WebTest { public static void main(String[] args) { String postUrl = ""; String postData = ""; try { URL url = new URL(postUrl); URLConnection conn = url.openConnection(); conn.setDoOutput(true); //conn.setRequestProperty("Referer", ""); // write POST data to server OutputStream output = conn.getOutputStream(); output.write(postData.getBytes()); output.close(); // read response from server BufferedReader reader = new BufferedReader( new InputStreamReader(conn.getInputStream())); String line; while ((line = reader.readLine()) != null) System.out.println(line); } catch (IOException ex) { ex.printStackTrace(); } } }
<h4>Post a comment</h4> <form action="comments.php" method="post" id="commentform"> <p><input type="text" name="author" id="author" value="" size="22" tabindex="1" /> <label for="author">← Name</label></p> <p><input type="text" name="email" id="email" value="" size="22" tabindex="2" /> <label for="email">← Email</label></p> <p><input type="text" name="url" id="url" value="" size="22" tabindex="3" /> <label for="url">← URL</label></p> <p><textarea name="comment" id="comment" cols="40" rows="10" tabindex="4"></textarea></p> <p><input name="submit" type="submit" id="submit" tabindex="5" value="Post comment" /></p> <input type="hidden" name="comment_post_ID" value="13" /> </p> </form> Activity 3: Set URL Request PropertiesThe following simple program can be used to validate a list of websites or not by auto visit them one by one. And you can custom agent text and refer links telling the webmaster "Hi, I visit you, and my link is here! import java.io.InputStream; import java.net.URL; import java.net.URLConnection; class IVisitYou { public static void main(String[] args) throws Exception { String target = ""; String source = ""; URL url = new URL(target); URLConnection urlc = url.openConnection(); urlc.setRequestProperty("User-Agent", "Mozilla 5.0 (Windows; U; " + "Windows NT 5.1; en-US; rv:1.8.0.11) "); urlc.setRequestProperty("Referer", source); InputStream is = urlc.getInputStream(); int c; while ((c = is.read()) != -1) System.out.print((char) c); } }
|