gpt4 book ai didi

c++ - 使用 libcurl 和 C++ 将图片上传到受密码保护的目录

转载 作者:行者123 更新时间:2023-11-30 03:24:02 26 4
gpt4 key购买 nike

我正在尝试使用带有 libcurl 的 C++ 应用程序将图片上传到我的服务器,我服务器中的文件是一个 protected 目录,我尝试了我所知道的一切,但没有成功。

struct curl_slist *headerlist = NULL;
const char * Picture = "C:\xxxx";

FILE *fd = fopen(Picture, "rb");
struct stat file_info;
fstat(fileno(fd), &file_info);

headerlist = curl_slist_append(headerlist, "user : xxxx");
headerlist = curl_slist_append(headerlist, "password : xxxxx");
headerlist = curl_slist_append(headerlist, "Content-Type: image/jpeg");


curl = curl_easy_init();

curl_easy_setopt(curl, CURLOPT_URL, "http://xxxx.000webhostapp.com/Pictures");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_READDATA, fd);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (curl_off_t)file_info.st_size);

res = curl_easy_perform(curl);

if (res != CURLE_OK)
{

MessageBox::Show("Picture Failed");

}


curl_easy_cleanup(curl);

curl_global_cleanup();

最佳答案

userpassword 不是 HTTP header ,因此您不应该通过 CURLOPT_HTTPHEADER 传递它们.使用 CURLOPT_USERNAMECURLOPT_PASSWORD反而。另请参阅 CURLOPT_HTTPAUTH .

试试这个:

curl_global_init(CURL_GLOBAL_ALL);

...

const char *Picture = "C:\\xxxx";

FILE *fd = fopen(Picture, "rb");
if (!fd)
{
MessageBox::Show("Cannot Open Picture File");
}
else
{
struct stat file_info;
fstat(fileno(fd), &file_info);

curl = curl_easy_init();
if (!curl)
{
MessageBox::Show("Cannot Initialize CURL Session");
}
else
{
struct curl_slist *headerlist = curl_slist_append(NULL, "Content-Type: image/jpeg");

curl_easy_setopt(curl, CURLOPT_URL, "http://xxxx.000webhostapp.com/Pictures");
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_easy_setopt(curl, CURLOPT_USERNAME, "xxxx");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "xxxxx");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_READDATA, fd);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (curl_off_t)file_info.st_size);

res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
MessageBox::Show("Picture Failed");
}

curl_easy_cleanup(curl);
}

fclose(fd);
}

...

curl_global_cleanup();

关于c++ - 使用 libcurl 和 C++ 将图片上传到受密码保护的目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50052145/

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