gpt4 book ai didi

c++ - libcurl 未初始化的变量 curl 错误

转载 作者:太空宇宙 更新时间:2023-11-04 13:16:23 25 4
gpt4 key购买 nike

我正在尝试发出 HTTP 请求来检索一些 JSON 数据;尽管我使用了 easy_init() ,但我得到了 curl 变量未初始化的错误。任何有关如何解决此错误的帮助都将非常友好!!

下面是我的代码:

#pragma once
#include "stdafx.h"
#include "RequestJson.h"
#include <string.h>
#include <include/curl/curl.h>
#include <fstream>
#include <iostream>
#include <sstream>


using namespace std;
class RequestJson
{
public:

static std::string RequestJsonString(std::string URL)
{
//set to get the JSON Response on listed loans; open a CSV file and read unemployment and other indices.

::CURL *curl;
CURLcode res;
struct curl_slist *headers = NULL;
std::ostringstream oss;

//curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
curl_slist_append(headers, "Accept: application/json");
curl_slist_append(headers, "Content-Type: application/json");
curl_easy_cleanup(curl);

if (curl)
{

curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_URL, URL.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer); //define a write-function below.
res = curl_easy_perform(curl);
if (CURLE_OK == res)
{
char *ct;
res = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);
if ((CURLE_OK == res) && ct)
{
return *DownloadedResponse;
}
}
}
}

//parse the JSON String and return the downloaded string.
static std::string *DownloadedResponse;
static int writer(char *data, size_t size, size_t nmemb, std::string *buffer_in)
{
if (buffer_in != NULL)
{
buffer_in->append(data, size * nmemb);
DownloadedResponse = buffer_in;
return size * nmemb;
}

return 0;
}


};

最佳答案

来自 the curl_easy_cleanup reference :

This function must be the last function to call for an easy session. It is the opposite of the curl_easy_init function and must be called with the same handle as input that a curl_easy_init call returned.

[强调我的]

当您调用 curl_easy_cleanup 时,它清理 curl_easy_init 分配的所有资源。之后你就不能再使用 CURL 指针了。

如引用资料所述:完成后将其放在最后

关于c++ - libcurl 未初始化的变量 curl 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37112341/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com