You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
TermApp/app/src/main/cpp/netcamera/httpclient.cpp

173 lines
5.3 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "httpclient.h"
#include "netcamera.h"
static size_t OnWriteData(void* buffer, size_t size, size_t nmemb, void* lpVoid)
{
std::vector<uint8_t>* data = (std::vector<uint8_t>*)lpVoid;
if( NULL == data || NULL == buffer )
{
return -1;
}
uint8_t* begin = (uint8_t *)buffer;
uint8_t* end = begin + size * nmemb;
data->insert(data->end(), begin, end);
return nmemb;
}
int DoGetRequest(const char* url, const char* userName, const char* password, const char* interface, std::vector<uint8_t>& data)
{
CURLcode nRet;
std::string auth;
CURL *curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
curl_easy_setopt(curl, CURLOPT_URL, url);
if (userName != NULL && password != NULL && strlen(userName) > 0)
{
auth = userName;
auth += ":";
auth += password;
curl_easy_setopt(curl, CURLOPT_USERPWD, auth.c_str());
// DIGEST Auth
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
}
// 设置回调函数
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
// 设置回调函数的参数,获取反馈信息
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
// 接收数据时超时设置如果5秒内数据未接收完直接退出
#ifndef NDEBUG
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);
#else
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 60);
#endif
// 设置重定向次数,防止重定向次数太多
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 4);
// 连接超时,这个数值如果设置太短可能导致数据请求不到就断开了
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10);
if (interface != NULL && strlen(interface) > 0)
{
curl_easy_setopt(curl, CURLOPT_INTERFACE , interface);
}
nRet = curl_easy_perform(curl);
if (CURLE_OK != nRet)
{
printf("GET err=%d", nRet);
}
curl_easy_cleanup(curl);
return (0 == nRet) ? 0 : 1;
}
int DoPutRequest(const char* url, const char* userName, const char* password, const char* interface, const char* contents, std::vector<uint8_t>& data)
{
std::string auth;
CURL *curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_easy_setopt(curl, CURLOPT_URL, url);
if (userName != NULL && password != NULL && strlen(userName) > 0)
{
auth = userName;
auth += ":";
auth += password;
curl_easy_setopt(curl, CURLOPT_USERPWD, auth.c_str());
// DIGEST Auth
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
}
// 设置回调函数
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
// 设置回调函数的参数,获取反馈信息
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
// 接收数据时超时设置如果5秒内数据未接收完直接退出
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 60);
// 设置重定向次数,防止重定向次数太多
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 4);
// 连接超时,这个数值如果设置太短可能导致数据请求不到就断开了
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10);
if (interface != NULL && strlen(interface) > 0)
{
curl_easy_setopt(curl, CURLOPT_INTERFACE , interface);
}
CURLcode nRet = curl_easy_perform(curl);
if (CURLE_OK != nRet)
{
printf("GET err=%d", nRet);
}
curl_easy_cleanup(curl);
return (0 == nRet) ? 0 : 1;
}
namespace nc_hy
{
bool requestCapture(uint8_t channel, uint8_t preset, const NET_PHOTO_INFO& photoInfo)
{
bool res = false;
std::vector<uint8_t> data;
const char* userName = NULL;
const char* password = NULL;
if (photoInfo.authType != 0)
{
userName = photoInfo.userName;
password = photoInfo.password;
}
const char* interface = photoInfo.interface;
std::string url = "http://";
url += photoInfo.ip;
url += photoInfo.url;
int nRet = DoGetRequest(url.c_str(), userName, password, interface, data);
if (0 == nRet)
{
if (!data.empty())
{
FILE *fp = fopen(photoInfo.outputPath, "wb");
if (fp != NULL)
{
fwrite(&data[0], data.size(), 1, fp);
fclose(fp);
res = true;
}
}
}
return res;
}
bool requestCapture(uint8_t channel, uint8_t preset, const NET_PHOTO_INFO& photoInfo, std::vector<uint8_t>& img)
{
bool res = false;
const char* userName = NULL;
const char* password = NULL;
if (photoInfo.authType != 0)
{
userName = photoInfo.userName;
password = photoInfo.password;
}
std::string url = "http://";
url += photoInfo.ip;
url += photoInfo.url;
int nRet = DoGetRequest(url.c_str(), userName, password, photoInfo.interface, img);
#ifdef _DEBUG
if (0 == nRet)
{
FILE *fp = fopen("/sdcard/com.xypower.mpapp/tmp/netimg.jpg", "wb");
if (fp != NULL)
{
fwrite(&img[0], img.size(), 1, fp);
fclose(fp);
}
}
#endif
return (0 == nRet);
}
}