不知怎传压缩包, 下次会了传压缩包. 这程序都能运行的.
HTTP 协议分析才做到这一步.
我可提供一点信息不错的地址, 看了再看我程序.:
(1))HTTP地址: http://www.nettoapp.com/Article/ShowArticle.asp?ArticleID=222
(2)HTTP协议状态码的含义地址: http://blog.csdn.net/kt400_hhx/archive/2007/05/31/1633451.aspx
(3) )HTTP地址: http://blog.csdn.net/guoguo1980/archive/2008/07/14/2649658.aspx
还有好多, 就不说啦, 总之一句话多用GOOGLE , 学会用, 以前我都不太用它,
有好多朋友说我怎不用GOOGLE上找.
如果不会先看, 看多了自然就会了.
这是自己写的一个源代码:
/**************************************
* HttpClient connection Server
*
* http protocol
*/
#include <windows.h>
#include <iostream>
#pragma comment(lib, "ws2_32")
using namespace std;
#define LEN 1024
int main()
{
int i=0;
char buf1[4096];
WORD wdVersionRequired;
WSADATA wsaData;
char saddress[100]="http://127.0.0.1:8080/cgi-bin/tt.cgi";
char protocolHead[]="GET /cgi-bin/tt.cgi HTTP/1.1\r\n"
"Accept: */*\r\n"
"Accept-Language: zh-cn,zh; q=0.5\r\n" //这 "\r\n" 不能丢一个. 不然程序不对.
"User-Agent: EasyAgent/1.01\r\n"
"Connection: Keep-Alive\r\n"
"Accept-Encoding: gzip, deflate\r\n"
"Host: 127.0.0.1:8080\r\n\r\n"; // 表示HTTP 结束符\r\n\r\n
printf("\n %s\n ", protocolHead);
int status;
char recvBuf[4096]="";
wdVersionRequired = MAKEWORD(2, 2); /* 2.2掳姹?*/
WSAStartup(wdVersionRequired, &wsaData);
SOCKET s = socket(AF_INET, SOCK_STREAM, 0);
if (SOCKET_ERROR == s)
{
return 0;
}
sockaddr_in saServer;
memset(&saServer, 0, sizeof(saServer));
saServer.sin_family = AF_INET;
saServer.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
saServer.sin_port = htons(8080);
if(connect(s, (const sockaddr*)&saServer, sizeof(sockaddr)) != 0)
{
perror("error");
exit(-1);
}
if(0 == send(s, protocolHead, sizeof(protocolHead), 0)) //send server
{
perror("error");
}
struct timeval tm = {20,7};
fd_set fds_r;
FD_ZERO(&fds_r);
FD_SET(s,&fds_r);
status=select(s+ 1, &fds_r, 0, 0, &tm); //socketId在这里是最大的fd
if(status > 0 && FD_ISSET(s, &fds_r))
{
printf("Socket is readable...fd=[%d]\n",s);
while(1)
{
memset(recvBuf, 0, sizeof(recvBuf));
if (recv(s,recvBuf,4096,0)==0)
{
break;
}
printf("\n%s\n", recvBuf);
}
}
// recv(s, buf1, sizeof(buf1), 0);
// printf("buf1: %s\n", buf1);
printf("send success!\n");
WSACleanup();
closesocket(s);
return 1;
}
===========================================================
要说一点: HTTP/1.0 200 OK 表示成功接收.
运行结果DOS窗口:
GET /cgi-bin/tt.cgi HTTP/1.1
Accept: */*
Accept-Language: zh-cn,zh; q=0.5
User-Agent: EasyAgent/1.01
Connection: Keep-Alive
Accept-Encoding: gzip, deflate
Host: 127.0.0.1:8080
Socket is readable...fd=[1952]
HTTP/1.0 200 OK
X-Powered-By: CSP/eybuild-2.6.8
Content-Type: text/html
<html>
<head>
<title></title>
</head>
<body>
hello world
<img src="?.=00001000" />
</body>
</HTML>
send success!
Press any key to continue