在TCP通信时,抓包得到的请求报文,我们需要对其进行分析,
用下面的报文举例:
GET /search?query=%E4%B8%89%E6%98%9F HTTP/1.1
Host: 192.168.0.118:50500
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 SLBrowser/9.0.5.8121 SLBChan/111 SLBVPV/64-bit
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
Referer: http://192.168.0.118:50500/submit
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9
可以看出,接收到的报文第一行包含了我们想要的信息,我们需要将%E4%B8%89%E6%98%9F这部分进行转换;
第一步:将%E4%B8%89%E6%98%9F 从第一行中拿到;
毫无疑问,strtok是最方便的办法:
如下:
char name[128] = {"GET /search?query=%E4%B8%89%E6%98%9F HTTP/1.1"};
char *temp = NULL;
temp = strtok(name,"=");
temp = strtok(NULL," ");
这样就分割出来了,现在对他进行转换,如下:
//现在temp指针指向 %E4%B8%89%E6%98%9F,
//接上面的继续写
char *end = NULL;
char name[128] = {0};
int i = 0;
temp = strtok(NULL,"%");
name[i] = strtol(temp,end,16);
i++;
while(1)
{
ptemp = strtok(NULL,"%");
if(ptemp == NULL)
{
name[i] = '\0';
break;
}
name[i] = strtol(ptemp,&end,16);
i++;
}
输出结果为:诺基亚
中心思想为:剔除%,用strtol将16进制乱码进行转换。