http post ok

This commit is contained in:
aozhiwei 2018-11-20 20:32:56 +08:00
parent ce7b882956
commit 1f3a5f7f32
4 changed files with 26 additions and 3 deletions

View File

@ -15,9 +15,20 @@ namespace a8
return size * nmemb;
}
bool Post(const char* url, std::string& response)
bool Post(const char* url, const std::string& content, std::string& response, const char* headers)
{
return true;
CURL *curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_HEADER, 0);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, 1);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
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);
return ok;
}
bool Get(const std::string& url, std::string& response, int timeout=10)
@ -32,5 +43,6 @@ namespace a8
curl_easy_cleanup(curl);
return ok;
}
}
}

View File

@ -5,7 +5,8 @@ namespace a8
{
namespace http
{
bool Post(const char* url, std::string& response);
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);
}
}

View File

@ -250,6 +250,15 @@ namespace a8
}
}
void XObject::ToUrlEncodeStr(std::string& data)
{
if (type_ == a8::XOT_OBJECT) {
for (auto& pair : *value_.object_value) {
data.append("&" + pair.first + "=" + a8::UrlEncode(pair.second->AsXValue().GetString()));
}
}
}
void XObject::JsonValueToXObject(Json::Value& json_val, a8::XObject& xobject)
{
Json::ValueType val_type = json_val.type();

View File

@ -43,6 +43,7 @@ namespace a8
bool ReadFromXmlFile(const std::string& filename);
bool ReadFromXmlString(const std::string& xmldata);
void ToJsonStr(std::string& data);
void ToUrlEncodeStr(std::string& data);
private:
void JsonValueToXObject(Json::Value& json_val, a8::XObject& xobject);