探究http协议

Posted multisim

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了探究http协议相关的知识,希望对你有一定的参考价值。

所谓http协议就是一种数据结构。

探究的目的是为了更好的理解网络模型--OSI七层参考模型和TCP/IP四层参考模型

如下:L"GET /myweb.htm HTTP/1.1\r\n"

L"Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"

L"Accept-Language:zh-Hans-CN,zh-Hans;q=0.8,en-US;q=0.6,en; q=0.4, ja; q=0.2\r\n"

L"Host:%s\r\n"

L"Connection:Keep-Alive\r\n"

L"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134\r\n\r\n",

 

上面的L加字符串是表示宽字符串。

 

数据类型不过多累赘。

 

上代码

 

#include<stdio.h>

#include<Windows.h>

#define RECV_BUFFER_SIZE 8192

 

 

void HttpPostPacket(LPWSTR packet,LPWSTR url,LPWSTR host,CHAR* data)

{

wsprintf(packet,L"POST %s HTTP/1.1\r\n"

L"Accept:*/*\r\n"

L"Accept-Encoding: gzip, deflate\r\n"

L"Content-Type:application/x-www-form-urlencode\r\n"

L"Host:%s\r\n"

L"Content-Length:%d\r\n"

L"Connection:Keep-Alive\r\n"

L"Cookie:"

L"%s\r\n\r\n",

url,host,strlen(data),data);

}

 

 

void HttpGetPacket(LPWSTR packet,LPWSTR url,LPWSTR host,CHAR* data)

{

wsprintf(packet,L"GET %s?%s HTTP/1.1\r\n"

L"Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"

L"Accept-Language:zh-Hans-CN,zh-Hans;q=0.8,en-US;q=0.6,en; q=0.4, ja; q=0.2\r\n"

L"Host:%s\r\n"

L"Connection:Keep-Alive\r\n"

L"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134\r\n\r\n",

url,data,host);

}

 

 

 

 

void main(int argc,char* argv[]){

 

wchar_t packet[1024];

char mulpacket[1024];

LPWSTR url=L"/zhuti_multisim_1.html";//实际页面URL: http://bbs.elecfans.com/zhuti_multisim_1.html

LPWSTR host=L"bbs.elecfans.com";

char data[80]="";

//HttpPostPacket(packet,url,host,data);

//printf("%s\n",packet);

 

HttpGetPacket(packet,url,host,data);

int d;

WideCharToMultiByte(

CP_UTF8,WC_SEPCHARS,

packet,

1024,

mulpacket,

1024,

NULL,

&d

);

SOCKADDR_IN clientService;

SOCKET ConnectSocket;

WSADATA wsaData;

LPVOID recvbuf;

int bytesRecv=0;

int bytesSent;

char sendbuf[1024]={0};

char acp[8192]={0};

char *ip="117.25.133.179";

 

 

int iResult=WSAStartup(MAKEWORD(2,2),&wsaData);

if(iResult!=NO_ERROR){

printf("Error at WSASTARTUP()\n");

}

ConnectSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

if(ConnectSocket==INVALID_SOCKET){

printf("Error at socket():%ld\n",WSAGetLastError());

WSACleanup();

return;

}

clientService.sin_family=AF_INET;

clientService.sin_addr.s_addr=inet_addr(ip);

clientService.sin_port=htons(80);//WEB PORT 80

if(connect(ConnectSocket,(SOCKADDR*)&clientService,sizeof(clientService))==SOCKET_ERROR){

printf("Faild to connect(%d)\n",WSAGetLastError());

WSACleanup();

return;

}

if(argc==2&&(!lstrcmpA(argv[1],"-d"))){

strcpy_s(sendbuf,1024,mulpacket);

}

bytesSent=send(ConnectSocket,sendbuf,lstrlenA(sendbuf)+1,0);

if(bytesSent==SOCKET_ERROR)

{

printf("send error(%d)\n",WSAGetLastError());

closesocket(ConnectSocket);

return ;

}

printf("Bytes Sent:%ld\n",bytesSent);

recvbuf=HeapAlloc(GetProcessHeap(),0,RECV_BUFFER_SIZE);

ZeroMemory(recvbuf,RECV_BUFFER_SIZE);

while(bytesRecv!=SOCKET_ERROR)

{

bytesRecv=recv(ConnectSocket,(char*)recvbuf,RECV_BUFFER_SIZE,0);

if(bytesRecv==0)

{

printf("Connection Closed.\n");

break;

}

int ulen=MultiByteToWideChar(CP_UTF8,0,(LPCH)recvbuf,-1,NULL,0);

 

wchar_t j[8192]={0};

MultiByteToWideChar(CP_UTF8,0,(LPCH)recvbuf,-1,j,ulen);

ulen=WideCharToMultiByte(CP_ACP,0,j,-1,NULL,0,NULL,NULL);

WideCharToMultiByte(CP_ACP,0,j,-1,(LPSTR)recvbuf,ulen,NULL,NULL);

printf("Bytes Recv:%ld\n",bytesRecv);

printf("%s\n",recvbuf);

}

HeapFree(GetProcessHeap(),0,recvbuf);

WSACleanup();

system("pause");

return;

}

由上述代码可扩展到ftp,telnet,e-mail,https.

上面的代码在vs2012可正常编译运行。**注意字符集是unicode。

效果如下








若有疑问发消息到后台。













以上是关于探究http协议的主要内容,如果未能解决你的问题,请参考以下文章

iOS开发探究--HTTP与HTTPS

php 伪协议探究

HTTP/3,从TCP到UDP。一文带你探究HTTP的“前世今生”

HTTPS 加密原理探究

http协议发送header+body+json及接收解析

WebSocket协议探究