解析浏览器请求头在c++
Parsing browser request header in C++
我在c++中做了一个简单的web服务器,我需要解析请求头。我是怎么做到的?
这是我的标题…
GET /test?username=2 HTTP/1.1
Host: stream.mysite.com:7777
Connection: keep-alive
Cache-Control: max-age=0
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: auth=asdfasdfaasdfasd
我需要得到页面(/test?username=2)和cookie变量auth的内容(asdfasdfaasdfasd)。
谢谢!
一个简单的例子:
#include <iostream>
#include <string>
#include <sstream>
int main() {
std::string tk1, tk2, line = "GET /test?username=2 HTTP/1.1";
std::stringstream ss(line);
ss >> tk1;
ss >> tk2;
if (tk1 == "GET") {
std::cout << "requested path: " << tk2 << std::endl;
}
return 0;
}
这里定义了一个Http请求:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html基本上你需要知道的:
GET <URL> <HTTP-Version><CRLF>
<Set of Headers each line terminated with <CRLF>>
<Empty Line with just <CRLF>>
您想要的位将始终是
相关文章:
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 如何在boost beast http请求中设置http头
- 发送一个带有libcurl C++问题的帖子请求:s
- 在多个核心中处理一个HTTP请求
- 错误:请求非类类型为"MULTIMEDIA_FILME [500]"的成员|
- 使用 Winsock2.h C++向不和谐 API 发送 HTTP 请求时出现问题
- 每个服务器请求的内存预算
- std::getline没有在while循环中重新请求用户输入
- 请求最简单的 OpenMP 目标 GPU 示例
- 请求有关C++中嵌入 for 循环的帮助
- 错误:请求从"常量字符 [5]"转换为非标量类型"字符串"
- 使用 winsock 接收 http 请求
- QNetworkAccessManager 稍后在添加请求时删除
- 为什么我收到来自谷歌的错误请求?
- 从网链套接字请求连接设备的列表
- libcurl :C++处理多个异步请求
- 对于 http 请求,python 比 c++ 快吗?
- 通过引用传递向量是请求 std::分配器
- 为什么当通过 TCP 发送的消息速率增加时,请求-响应消息对的延迟会降低?
- 如何使用C++和Boost Asio从HTTP发布请求中获取键值