添加headers
This commit is contained in:
parent
972852f335
commit
3d34fc3fbe
23
a8/curl.cc
23
a8/curl.cc
@ -15,7 +15,8 @@ namespace a8
|
||||
return size * nmemb;
|
||||
}
|
||||
|
||||
bool Post(const char* url, const std::string& content, std::string& response, const char* headers)
|
||||
bool Post(const char* url, const std::string& content, std::string& response, a8::XObject* headers,
|
||||
int timeout)
|
||||
{
|
||||
CURL *curl = curl_easy_init();
|
||||
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);
|
||||
@ -23,30 +24,42 @@ namespace a8
|
||||
curl_easy_setopt(curl, CURLOPT_HEADER, 0);
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPPOST, 1);
|
||||
curl_slist* list = nullptr;
|
||||
if (headers) {
|
||||
list = curl_slist_append(list, headers);
|
||||
if (headers && headers->GetType() == a8::XOT_ARRAY) {
|
||||
for (int i = 0; i < headers->Size(); ++i) {
|
||||
list = curl_slist_append(list, headers->At(i)->AsXValue().GetString().c_str());
|
||||
}
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
|
||||
}
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, content.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &_ReceiveResponse);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
|
||||
bool ok = curl_easy_perform(curl) == CURLE_OK;
|
||||
curl_easy_cleanup(curl);
|
||||
if (list) {
|
||||
curl_slist_free_all(list);
|
||||
}
|
||||
curl_easy_cleanup(curl);
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool Get(const std::string& url, std::string& response, int timeout=10)
|
||||
bool Get(const std::string& url, std::string& response, a8::XObject* headers, int timeout)
|
||||
{
|
||||
CURL *curl = curl_easy_init();
|
||||
curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
|
||||
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
|
||||
curl_slist* list = nullptr;
|
||||
if (headers && headers->GetType() == a8::XOT_ARRAY) {
|
||||
for (int i = 0; i < headers->Size(); ++i) {
|
||||
list = curl_slist_append(list, headers->At(i)->AsXValue().GetString().c_str());
|
||||
}
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
|
||||
}
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &_ReceiveResponse);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
|
||||
bool ok = curl_easy_perform(curl) == CURLE_OK;
|
||||
if (list) {
|
||||
curl_slist_free_all(list);
|
||||
}
|
||||
curl_easy_cleanup(curl);
|
||||
return ok;
|
||||
}
|
||||
|
@ -6,8 +6,8 @@ namespace a8
|
||||
namespace http
|
||||
{
|
||||
bool Post(const char* url, const std::string& content, std::string& response,
|
||||
const char* headers = nullptr);
|
||||
bool Get(const std::string& url, std::string& response, int timeout=10);
|
||||
a8::XObject* headers = nullptr, int timeout = 10);
|
||||
bool Get(const std::string& url, std::string& response, a8::XObject* headers = nullptr, int timeout=10);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user