libcurl未初始化变量curl错误
libcurl uninitialized variable curl error
我正在尝试发出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;
}
};
来自curl_easy_cleanup
参考:
此函数必须是最后一个函数,才能调用轻松会话。它与curl_easy_init函数相反,必须使用与curl_asy_init调用返回的输入相同的句柄进行调用。
[强调矿]
当您调用curl_easy_cleanup
时,会清理curl_easy_init
分配的所有资源。之后就不能再使用CURL
指针了。
正如参考资料所说:当你完成时,把它放在最后。
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 从 C++ 调用 cURL 命令会返回意外的错误代码,如 1792 和 6656
- C++ CURL 库链接器错误,但包含正确的库
- C++ 使用 Curl 库编程 - 抛出错误"protocol https not supported or disabled."
- CURL 不会返回正确的错误代码
- 在多线程中使用curl句柄的错误工作方法的替代方法
- libcurl未初始化变量curl错误
- Curl-openssl编译会产生大量链接器错误
- 错误 C2061:语法错误:使用 cURL 时标识符'acosf'
- 当发生错误(拒绝访问)时调用Curl-headerfunction和write函数
- CURL ilink32错误:未解决的外部
- 多线程curl请求的段错误
- c++ CURL post文件然后得到响应错误
- 在将cURL库加载到Qt5后出现错误
- C++ cURL 链接错误"unresolved external symbol _curl_easy_"
- qt c++错误:curl/curl.h:没有这样的文件或目录
- 在编译使用curl的代码时,在c++中获得错误