Example of C Programming for HTTP protocol

Source: Internet
Author: User

Keywords: HTTP client rfc2616 protocol download everyone is very familiar with HTTP protocol applications, because every day on the network browsing a lot of things, it is also known that the HTTP protocol is quite simple. Every time you use a download software such as flashget to download a webpage, it is amazing to use the "Download all links with flashget.
Later I thought about it. In fact, it is not difficult to implement these download functions. I only need to send a request according to the HTTP protocol and then analyze the received data, if there are links such as href pointing to the mark on the page, you can download the link at a deeper level. Currently, the most popular HTTP Protocol version is 1.1. to fully understand it, see rfc2616.
The following is an HTTP download program programmed in C language. The source code is as follows:

/******* HTTP client program httpclient. c ************/
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <sys/types. h>
# Include <sys/socket. h>
# Include <errno. h>
# Include <unistd. h>
# Include <netinet/in. h>
# Include <limits. h>
# Include <netdb. h>
# Include <ARPA/inet. h>
# Include <ctype. h>

/// // Httpclient. start with C ////////////////////////////////////// /////

/*************************************** *****
Function: search for the first matching character from the right of the string.
**************************************** ****/
Char * rstrchr (char * s, char X ){
Int I = strlen (s );
If (! (* S) return 0;
While (s [I-1]) if (strchr (S + (I-1), x) Return (S + (I-1); else I --;
Return 0;
}

/*************************************** *****
Function: converts a string to lowercase letters.
**************************************** ****/
Void tolowercase (char * s ){
While (* s) * s = tolower (* s ++ );
}

/*************************************** ***********************
Function: analyzes the website address and port from the SRC string and obtains the file to be downloaded.
**************************************** ***********************/
Void gethost (char * SRC, char * web, char * file, int * port ){
Char * pA;
Char * pb;
Memset (Web, 0, sizeof (Web ));
Memset (file, 0, sizeof (File ));
* Port = 0;
If (! (* SRC) return;
Pa = SRC;
If (! Strncmp (Pa, "http: //", strlen ("http: //") pA = SRC + strlen ("http ://");
Else if (! Strncmp (Pa, "https: //", strlen ("https: //") pA = SRC + strlen ("https ://");
PB = strchr (Pa ,'/');
If (PB ){
Memcpy (Web, Pa, strlen (PA)-strlen (PB ));
If (Pb + 1 ){
Memcpy (file, Pb + 1, strlen (Pb)-1 );
File [strlen (Pb)-1] = 0;
}
}
Else memcpy (Web, Pa, strlen (PA ));
If (PB) Web [strlen (PA)-strlen (PB)] = 0;
Else web [strlen (PA)] = 0;
Pa = strchr (Web ,':');
If (PA) * Port = atoi (PA + 1 );
Else * Port = 80;
}

/*************************************** ******************************
* Filename: httpclient. c
* Purpose: an HTTP client program that can be used to download webpages.
* Wrote by: zhoulifa (zhoulifa@163.com) Zhou Lifa (http://zhoulifa.bokee.com)
Linux enthusiasts Linux knowledge disseminators sohowhose developers are best at C Language
* Date Time: 2006-03-11 21: 4 array: 00
* Note: Anyone can copy and use the code at will, including your commercial use.
* But follow the GPL
**************************************** *****************************/
Int main (INT argc, char * argv [])
{
Int sockfd;
Char buffer [1024];
Struct sockaddr_in server_addr;
Struct hostent * Host;
Int portnumber, nbytes;
Char host_addr [256];
Char host_file [1024];
Char local_file [256];
File * FP;
Char request [1024];
Int send, totalsend;
Int I;
Char * PT;

If (argc! = 2)
{
Fprintf (stderr, "Usage: % s web-address \ A \ n", argv [0]);
Exit (1 );
}
Printf ("parameter.1 is: % s \ n", argv [1]);
Tolowercase (argv [1]);/* convert the parameter to all lowercase letters */
Printf ("lowercase parameter.1 is: % s \ n", argv [1]);

Gethost (argv [1], host_addr, host_file, & portnumber);/* analyze the URL, port, and file name */
Printf ("webhost: % s \ n", host_addr );
Printf ("hostfile: % s \ n", host_file );
Printf ("portnumber: % d \ n", portnumber );

If (host = gethostbyname (host_addr) = NULL)/* obtain the Host IP Address */
{
Fprintf (stderr, "gethostname error, % s \ n", strerror (errno ));
Exit (1 );
}

/* The customer program starts to establish the sockfd descriptor */
If (sockfd = socket (af_inet, sock_stream, 0) =-1)/* establish a socket connection */
{
Fprintf (stderr, "socket error: % s \ A \ n", strerror (errno ));
Exit (1 );
}

/* Fill in the information of the client program on the server */
Bzero (& server_addr, sizeof (server_addr ));
Server_addr.sin_family = af_inet;
Server_addr.sin_port = htons (portnumber );
Server_addr.sin_addr = * (struct in_addr *) Host-> h_addr );

/* The client program initiates a connection request */
If (connect (sockfd, (struct sockaddr *) (& server_addr), sizeof (struct sockaddr) =-1)/* connect to the website */
{
Fprintf (stderr, "Connect error: % s \ A \ n", strerror (errno ));
Exit (1 );
}

Sprintf (request, "Get/% s HTTP/1.1 \ r \ naccept: */* \ r \ naccept-language: ZH-CN \ r \ n \
User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0) \ r \ n \
HOST: % s: % d \ r \ nconnection: Close \ r \ n ", host_file, host_addr, portnumber );
Printf ("% s", request);/* prepare the request and send it to the Host */

/* Get the actual file name */
If (host_file & * host_file) Pt = rstrchr (host_file ,'/');
Else Pt = 0;

Memset (local_file, 0, sizeof (local_file ));
If (PT & * PT ){
If (Pt + 1) & * (Pt + 1) strcpy (local_file, Pt + 1 );
Else memcpy (local_file, host_file, strlen (host_file)-1 );
}
Else if (host_file & * host_file) strcpy (local_file, host_file );
Else strcpy (local_file, "index.html ");
Printf ("local filename to write: % s \ n", local_file );

/* Send an HTTP request */
Send = 0; totalsend = 0;
Nbytes = strlen (request );
While (totalsend <nbytes ){
Send = write (sockfd, request + totalsend, nbytes-totalsend );
If (send =-1) {printf ("send error! % S \ n ", strerror (errno); exit (0 );}
Totalsend + = Send;
Printf ("% d bytes send OK! \ N ", totalsend );
}

Fp = fopen (local_file, "");
If (! FP ){
Printf ("Create File error! % S \ n ", strerror (errno ));
Return 0;
}
Printf ("\ nthe following is the response header: \ n ");
I = 0;
/* The connection is successful, and the HTTP response is received, response */
While (nbytes = read (sockfd, buffer, 1) = 1)
{
If (I <4 ){
If (buffer [0] = '\ R' | buffer [0] =' \ n') I ++;
Else I = 0;
Printf ("% C", buffer [0]);/* print the HTTP header information on the screen */
}
Else {
Fwrite (buffer, 1, 1, FP);/* write HTTP subject information to the file */
I ++;
If (I % 1024 = 0) fflush (FP);/* Save the disk once every 1 K */
}
}
Fclose (FP );
/* End communication */
Close (sockfd );
Exit (0 );
}
/// // Httpclient. C end ////////////////////////////////////// /////

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.