C发送http请求

之前用python编写了发送http请求的。非常非常方便。很多细节都已经被python内部处理了。这里来说说C编写的http请求:

#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define MAXLINE 1024

int main()
{
	int sockfd,n;
	char recvline[MAXLINE];
	struct sockaddr_in servaddr;
	char dns[32];
	char url[128];
	char *IP = "218.244.133.30";
	char *buf = "GET /forum.php HTTP/1.1\r\n\
Host: 218.244.133.30\r\n\
Proxy-Connection: keep-alive\r\n\
Cache-Control: max-age=0\r\n\
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n\
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36\r\n\
Accept-Encoding: gzip,deflate,sdch\r\n\
Accept-Language: zh-CN,zh;q=0.8,en;q=0.6\r\n\
Cookie: P52Q_2132_saltkey=M99iR1So; P52Q_2132_lastvisit=1402616897; P52Q_2132_nofavfid=1; P52Q_2132_visitedfid=2D41D38; P52Q_2132_ulastactivity=63e8Um91gzrgSl71VxWDURz5puThdraSkMWMi3yotj2QHi7O95z5; P52Q_2132_lastcheckfeed=2%7C1404624305; P52Q_2132_editormode_e=1; P52Q_2132_smile=2D1; P52Q_2132_st_t=0%7C1404692476%7Cf1fb107592d1e4dabffd9e8cc052c9bc; P52Q_2132_forum_lastvisit=D_41_1404310798D_2_1404692476; P52Q_2132_seccode=121.8807c72fe7905a644e; P52Q_2132_st_p=0%7C1404692480%7C80767b97b5c8a3d320ed9e0348d982cf; P52Q_2132_viewid=tid_76; P52Q_2132_sid=GQJqQ6; P52Q_2132_lastact=1404693952%09forum.php%09ajax\r\n\
\r\n";
	if((sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0)
		printf("socket error\n");
	printf("1\n");
	bzero(&servaddr,sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(80);
	if(inet_pton(AF_INET,IP,&servaddr.sin_addr) <= 0)
		printf("inet_pton error\n");
	if(connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr)) < 0)
		printf("connect error\n");
	write(sockfd,buf,strlen(buf));
	printf("%s\n\n",buf);
	while((n = read(sockfd,recvline,MAXLINE)) > 0)
	{
		recvline[n] = 0;
		if(fputs(recvline,stdout) == EOF)
			printf("fputs error\n");
	}
	if(n < 0)
		printf("read error\n");
	printf("all ok now\n");
	exit(0);
}


注意了,这个编写主要是buf的内容,不能出任何差错。一个空格都不能有,不然,就会报错。在这里,格式一定要注意,你要访问的某个网站,你可以用一些抓包软件来看看访问某个网站的请求是什么。是的,千万不能出错,一个空格,对方可能就返回错误呢。

执行返回内容:

GET /forum.php HTTP/1.1
Host: 218.244.133.30
Proxy-Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8,en;q=0.6
Cookie: P52Q_2132_saltkey=M99iR1So; P52Q_2132_lastvisit=1402616897; P52Q_2132_nofavfid=1; P52Q_2132_visitedfid=2D41D38; P52Q_2132_ulastactivity=63e8Um91gzrgSl71VxWDURz5puThdraSkMWMi3yotj2QHi7O95z5; P52Q_2132_lastcheckfeed=2%7C1404624305; P52Q_2132_editormode_e=1; P52Q_2132_smile=2D1; P52Q_2132_st_t=0%7C1404692476%7Cf1fb107592d1e4dabffd9e8cc052c9bc; P52Q_2132_forum_lastvisit=D_41_1404310798D_2_1404692476; P52Q_2132_seccode=121.8807c72fe7905a644e; P52Q_2132_st_p=0%7C1404692480%7C80767b97b5c8a3d320ed9e0348d982cf; P52Q_2132_viewid=tid_76; P52Q_2132_sid=GQJqQ6; P52Q_2132_lastact=1404693952%09forum.php%09ajax



HTTP/1.1 200 OK
Date: Mon, 07 Jul 2014 06:51:21 GMT
Server: Apache/2.2.15 (CentOS)
X-Powered-By: PHP/5.3.3
Set-Cookie: P52Q_2132_sid=mX4nx9; expires=Tue, 08-Jul-2014 06:51:21 GMT; path=/
Set-Cookie: P52Q_2132_lastact=1404715881%09forum.php%09; expires=Tue, 08-Jul-2014 06:51:21 GMT; path=/
Set-Cookie: P52Q_2132_onlineusernum=1; expires=Mon, 07-Jul-2014 06:56:21 GMT; path=/
Set-Cookie: P52Q_2132_sid=mX4nx9; expires=Tue, 08-Jul-2014 06:51:21 GMT; path=/
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html; charset=gbk

42cc




��̳ -  Powered by Discuz!




















Discuz! Board

 �һ�����
 ����ע��
����
����: � ���� discuz

����: 0|����: 10|����: 232|��Ա: 58|��ӭ�»�Ա: altenli

����/չ��

���߻�Ա - 1 ����� - 1 ��Ա(0 ����), 0 λ�ο - ���߼�¼�� 23 �� 2014-6-8.

����Ա       ��������       ����       ��Ա      

Archiver|�ֻ���|С����|Comsenz Inc.  

GMT+8, 2014-7-7 14:51 , Processed in 0.035802 second(s), 15 queries .

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.


有了这些,我想进一步处理就非常好了。但是如果你想更近一步,我想原始数据包肯定是一个重点研究的方向。

你可能感兴趣的:(网络安全)