C++ 使用C++;卷曲
我在端口1234上有一个本地服务器,在那里我有几个端点。 从bash中,我可以使用以下方法获取值:C++ 使用C++;卷曲,c++,curl,request,C++,Curl,Request,我在端口1234上有一个本地服务器,在那里我有几个端点。 从bash中,我可以使用以下方法获取值: curl -v --noproxy localhost, localhost:1234/option_name -X GET 我想用C++中的CURL LIB复制这个命令。 我的代码是: enum RquestType {GET, POST}; std::string send_request(std::string port, std::string option_name, std::str
curl -v --noproxy localhost, localhost:1234/option_name -X GET
我想用C++中的CURL LIB复制这个命令。
我的代码是:
enum RquestType {GET, POST};
std::string send_request(std::string port, std::string option_name, std::string value, RequestType type)
{
auto handle = curl_easy_init();
std::string returnData = "";
if(handle)
{
std::stringstream ss;
ss << "localhost, localhost:" << port << "/" << option_name
curl_easy_setopt(handle, CURLOPT_NOPROXY, ss.str());
curl_easy_setopt(handle, CURLOPT_WRITEDATA, &returnData);
if(type == GET)
{
curl_easy_setopt(handle, CURLOPT_GET, 1);
}
else if(type == POST)
{
curl_easy_setopt(handle, CURLOPT_HTTPPOST, 1);
}
auto ret = curl_easy_perform(handle);
if(ret != CURLE_OK)
std::cout << "curl_easy_perform() failed: " << curl_easy_stderror(ret) << "\n"
curl_easy_cleanup(handle);
}
return returnData;
}
int main()
{
// Empty request should send available options
std::string ret = send_request("1234", "", "", GET);
std::cout << ret;
}
enum RquestType{GET,POST};
std::string发送请求(std::string端口、std::string选项名称、std::string值、RequestType类型)
{
自动句柄=curl_easy_init();
std::string returnData=“”;
if(句柄)
{
std::stringstream-ss;
ss您忘了设置URL。
相反,您将CURLOPT_PROXY设置为“localhost,localhost:1234/option_name”
CURLOPT_URL应该是您请求的URL(可能是localhost:1234/option_name)。您可能不需要CURLOPT_PROXY应该有CURLOPT_NOPROXY,我刚刚编辑了它。通过您的更改,它几乎可以工作,但现在无法写入std::string。好的,我需要定义写入函数,现在它工作得很好,谢谢!)