在使用c++去请求https服务的时候,使用的curl工具提供的类库 libcurl但是在使用的过程中发现,如果请求的参数值带了空格或者是参数是中文,会导致响应的回调函数没有被执行,虽然curl_easy_perform 返回的CURLcode 为 CURLE_OK,但是实际上是没有数据的。
获取到的gps轨迹信息请求Url:
https://20.232.11.27:8991/api/gis/gis/device/track/?beginTime=2023-05-29 10:09:07&endTime=2023-05-29 10:09:12
libcurl返回码是:0,返回信息:
<html>
<head><title>400 Bad Request</title></head>
<body>
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx/1.22.1</center>
</body>
</html>
考虑到可能需要转义
使用 curl_easy_escape 方法可解决上述的问题,注意使用的时候需要释放返回的字符串,举例如下:
char* pszBeginTime = curl_easy_escape(NULL, strBeginTime.c_str(), strBeginTime.length());
char* pszEndTime = curl_easy_escape(NULL, strBeginTime.c_str(), strBeginTime.length());
strUrl = "https://20.232.11.27:8991/api/gis/gis/device/track/" + deviceid + "?beginTime=" + pszBeginTime + "&endTime=" + pszEndTime;
curl_free(pszBeginTime);
curl_free(pszEndTime);
curl_easy_setopt(pCurl, CURLOPT_URL, str_url.c_str());
使用curl_escape() 函数对所要发送的请求参数进行处理,然后再发送.
注意:
curl_escape() 函数 不能对整个url 请求地址进行处理,只能对后面的请求参数进行处理,否则会造成地址错误。
1758

被折叠的 条评论
为什么被折叠?



