#include #include #include #include /* constants */ const int MSGLEN = 256; /* query length */ const int maxLen = 65536; /* content length (web page length) */ const int HTTPDPORT = 80; /* default httpd port */ /* function prototypes */ int socketClient(char *,int); void requestPage(int,char *); int main(int argc, char *argv[]) { int s; /* socket descriptors */ char *myname; /* pointer to name of this program */ char *host; /* pointer to remote host name */ int port; /* port number */ int id; /* host id */ char *fname; /* file name */ char *content; int len, totalLen = 0; content = new char[maxLen]; myname = argv[0]; /* it should be in the form or */ switch (argc) { case 2 : host = argv[1]; fname = new char[MSGLEN]; strcpy(fname,"index.html"); break; case 3 : host = argv[1]; fname = argv[2]; break; default :cerr << " Usage: " << myname << " site (e.g. www.cs.panam.edu)\n"; cerr << " or " << myname << " site file (e.g. www.cs.panam.edu /index.html)\n"; exit(1); } s = socketClient(host,HTTPDPORT); requestPage(s,fname); while ((len = read(s, &(content[totalLen]), maxLen)) > 0) totalLen += len; content[totalLen] = 0; printf("%s\n",content); close(s); } void requestPage(int s, char *fname) { char buf[BUFSIZ + 1]; /* buffer to read the page */ int len; /* length of received data */ strcpy(buf,"GET /"); /* the protocol specifies that the command */ /* should be GET /file HTTP/version\n\n */ strcat(buf,fname); strcat(buf," HTTP/1.0\n\n"); /* the following request gets the main search page */ /* strcpy(buf,"POST /search HTTP/1.0\nHost: www.excite.com\nFrom: meng@panam.edu\nContent-Type: text/plain\nContent-Length: 0\n\n\n"); */ /* the following request gets the main search page */ /* strcpy(buf,"GET /search HTTP/1.0\nHost: www.excite.com\nAccept: text/*\nFrom: meng@panam.edu\n\n"); */ /* the following will get nothing from excite.com which is netscape server, */ /* it will get the appropriate information from an apache server */ /* strcpy(buf,"OPTIONS /search HTTP/1.1\nHost: www.excite.com\n\n"); */ /* gets relocation */ /* strcpy(buf,"GET /redir.dcg HTTP/1.0\nHost: search.excite.com\nAccept: text/*\nFrom: meng@panam.edu\n\n"); */ /* often gets "Request-URI Too Long"!! */ /* strcpy(buf,"GET / HTTP/1.0\nHost: search.excite.com\nAccept: text/*\nFrom: meng@panam.edu\n\n"); */ /* often gets "Request-URI Too Long"!! */ /* strcpy(buf,"HEAD / HTTP/1.0\n\n"); */ len = strlen(buf); if (write(s,buf, len) != len) { perror("write"); exit(1); } } // end requestPage