Sending Information to a Remote Site 

  • Just as it is easy to receive data from a server, it is also easy to send data to a server, or more likely, to a CGI script on a server. 
  • In this case, we will use an output and print stream instead of an input stream as in the following example. 
  • try
      {
      Socket s = new Socket("www.extropia.com", 
                             80);
      OutputStream os = s.getOutputStream());
      PrintStream ps = new PrintStream(os);
      ps.println("POST /cgi-bin/test.cgi");
      ps.println("Content-type: plain/text");
      String string = "Hello CGI Script!";
      ps.println("Content-length: " + 
                 string.length + "\n");
      ps.println(string);
      }
    catch (IOException e)
      {
      System.out.println("Error: " + e);
      }
  • Of course, you could simply use the GET method by passing the data as URL encoded information by replacing the first println line above with something more like: 

  • ps.println("GET /cgi-bin/test.cgi?phrase=Hello+CGI+Script"); 

Additional Resources:

Getting Information from a Remote Site 
Table of Contents
Threads


Graphics & Media Lab. >> Библиотека | Курсы | Графикон

Hosted by Graphics & Media Lab
http://graphics.cs.msu.su
lab_logo
mailto: Laboratory