Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用C++;卷曲_C++_Curl_Request - Fatal编程技术网

C++ 使用C++;卷曲

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

我在端口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::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。好的,我需要定义写入函数,现在它工作得很好,谢谢!)