#include "httpclient.h" #include "netcamera.h" #include static size_t OnWriteData(void* buffer, size_t size, size_t nmemb, void* lpVoid) { std::vector* data = (std::vector*)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; } static int SockOptCallback(void *clientp, curl_socket_t curlfd, curlsocktype purpose) { net_handle_t netHandle = *((net_handle_t *)clientp); int res = android_setsocknetwork(netHandle, curlfd); if (res == -1) { int errcode = errno; printf("android_setsocknetwork errno=%d", errcode); } return res == 0 ? CURL_SOCKOPT_OK : CURL_SOCKOPT_ERROR; } int DoGetRequest(const char* url, int authType, const char* userName, const char* password, net_handle_t netHandle, std::vector& 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 (authType != HTTP_AUTH_TYPE_NONE) { if (userName != NULL && password != NULL && strlen(userName) > 0) { auth = userName; auth += ":"; auth += password; curl_easy_setopt(curl, CURLOPT_USERPWD, auth.c_str()); // DIGEST Auth if (authType == HTTP_AUTH_TYPE_BASIC) { curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); } else if (authType == HTTP_AUTH_TYPE_DIGEST) { curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); } } } if (netHandle != NETWORK_UNSPECIFIED) { curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, SockOptCallback); curl_easy_setopt(curl, CURLOPT_SOCKOPTDATA, &netHandle); } // curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData); // 设置回调函数的参数,获取反馈信息 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data); // 接收数据时超时设置,如果5秒内数据未接收完,直接退出 #ifndef NDEBUG curl_easy_setopt(curl, CURLOPT_TIMEOUT, 60); #else curl_easy_setopt(curl, CURLOPT_TIMEOUT, 60); #endif // 设置重定向次数,防止重定向次数太多 curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 4); // 连接超时,这个数值如果设置太短可能导致数据请求不到就断开了 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10); nRet = curl_easy_perform(curl); long responseCode = 0; if (CURLE_OK == nRet) { curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &responseCode); if (responseCode != 200) { #ifdef _DEBUG char * log = new char[data.size() + 1]; log[data.size()] = 0; memcpy(log, &data[0], data.size()); printf("%s", log); delete[] log; #endif } } else { printf("GET err=%d", nRet); } curl_easy_cleanup(curl); return ((0 == nRet) && (responseCode == 200)) ? 0 : 1; } int DoPutRequest(const char* url, int authType, const char* userName, const char* password, net_handle_t netHandle, const char* contents, std::vector& data) { std::string auth; CURL *curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT"); curl_easy_setopt(curl, CURLOPT_URL, url); if (authType != HTTP_AUTH_TYPE_NONE) { if (userName != NULL && password != NULL && strlen(userName) > 0) { auth = userName; auth += ":"; auth += password; curl_easy_setopt(curl, CURLOPT_USERPWD, auth.c_str()); // DIGEST Auth if (authType == HTTP_AUTH_TYPE_BASIC) { curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); } else if (authType == HTTP_AUTH_TYPE_DIGEST) { curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); } } } if (netHandle != NETWORK_UNSPECIFIED) { curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, SockOptCallback); curl_easy_setopt(curl, CURLOPT_SOCKOPTDATA, &netHandle); } 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); CURLcode nRet = curl_easy_perform(curl); if (CURLE_OK != nRet) { printf("GET err=%d", nRet); } curl_easy_cleanup(curl); return (0 == nRet) ? 0 : 1; } bool requestCapture(uint8_t channel, uint8_t preset, const NET_PHOTO_INFO& photoInfo) { bool res = false; std::vector data; 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(), photoInfo.authType, userName, password, photoInfo.netHandle, 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& 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(), photoInfo.authType, userName, password, photoInfo.netHandle, img); return (0 == nRet); } namespace nc_hk { bool requestCapture(uint8_t channel, uint8_t preset, const NET_PHOTO_INFO& photoInfo, std::vector& 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(), photoInfo.authType, userName, password, photoInfo.netHandle, 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); } } namespace nc_ys { bool requestCapture(uint8_t channel, uint8_t preset, const NET_PHOTO_INFO& photoInfo, std::vector& 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(), photoInfo.authType, userName, password, photoInfo.netHandle, 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); } }