¥¥¥¥求C语言书写的HTTP报头结构¥¥¥
¥¥¥¥求C语言书写的HTTP报头结构¥¥¥ 问题点数:0、回复次数:9Top
1 楼masterz(www.fruitfruit.com)回复于 2003-01-20 11:13:17 得分 0
//Get html page by SDK socket
#define host_name "www.csdn.net"
#define winsock_version 0x0101
void main()
{
SOCKADDR_IN saServer;
LPHOSTENT lphostent;
WSADATA wsadata;
SOCKET hsocket;
int nRet;
char hostname[100] ;
wsprintf(hostname,"GET /expert/topic/378/378382.shtm HTTP/1.0 %c%c",10,10);
printf("%s",hostname);
char dest[1000];
if(WSAStartup(winsock_version,&wsadata))
printf("can't open");
lphostent=gethostbyname(host_name);
if(lphostent==NULL)
printf("lphostent is null");
hsocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
saServer.sin_family = AF_INET;
// Use def. now, need to handle general case
saServer.sin_port = htons(80);
saServer.sin_addr = *((LPIN_ADDR)*lphostent->h_addr_list);
nRet = connect(hsocket, (LPSOCKADDR)&saServer, sizeof(SOCKADDR_IN));
if (nRet == SOCKET_ERROR)
{
printf("can't connect");
closesocket(hsocket);
return;
}
else
printf("connected with %s\n",host_name);
nRet = send(hsocket, hostname, strlen(hostname), 0);
if (nRet == SOCKET_ERROR)
{
printf("send() failed");
closesocket(hsocket);
}
else
printf("send() OK\n");
nRet=1;
while(nRet>0)
{
nRet=recv(hsocket,(LPSTR)dest,sizeof(dest),0);
if(nRet>0)
dest[nRet]=0;
else
dest[0]=0;
printf("\nReceived bytes:%d\n",nRet);
printf("Result:\n%s",dest);
}
}
///////////////////////////////////////////////////////////////////////////
//SDK post
///////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "winsock.h"
#pragma comment(lib,"ws2_32.lib")
#define winsock_version 0x0101
void main()
{
//I create C:\Inetpub\wwwroot\test\test.asp ,start the web service
//start my program, the result is OK.
//If it works,it is written by masterz,otherwise I don't know who write it.
SOCKADDR_IN saServer;
LPHOSTENT lphostent;
WSADATA wsadata;
SOCKET hsocket;
int nRet;
const char* host_name="127.0.0.1";
char* req="POST /test/test.asp HTTP/1.0\r\n"
"From: local\r\n"
"User-Agent: post_test/1.0\r\n"
"Content-Type: application/x-www-form-urlencoded\r\n"
"Content-Length: 20\r\n\r\n"
"type=12345&name=aaaa";
if(WSAStartup(winsock_version,&wsadata))
printf("can't initial socket");
lphostent=gethostbyname(host_name);
if(lphostent==NULL)
printf("lphostent is null");
hsocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
saServer.sin_family = AF_INET;
// Use def. now, need to handle general case
saServer.sin_port = htons(80);
saServer.sin_addr = *((LPIN_ADDR)*lphostent->h_addr_list);
nRet = connect(hsocket, (LPSOCKADDR)&saServer, sizeof(SOCKADDR_IN));
if (nRet == SOCKET_ERROR)
{
printf("can't connect");
closesocket(hsocket);
return;
}
else
printf("connected with %s\n",host_name);
nRet = send(hsocket, req, strlen(req), 0);
if (nRet == SOCKET_ERROR)
{
printf("send() failed");
closesocket(hsocket);
}
else
printf("send() OK\n");
char dest[1000];
nRet=1;
while(nRet>0)
{
nRet=recv(hsocket,(LPSTR)dest,sizeof(dest),0);
if(nRet>0)
dest[nRet]=0;
else
dest[0]=0;
printf("\nReceived bytes:%d\n",nRet);
printf("Result:\n%s",dest);
}
}
Top
2 楼andy1976(风流键客)回复于 2003-01-20 16:04:42 得分 0
我是用来解析截获的HTTP数据包的,
请给我一个类似这样的数据结构:
typedef struct _IPHdr
{
#if defined(WORDS_BIGENDIAN)
u_int8_t ip_ver:4, /* IP version */
ip_hlen:4; /* IP header length */
#else
u_int8_t ip_hlen:4, ip_ver:4;
#endif
u_int8_t ip_tos; /* type of service */
u_int16_t ip_len; /* datagram length */
u_int16_t ip_id; /* identification */
u_int16_t ip_off; /* fragment offset */
u_int8_t ip_ttl; /* time to live field */
u_int8_t ip_proto; /* datagram protocol */
u_int16_t ip_csum; /* checksum */
struct in_addr ip_src; /* source IP */
struct in_addr ip_dst; /* dest IP */
} IPHdr;
Top
3 楼ty_star(小鼹鼠)回复于 2003-01-21 10:11:58 得分 0
收藏!Top
4 楼shitalone(西特龙)回复于 2003-01-21 10:20:27 得分 0
Http包是Tcp报文
去掉Ip头、Tcp头,剩下的就是Http部分和校验和。
看看Http的rfc吧Top
5 楼rafter263(寒歌)回复于 2003-01-22 09:15:00 得分 0
mkTop
6 楼andy1976(风流键客)回复于 2003-01-23 16:14:50 得分 0
看RFC,怎么觉得分析这个HTTP有点象编译原理的语法分析?好痛苦,我没有必要搞清楚整个协议,我只需要知道包是请求哪一个网址,也就是取得请求行,和HOST标题就可以了,但这个协议好象没有象IP协议那样的报头,指示哪个域多长,代表什么,而都是用什么空行,空格来分开每个不同意义的部分,这如何分析啊,不搞通整个协议能作到我的简单要求吗(只要请求行和HOSE标题的值)Top
7 楼andy1976(风流键客)回复于 2003-01-26 21:49:11 得分 0
helpTop
8 楼mengxihe(濛溪河)回复于 2003-01-27 09:25:19 得分 0
upTop
9 楼shesh(无所谓)回复于 2003-02-13 13:23:40 得分 0
CString m_Command = "HTTP/1.1 200 OK\r\n";
m_Command += "Server: Microsoft-IIS/5.0\r\n";
m_Command += "Date: Thu, 13 Jul 2003 05:46:53 GMT\r\n";
CString m_Strlen;
m_Strlen.Format("Content-Length: %d\r\n",len);
m_Command += m_Strlen;
m_Strlen.Format("Content-Type: %d\r\n",command);
// m_Strlen.Format("Content-Type: *.*\r\n");
m_Command += m_Strlen;
m_Command += "Connection: Keep-Alive\r\n";
m_Command += "Cache-Control: no-cache\r\n\r\n";
Top





