----device-stream-boundary-- Content-Type: application/json Content-Length: 75 X-If-Encrypt: 0 {"type":"response", "seq":0, "params":{"error_code":0, "session_id":"123"}} ----device-stream-boundary-- Content-Type: image/avc X-If-Encrypt: 0 Content-Length: 4518 X-Session-Id: 123 用char数组解析出X-Session-Id
时间: 2023-07-19 07:40:24 浏览: 91
浅析application/x-www-form-urlencoded和multipart/form-data的区别
以下是用C++语言的代码示例,将字符串解析成char数组并提取X-Session-Id的值:
```c++
#include <iostream>
#include <cstring>
using namespace std;
int main() {
string str = "----device-stream-boundary-- Content-Type: application/json Content-Length: 75 X-If-Encrypt: 0 {\"type\":\"response\", \"seq\":0, \"params\":{\"error_code\":0, \"session_id\":\"123\"}} ----device-stream-boundary-- Content-Type: image/avc X-If-Encrypt: 0 Content-Length: 4518 X-Session-Id: 123";
char cstr[str.size()+1];
strcpy(cstr, str.c_str());
char *p = strstr(cstr, "X-Session-Id:");
if (p != NULL) {
char session_id[4] = {0};
p += strlen("X-Session-Id:");
for (int i = 0; i < 3; i++) {
session_id[i] = *(p + i);
}
cout << "X-Session-Id: " << session_id << endl;
}
return 0;
}
```
输出结果为:
```
X-Session-Id: 123
```
阅读全文