目录
1.序列化和反序列化
2.HTTP协议
3.编写一个简单的http
3.2.简单的http的使用
3.3.get和post请求传参的区别
4.http的状态码分类
1.1.序列化和反序列化的优势
1.2.序列化和反序列化需要那些要求
双方都要知道的条件不就是一种协议吗
HTTP(超文本传输协议)是一个应用层协议,超文本:含有指向其它文本文件链接的文本
http协议通常以3到4部分组成:
2.1.URL就是网址
3.1查看http的请求
套接字的封装:socket.hpp
#pragma once #include
#include #include #include #include #include namespace ns_socket{class sock{public:static int Socket(){int sock=socket(AF_INET,SOCK_STREAM,0);if(sock<0){std::cerr<<"socket"< 服务器端获取http的请求并打印打出来
#include "socket.hpp" #include
#include #include using namespace ns_socket; using namespace std;#define CAPACITY 1024 * 10void *HandlerRequest(void *args) {pthread_detach(pthread_self());//线程分离int new_sock = *((int *)args);//获取http请求char buffer[CAPACITY]={0};ssize_t s=recv(new_sock,buffer,sizeof(buffer)-1,0);if(s>0){buffer[s]=0;cout< 建立网络通信
int main() {int sock = sock::Socket();sock::Bind(sock, 8080);sock::Listen(sock);while (1){int new_sock = sock::Accept(sock);cout << "get a new link" << endl;pthread_t tid;pthread_create(&tid, 0, HandlerRequest, (void *)&new_sock);}return 0; }
执行结果:
前端代码
你好吗!
其它的代码和上面的一样,只有这个线程执行的函数不同
#include "socket.hpp"#include
#include
#include
#include
#include
#include using namespace ns_socket;
using namespace std;#define CAPACITY 1024 * 10
#define WWWROOT "./wwwroot/"
#define HOME_PAGE "index.html"void *HandlerRequest(void *args)
{pthread_detach(pthread_self());int new_sock = *((int *)args);// 资源路径std::string file_path = WWWROOT;file_path += HOME_PAGE;//打开资源文件ifstream in(file_path.c_str());if (in.is_open()){// 获取文件属性struct stat page_stat;stat(file_path.c_str(), &page_stat);//编写http的响应std::string http_response;http_response += "http/1.0 200 ok\n";;//状态行http_response += "Content-Type: text/html\n";//资源是什么类型,查content-type对照表//资源的长度,字节为单位http_response += "Content-Longth: ";http_response += std::to_string(page_stat.st_size);http_response += "\n";http_response += "\n"; // 空行// http的有效载荷,正文std::string content;std::string line;while (std::getline(in, line)){content += line;}http_response += content;//把响应发给对端 send(new_sock, http_response.c_str(), http_response.size(), 0);}close(new_sock);
}
执行的结果:
3.3.2.
hello 我是首页!
hello 我是表单!
姓名:
密码:
使用POST传参数,参数是通过正文传参的;
3.3.3.get和post的区别
数字 | 类型含义 | 类型解释 |
1XX | Information(信息型状态码) | 接受的请求正在处理(不常见,处理的请求通常不会很长) |
2XX | Success(成功状态码) | 类似200 表示请求被处理成功 |
3XX | Redirection(重定向状态码) | 访问某一个服务器,会响应到其他网址;跳转到其他网址 |
4XX | Clinet Error | 类似404 Not Found ,服务器处理不了请求 |
5XX | Server Error | 服务器处理请求失败 |
4.1.重定向如何实现
void *HandlerRequest(void *args)
{pthread_detach(pthread_self());int new_sock = *((int *)args);std::string response;response+="http/1.0 301 Permanently moved\n";response+="Location: https://www.baidu.com/\n";//location报文配合301永久重定向或者302临时重定向使用response+="\n";send(new_sock,response.c_str(),response.size(),0);close(new_sock);
}
5.1.为什么需要cookie
当我们登录CSDN后,访问任一一篇文章都是一个另外新的链接,http协议是一个无状态的协议(不会认识用户),为什么不需要我们再输入一次密码,来验证是用户;
cookie中保存用户的私密信息,如果每次都需要我们输入账号密码会让客户端的体验变差
5.2.cookie的思想和自己写一个cookie
5.2.1.自己写一个简单cookie测试
void *HandlerRequest(void *args)
{pthread_detach(pthread_self());int new_sock = *((int *)args);char client_requist[1024*10]={0};recv(new_sock,client_requist,sizeof(client_requist)-1,0);std::cout<
5.3.sesion(英文会话的意思)
因为如果cookie直接保存用户的账号密码,被盗取就非常危险;