FileDocCategorySizeDatePackage
HTTPGrab.javaAPI DocExample1395Tue Feb 14 08:17:50 GMT 2006None

HTTPGrab

public class HTTPGrab extends Object

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)


    if (args.length != 2) {
      System.err.println("Usage: java HTTPGrab url filename");
      return;
    }

    URL u = new URL(args[0]);
    if (!u.getProtocol().equalsIgnoreCase("http")) {
      System.err.println("Sorry, " + u.getProtocol()
        + " is not supported");
      return;
    }
    
    String host = u.getHost();
    int port    = u.getPort();
    String file = u.getFile();
    if (file == null) file = "/";
    if (port <= 0) port = 80;
    
    SocketAddress remote = new InetSocketAddress(host, port);
    SocketChannel channel = SocketChannel.open(remote);
    FileOutputStream out = new FileOutputStream(args[1]);
    FileChannel localFile = out.getChannel();
    
    String request = "GET " + file + " HTTP/1.1\r\n"
     + "User-Agent: HTTPGrab\r\n"
     + "Accept: text/*\r\n"
     + "Connection: close\r\n"
     + "Host: " + host + "\r\n"
     + "\r\n";
    
    ByteBuffer header = ByteBuffer.wrap(request.getBytes("US-ASCII"));
    channel.write(header);
    
    ByteBuffer buffer = ByteBuffer.allocate(8192);
    while (channel.read(buffer) != -1) {
      buffer.flip();
      localFile.write(buffer);
      buffer.clear();
    }
    
    localFile.close();
    channel.close();