/* * This program connects to a web server and gets infor from it. * It retrives the root page "/" if no other specifc page is given, * or it retrives the specified 'dir' * page.
* * Apr-1998 X. Meng
* sep-03-2002 X. Meng replace the DataInputStream with BufferedReader
* nov-12-2002 X. Meng consider the cases where the content-length is not * returned by the server.
* * @author Xiannong Meng * Date: September 7, 2004
*/
import java.io.*;
import java.net.*;
import java.util.*;
class PostClient {
// public final static int PORT = 8808; // default web port
public final static int PORT = 80; // default web port
public static void main(String[] args) throws IOException {
Date d;
long start, end;
if (args.length != 1 && args.length != 2) {
System.out.println(" usage: host [dir]");
System.exit(1);
}
Socket t = new Socket(args[0], PORT); // create a socket
String cmd;
cmd = new String("POST / HTTP/1.0\n");
// construct the rest of the command
cmd += "Host: " + args[0].trim() + "\n";
cmd += "Connection: Keep-Alive\n";
// cmd += "User-Agent: self-module\n";
cmd += "UserAgent: self-module\n";
cmd += "Accept: text/html\n";
cmd += "Accept: image/gif\n";
cmd += "Accept: image/x-xbitmap\n";
cmd += "Accept: image/jpeg\n";
cmd += "\r\n\r\n";
// echo to local screen
System.out.println("sending command :");
System.out.println(cmd);
// connecting to socket input and output
BufferedReader is = new BufferedReader
(new InputStreamReader
(new DataInputStream(t.getInputStream())));
PrintStream out = new PrintStream(t.getOutputStream());
// actually send the command
out.print(cmd);
out.flush();
int count = 0;
String inBuffer = new String();
boolean more = true;
// read the header portion of the response from the server
inBuffer = readHttpHeader(is);
System.out.println("header : " + inBuffer);
String content = "";
Hashtable
*
* @param fileIn a pointer to the input buffered reader
* @author Xianonng Meng
*
*/
public static String readHttpHeader(BufferedReader fileIn)
{
String inBuffer = "";
try
{
String line;
do
{
line = fileIn.readLine();
inBuffer += line + "\r\n";
// System.out.print("in readAll -> " + line + "<-");
if (line.length() == 2 && line.compareTo("\n\n") == 0)
{
System.out.println("we hit the break in readAll");
break;
}
} while (line.length() > 0);
}
catch (IOException ignored) {};
// System.out.println("\n end of readAll -> " + inBuffer + "<-");
return inBuffer;
}
}