Use C language to implement a simple HTTP client)

Source: Internet
Author: User

Implement a simple HTTP client in C Language)

Author: gobitan (rain) Date: Reprint please indicate the source http://blog.csdn.net/gobitan HTTP protocol is a text-based protocol, so using C language to achieve a simple HTTP client is not difficult. However, if you are not familiar with this, it may not take two or three minutes to implement a simple http get method to retrieve a webpage. The key is to understand the working principle of the HTTP protocol, see rfc2616 specification (http://www.ietf.org/rfc/rfc2616.txt); this article uses only 10 or 20 lines of code to achieve a simple HTTP client, it can obtain and print the 163 homepage. The entire source code is as follows (httpclient. c). Note that the following program is streamlined, and many parameters are directly written into the program for demonstration purposes only. Among them, the 163 server address is obtained through ping www.163.com, which may be changed. Be flexible during testing. You only need to modify "strcat (sndbuf," Host: www.163.com/n/r/n ");" and "inet_addr (" 202.108.9.51 ");" to get the pages of other addresses. # Include <stdio. h> # include "winsock2.h" # pragma comment (Lib, "ws2_32.lib") int main () {socket ssocket = invalid_socket; sockaddr_in stsvraddrin = {0 }; /* server address */Char sndbuf [1024] = {0}; char rcvbuf [2048] = {0}; char * prcv = rcvbuf; int num = 0; int nret = socket_error; wsadata;/* Start of HTTP message construction, which is the key of the Program */sprintf (sndbuf, "Get/HTTP/1.1/N "); strcat (sndbuf, "Host: www.163.com/n/r/n ");/* HTTP message construction ends * // * socket DLL initialization */wsastartup (makeword (2, 0), & wsadata); stsvraddrin. sin_family = af_inet; stsvraddrin. sin_port = htons (80); stsvraddrin. protocol = inet_addr ("202.108.9.51"); ssocket = socket (af_inet, sock_stream, ipproto_tcp);/* connection */nret = connect (ssocket, (sockaddr *) & stsvraddrin, sizeof (sockaddr); If (socket_error = nret) {printf ("Connect fail! /N "); Return-1;}/* Send HTTP Request Message */send (ssocket, (char *) sndbuf, sizeof (sndbuf), 0 ); /* receive HTTP Response Message */while (1) {num = Recv (ssocket, prcv, 2048, 0); prcv + = num; If (0 = num) | (-1 = num) {break ;}/ * print Response Message */printf ("% s/n", rcvbuf); Return 0 ;} the most important part of this program is to build an http get message. The HTTP message format is described in Section 4.1 of the HTTP protocol specification. It contains a starting line, zero or multiple message headers, then a blank line (CRLF), and finally an optional message body. The HTTP message constructed in the demo contains a request line (get/HTTP/1.1) and a message header (HOST: www.163.com ). The following two lines of code: sprintf (sndbuf, "Get/HTTP/1.1/N"); strcat (sndbuf, "Host: www.163.com/n/r/n"); get is the method for obtaining HTTP, the subsequent '/' indicates obtaining the default page under the root directory. "HTTP/1.1" indicates the protocol and version. Note that "/N" is necessary. The following line is the message header, indicating the obtained host. Pay attention to "/n/R/N" next to it. This must not be an error. Empty line CRLF is represented by "/R/N", which takes a lot of time and is helpful to everyone. This program is compiled and run in the Visual C ++ 6.0 environment. Directly execute the program and print the following results: HTTP/1.0 200 okdate: Tue, 03 Apr 2007 15:40:35 gmtserver: Apache/2.0.55 (UNIX) Accept-ranges: bytesvary: accept-encodingcontent-length: 115440content-type: text/html; charset = gb2312age: 176x-cache: hit from www.163.comconnection: Close <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml" XML: lang = "ZH-CN" lang = "ZH-CN">

 

Author: Hu Jiahui, net name: Yu currently focuses on XML document management and related technologies

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.