如何从C 中的卷曲中获取原始数据
How to obtain raw data from curl in C++
我正在使用curl库(https://curl.haxx.se/download.html(,我想读取特定 pastebin的原始文本(RAW((链接,然后将原始数据存储在称为"密码"的字符串中,但它不起作用。它仅获取数据" test12345" (从下面的代码中的链接中(并出于某种原因将其打印出来,但不允许我将其存储在"密码"变量中。
我认为我在做错事,但我不确定。
这是问题的屏幕截图:
它在没有std :: cout的情况下输出原始数据,但不会将其存储在"密码"变量中。我真的很困惑
这是我的代码:
#define CURL_STATICLIB
#include <curl/curl.h>
#include <iostream>
#include <string>
using std::string;
int main()
{
CURL *curl = curl_easy_init();
string password;
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://pastebin.com/raw/95W9vsvR");
curl_easy_setopt(curl, CURLOPT_READDATA, &password);
curl_easy_perform(curl);
}
/*std::cout << password;*/
}
如Super提到的,也许您应该更详细地阅读卷曲文档。在您的情况下,您不能依靠默认回调函数,因为该默认函数将将用户数据指针解释为文件*指针。
因此,您需要提供自己的回调功能,该功能应将Web数据附加到C 字符串对象。
下面的代码似乎可以按照您想要的工作:
#include <curl/curl.h>
#include <iostream>
#include <string>
size_t write_callback(const char* ptr, size_t size, size_t nc, std::string* stp)
{
if (size != 1) {
std::cerr << "write_callback() : unexpected size value: " <<
size << std::endl;
}
size_t initialLength = stp->length();
size_t finalLength = initialLength;
stp->append(ptr, nc); // REAL WORK DONE HERE
finalLength = stp->length();
// must return the actual gain:
return (finalLength - initialLength);
}
size_t write_callback_shim(char* ptr, size_t size, size_t nmemb, void *userdata)
{
// just a place for the cast
size_t rc = 0;
std::string* stp = reinterpret_cast<std::string*>(userdata);
rc = write_callback(ptr, size, nmemb, stp);
return rc;
}
int main(int argc, const char* argv[])
{
CURL* curl = curl_easy_init();
std::string password;
if (curl == nullptr) {
std::cerr << "curl pointer is null." << std::endl;
}
else {
curl_easy_setopt(curl, CURLOPT_URL, "https://pastebin.com/raw/95W9vsvR");
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &password);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback_shim);
curl_easy_perform(curl);
}
std::cout << "Password set to: " << password << std::endl;
}
相关文章:
- 尝试通过OCI例程从Oracle获取blob数据,但出现错误:ORA-01008:并非所有变量都绑定
- 如何获取C++字符数据类型的地址
- 是否可以从 OpenGL 缓冲区获取原始大小的像素?
- 获取文件数据预处理器宏
- C++:将值赋值给原始数据类型(例如布尔值)是原子操作吗?
- 在哪里可以从Android框架中获取原始的PCM数据
- 如何从C 中的卷曲中获取原始数据
- opencv::Mat,从原始数据中获取像素值
- 如何使用JNI + OpenCV + NDK获取原始的android相机传感器图像数据
- c++从hbitmap获取原始像素数据
- 如何获取指向原始数据的std::set指针
- 如何获取 IPMI FRU 原始数据
- 如何使用 C/C++获取原始相机数据
- 从Qt应用程序获取原始数据包数据
- 如何在Windows中从USB HID设备获取原始描述符数据
- 如何获取指向原始数据的std::deque指针
- 是否有一个库可以从数据包的winpcap原始数据中获取ip地址和端口?
- 如何获取指向原始数据的std::vector指针
- 获取指向集合中的原始数据的指针,例如 &(向量[0])
- 从QImage获取原始数据