I have written a c socket programe which can send the http GET request.But it dont work for HEAD reuest.can anyone help me.I am connected to internet via a proxy and the port/ip in the programe are proxies ones
--------------------------------------------------
#include <stdlib.h>
#include <stdio.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
/* Print the contents of the home page for the server's socket.
Return an indication of success. */
void get_home_page (int socket_fd)
{
char buffer[10000];
ssize_t number_characters_read;
/* Send the HTTP GET command for the home page. */
sprintf (buffer, "GET http://www.google.lk\n");
write (socket_fd, buffer, strlen (buffer));
/* Read from the socket. read may not return all the data at one
time, so keep trying until we run out. */
while (1) {
number_characters_read = read (socket_fd, buffer, 10000);
if (number_characters_read == 0)
return;
/* Write the data to standard output. */
fwrite (buffer, sizeof (char), number_characters_read, stdout);
}
}
int main (int argc, char* const argv[])
{
int socket_fd;
struct sockaddr_in name;
struct hostent *hostinfo;
/* Create the socket. */
socket_fd = socket (AF_INET, SOCK_STREAM, 0);
/* Store the server's name in the socket address. */
name.sin_family = AF_INET;
/* Convert from strings to numbers.this is proxy ip address */
hostinfo = gethostbyname (argv[1]);
if (hostinfo == NULL)
return 1;
else
name.sin_addr.s_addr = ((struct in_addr *)(hostinfo->h_addr))->s_addr;
/* Web servers use port 3128 which is the port we connect to proxy. */
name.sin_port = htons (3128);
/* Connect to the web server */
if (connect (socket_fd,(struct sockaddr *)&name,sizeof (name)) == -1) {
perror ("connect");
return 1;
}
/* Retrieve the server's home page. */
get_home_page (socket_fd);
return 0;
}