场景

    向海康人脸服务器注册告警抓拍推送的主机IP地址和端口,采用的是数字认证(degist md5)


代码

 Json::Value root;
 Json::Value context;
 char szUrl[128] = { 0 };
 char szIP[128] = "192.168.10.85";
 int nPort = 9000;
 sprintf(szUrl, "http://%s:%d/alarm", szIP, nPort);
 context["url"] = szUrl;
 context["protocolType"] = "HTTP";
 context["parameterFormatType"] = "json";
 context["addressingFormatType"] = "ipaddress";
 context["ipAddress"] = szIP;
 context["ipv6Address"] = 0;
 context["portNo"] = nPort;
 context["httpAuthenticationMethod"] = "MD5digest";
 context["uploadImagesDataType"] = "URL";
 context["format"] = "json";
 context["eventType"] = "alarmResult,captureResult";
 root["HttpHostNotification"] = context;
 std::string strPostData = root.toStyledString();
 CURL *pCurlHandle = curl_easy_init();
 struct curl_slist *pCurlHeadList = NULL;
 curl_easy_setopt(pCurlHandle, CURLOPT_CUSTOMREQUEST, "POST");
 curl_easy_setopt(pCurlHandle, CURLOPT_URL, "http://192.168.10.211:80/ISAPI/Event/notification/httpHosts?format=json");
 curl_easy_setopt(pCurlHandle, CURLOPT_USERPWD, "admin:admin12345");
 curl_easy_setopt(pCurlHandle, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
 pCurlHeadList = curl_slist_append(pCurlHeadList, "Content-Type: application/json");
 curl_easy_setopt(pCurlHandle, CURLOPT_HTTPHEADER, pCurlHeadList);
 curl_easy_setopt(pCurlHandle, CURLOPT_WRITEFUNCTION, WriteResponseBody);//设置回调函数                   //curl_easy_setopt(pCurlHandle, CURLOPT_HEADER, 1);//保存HTTP头部信息到strResponseData
 curl_easy_setopt(pCurlHandle, CURLOPT_WRITEDATA, &strResponseData);//设置回调函数的参数,获取反馈信息
 curl_easy_setopt(pCurlHandle, CURLOPT_TIMEOUT, 15);//接收数据时超时设置,如果10秒内数据未接收完,直接退出
 curl_easy_setopt(pCurlHandle, CURLOPT_MAXREDIRS, 1);//查找次数,防止查找太深
 curl_easy_setopt(pCurlHandle, CURLOPT_CONNECTTIMEOUT, 5);//连接超时,这个数值如果设置太短可能导致数据请求不到就断开了
 curl_easy_setopt(pCurlHandle, CURLOPT_POSTFIELDS, strPostData.c_str());

 CURLcode nRet = curl_easy_perform(pCurlHandle);
 if (0 == nRet)
 {
  std::cout << strResponseData << std::endl;
 }
 curl_easy_cleanup(pCurlHandle);



删除注册

curl_easy_setopt(hnd, CURLOPT_URL, "http://192.168.10.211:80/ISAPI/Event/notification/httpHosts?format=json&ID=1");