生活的天平本不平衡,只有通过努力改变其偏向。

通过Sock进行http连接事例

2008-03-21

一段使用sock进行http连接的事例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <winsock2.h>
#include <windows.h>
#include <stdio.h>
#pragma comment(lib, "ws2_32.lib") //链接到WS2_32.LIB库
//下面是定义header包头数据的字符数组:
char *HMod[] =     { "GET","POST"};
char *HttpVer[] =   { "HTTP/1.0", "HTTP/1.1"};
char *HAccept[] =   { "Accept:"," image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"};
char *HAcceptLg[] =   { "Accept-Language:"," zh-cn"};
char *HContentTp[]=   { "Content-Type:"," application/x-www-form-urlencoded"};
char *HAcceptEn[] =   { "Accept-Encoding:"," gzip, deflate"};
char *HUserAgent[]=   { "User-Agent:"," Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon)"};
char *HReferer[]=   { "REFERER:"," http://localhost/"};
char *HHost[]=     { "Host:"," http://http://www.dream2fly.net/"};
char *HContentLg[]=   { "Content-Length:"};
char *HContion[]=   { "Connection:"," Keep-Alive"};
char *HAcceptCt[]=   { "Accept-Charset:"," iso-8859-1,*,utf-8"};
char *HCacheCtr[]=   { "Cache-Control:"," no-cache"};
//char *Data[]={" "};
char temp1[1024],temp2[10240];
int Ret,ren = 0;
int main(int argc,char *argv[]){
WSADATA WSAData={0};
struct sockaddr_in ServerAddr={0};
SOCKET ServerSocket=0;
struct hostent *pHostent;
//初使化winsock版本2.2:
if((Ret=WSAStartup(MAKEWORD(2,2), &WSAData))!=0)
{   printf("WSAStartup failed with error %d!\n",Ret);
return 0;
}
//创建一个新套接字来建立客户连接:
ServerSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
//下面将网址转换成IP地址:
pHostent = gethostbyname("http://www.dream2fly.net");
ServerAddr.sin_family = AF_INET; //Ipv4地址族
//取得数组中的第一个IP地址:
ServerAddr.sin_addr.s_addr = *(long *)pHostent->h_addr_list[0];
//将主机字节顺序转换为网络字节顺序:
ServerAddr.sin_port = htons(80);
memset(temp1,0,1024); //清0
memset(temp2,0,10240);
sprintf(temp1, "%s %s %s\r\n"
"%s%s\r\n"
"%s%s\r\n"
"%s%s\r\n"
"%s%s\r\n"
"%s%s\r\n"
"%s%s\r\n"
"%s%s\r\n"
"%s%s\r\n"
"%s%s\r\n"
"%s%s\r\n"
"%s%s\r\n"
"\r\n"
,
HMod[0],"/",HttpVer[1], //GET /index.php HTTP/1.1
HAccept[0],HAccept[1], //Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
HAcceptLg[0],HAcceptLg[1],//Accept-Language: zh-cn
HContentTp[0],HContentTp[1],//Content-Type: application/x-www-form-urlencoded
HAcceptEn[0],HAcceptEn[1],//Accept-Encoding: gzip, deflate
HUserAgent[0],HUserAgent[1],//User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon)
HReferer[0],HReferer[1],//REFERER: http://localhost/
HHost[0],HHost[1],//Host: http://http://www.dream2fly.net/
HContentLg[0]," 0",//Content-Length: 0
HContion[0],HContion[1],//Connection: Keep-Alive
HCacheCtr[0],HCacheCtr[1],//Cache-Control: no-cache
HAcceptCt[0],HAcceptCt[1]//Accept-Charset: iso-8859-1,*,utf-8
);
connect(ServerSocket,(SOCKADDR *)&ServerAddr,sizeof(ServerAddr));
send(ServerSocket,temp1,strlen(temp1),0);
while((ren = recv(ServerSocket,temp2+strlen(temp2),10240-strlen(temp2),0))<=0){;}
printf("返回信息:\n%s",temp2);
printf("\r\nPress any key to exit….");
getchar();//从stdin流中读字符
closesocket(ServerSocket);
WSACleanup();
return 0;
}
作者:lonkil | 分类目录:编程开发 | 标签:

评论已关闭。