简体   繁体   English

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

[英]Uploading a picture to a Password Protected Directory using libcurl and C++

I'm trying to upload pictures using c++ application with libcurl to my server, the file in my server is a protected directory, I tried everything I know but it didn't work. 我正在尝试使用带有libcurl的c ++应用程序将图片上传到我的服务器,我服务器中的文件是受保护的目录,我尝试了我所知道的一切,但是没有用。

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();

user and password are not HTTP headers, so you should not be passing them via CURLOPT_HTTPHEADER . userpassword不是HTTP标头,因此您不应通过CURLOPT_HTTPHEADER传递它们。 Use CURLOPT_USERNAME and CURLOPT_PASSWORD instead. 请改用CURLOPT_USERNAMECURLOPT_PASSWORD Also look at CURLOPT_HTTPAUTH . 还要看CURLOPT_HTTPAUTH

Try this: 尝试这个:

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM