Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

httpHEAD

Part of StandardLibraryCategory

Description

Demonstrates Socket and SocketStream.

Example

/*
 * Title: httpHEAD
 * Author: michal wallace (http://withoutane.com/)
 * License: public domain
 * Purpose: demonstrate sockets in D
 *
 * This program makes  an HTTP HEAD
 * request to a server specified on the
 * command line and prints the response.
 *
 */

private import std.socket;
private import std.socketstream;
private import std.string;


const int WEBPORT=80;

void main(char[][] args){ 

  if (args.length != 3) {
    printf("usage: httpHEAD host path");
    return(1);
  }

  char[] host = args[1];
  char[] path = args[2];

  // open a socket:
  InternetHost ih = new InternetHost;
  ih.getHostByName(host);
  InternetAddress ia = new InternetAddress(ih.addrList[0], WEBPORT);
  TcpSocket sock = new TcpSocket();
  sock.connect(ia);

  // send the HTTP request
  sock.send("HEAD " ~ path ~ " HTTP/1.0\n\n");
        
  // read and print the result:
  char[] line;
  SocketStream stream = new SocketStream(sock);
  while (! stream.eof()) {
    line = stream.readLine();
    if (line=="") break;
    printf ("%s\n", toStringz(line));
  }
}

Source

Link http://www.dsource.org/tutorials/index.php?show_example=95
Posted by Anonymous
Date/Time Sat May 29, 2004 5:20 pm