Linux下发送HTTP协议请求

      在Linux系统中用C语言实现的HTTP协议的POST和GET请求,下面是代码实现,如果要测试需要搭建个后台服务器的环境,

作者测试用的是PHP开发环境,具体搭建可参看另一篇文章:http://blog.csdn.net/hanbo622/article/details/51598648

demo.c

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
int main(int argc, char *argv[])
{
	unsigned short port = 80;           // 服务器的端口号
	char *server_ip = "192.168.2.203";  // 服务器ip地址

	int sockfd = socket(AF_INET, SOCK_STREAM, 0);// 创建TCP套接字
	if(sockfd < 0)
	{
		perror("socket");
		exit(-1);
	}
	
	struct sockaddr_in server_addr; //定义服务器信息结构体
	bzero(&server_addr,sizeof(server_addr)); 
	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(port);
	inet_pton(AF_INET, server_ip, &server_addr.sin_addr);
	
	int err_log = connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));   // 主动连接服务器
	if(err_log != 0)
	{
		perror("connect");
		close(sockfd);
		exit(-1);
	}
	
	char str1[1024] = "";
	char data[]="username=hanbo&num=123"; //POST 数据
#if 1    //POST 格式请求
	sprintf(str1, "%s\r\n","POST http://192.168.2.203/index.php HTTP/1.1"); //http://192.168.2.203/index.php 服务端接收数据处理文件地址
	sprintf(str1, "%s%s\r\n",str1,"Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/msword, application/vnd.ms-powerpoint, application/vnd.ms-excel, */*");
	sprintf(str1, "%s%s\r\n",str1,"Referer: */*");
	sprintf(str1, "%s%s\r\n",str1,"Accept-Language: en-US,zh-CN;q=0.5");
	sprintf(str1, "%s%s\r\n",str1,"User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; qdesk 2.4.1265.203; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; InfoPath.3)");
	sprintf(str1, "%s%s\r\n",str1,"Content-Type: application/x-www-form-urlencoded");
	sprintf(str1, "%s%s\r\n",str1,"Accept-Encoding: gzip, deflate");
	sprintf(str1, "%s%s\r\n",str1,"Host: 192.168.2.203"); //服务器地址
	sprintf(str1, "%s%s%d\r\n",str1,"Content-Length: ",strlen(data)); //数据长度
	sprintf(str1, "%s%s\r\n",str1,"Connection: Keep-Alive");
	sprintf(str1,"%s%s\r\n",str1,"Cache-Control: no-cache");
	sprintf(str1, "%s\r\n",str1); //多加个回车
	sprintf(str1, "%s%s\r\n",str1,data);
#else	  //GET 格式请求
	sprintf(str1, "%s\r\n","GET http://192.168.2.203/index.php?username=hanbo&num=123 HTTP/1.1"); //服务端接收数据处理文件地址,并带参数
	sprintf(str1, "%s%s\r\n",str1,"Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/msword, application/vnd.ms-powerpoint, application/vnd.ms-excel, */*");
	sprintf(str1, "%s%s\r\n",str1,"Accept-Language: en-US,zh-CN;q=0.5");
	sprintf(str1, "%s%s\r\n",str1,"User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; qdesk 2.4.1265.203; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; InfoPath.3)");
	sprintf(str1, "%s%s\r\n",str1,"Accept-Encoding: gzip, deflate");
	sprintf(str1, "%s%s\r\n",str1,"Host: 192.168.2.203"); //服务器地址
	sprintf(str1, "%s%s\r\n",str1,"Connection: Keep-Alive");
	sprintf(str1,"%s%s\r\n",str1,"Cookie: JSESSIONID=5386A9443729D7EB0B61E38A9C7CF52F");
	sprintf(str1, "%s\r\n",str1);	
#endif
	printf("----------------------------- HTTP Data ----------------------------------\n");
	printf("%s",str1);
	printf("--------------------------- Data Len=%d ----------------------------------\n\n",strlen(str1));
	
	int ret=send(sockfd, str1, strlen(str1), 0);   // 向服务器发送信息
	if(ret<0)
	{
		perror("send");
		close(sockfd);
		exit(-1);
	}
	
	char recv_buf[521]="";
	recv(sockfd, recv_buf, sizeof(recv_buf), 0);
	printf("------------------------ server retrun data -------------------------------\n");
	printf("%s\n\n",recv_buf);
	close(sockfd);

	return 0;
}


在搭建好的服务端,在Apache安装目录D:\AppServ\Apache24\htdocs 中新建文件index.php内容如下:

运行POST请求会出现如下结果:

----------------------------- HTTP Data ----------------------------------
POST http://192.168.2.203/index.php HTTP/1.1
Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/msword, application/vnd.ms-powerpoint, application/vnd.ms-excel, */*
Referer: */*
Accept-Language: en-US,zh-CN;q=0.5
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; qdesk 2.4.1265.203; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; InfoPath.3)
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: 192.168.2.203
Content-Length: 22
Connection: Keep-Alive
Cache-Control: no-cache

username=hanbo&num=123
--------------------------- Data Len=701 ----------------------------------

------------------------ server retrun data -------------------------------
HTTP/1.1 200 OK
Date: Mon, 06 Jun 2016 02:40:28 GMT
Server: Apache/2.4.10 (Win32) PHP/5.5.25
X-Powered-By: PHP/5.5.25
Content-Length: 22
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html

username=hanbo
num=123


运行GET请求会出现如下结果:

----------------------------- HTTP Data ----------------------------------
GET http://192.168.2.203/index.php?username=hanbo&num=123 HTTP/1.1
Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/msword, application/vnd.ms-powerpoint, application/vnd.ms-excel, */*
Accept-Language: en-US,zh-CN;q=0.5
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; qdesk 2.4.1265.203; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; InfoPath.3)
Accept-Encoding: gzip, deflate
Host: 192.168.2.203
Connection: Keep-Alive
Cookie: JSESSIONID=5386A9443729D7EB0B61E38A9C7CF52F

--------------------------- Data Len=644 ----------------------------------

------------------------ server retrun data -------------------------------
HTTP/1.1 200 OK
Date: Mon, 06 Jun 2016 02:42:52 GMT
Server: Apache/2.4.10 (Win32) PHP/5.5.25
X-Powered-By: PHP/5.5.25
Content-Length: 22
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html

username=hanbo
num=123


 


 

你可能感兴趣的:(其它,Linux,http协议,POST,GET,PHP)