Recent Changes - Search:

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 - Java Network Programming - URL Connection

Overview : This tutorial gives you some programs that use Java URL and URLConnection objects to do something interesting.


Activity 1

The 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();
    }
  }
}
  • Note: Though it is easier to program than using the Socket class to send emails, it is not very useful. It is because the URL class does not support SMTP authentication, which is required by most, if not all, SMTP servers on the Web nowadays.
  • Task: Compile and test the program so that it can send email to you.

Activity 2: Post data to HTML Form

The 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();
        }
    }
}
  • Task: modify the program so that it can be used to automatically post comments to a blog (the actual target will be given in class for testing purpose).
  • You need to understand the working of HTML form and the POST method of HTTP. The following is an example code of a HTML form.
<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">&larr; Name</label></p>
<p><input type="text" name="email" id="email" value="" size="22" tabindex="2" />
<label for="email">&larr; Email</label></p>
<p><input type="text" name="url" id="url" value="" size="22" tabindex="3" />
<label for="url">&larr; 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 Properties

The 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);
  }
}
  • Task: Modify the program so that it can be used to visit a list of websites.

Edit - History - Print - Recent Changes - Search
Page last modified on November 24, 2009, at 08:54 AM