如何使用libcurl设置具有名称/值和文件的表单POST

时间:2016-05-15 04:03:56

标签: c++ libcurl

这是我的代码:

    CURL *curl;
    CURLcode res;
    std::string response;
    struct curl_httppost *formpost = NULL;
    struct curl_httppost *lastptr = NULL;
    struct curl_slist *headerlist = NULL;

    curl_global_init(CURL_GLOBAL_ALL);
    curl_formadd(&formpost,
                 &lastptr,
                 CURLFORM_COPYNAME, "curuserid",
                 CURLFORM_COPYCONTENTS, curuserid.toStdString().c_str(),
                 CURLFORM_END);

    curl_formadd(&formpost,
                 &lastptr,
                 CURLFORM_COPYNAME, "token",
                 CURLFORM_COPYCONTENTS, token.toStdString().c_str(),
                 CURLFORM_END);

    curl_formadd(&formpost,
                 &lastptr,
                 CURLFORM_COPYNAME, "macaddress",
                 CURLFORM_COPYCONTENTS, macaddress.toStdString().c_str(),
                 CURLFORM_END);

    curl_formadd(&formpost,
                 &lastptr,
                 CURLFORM_COPYNAME, "channelsrc",
                 CURLFORM_COPYCONTENTS, channelsrc.toStdString().c_str(),
                 CURLFORM_END);

    curl_formadd(&formpost,
                 &lastptr,
                 CURLFORM_COPYNAME, "bless",
                 CURLFORM_COPYCONTENTS, bless.toStdString().c_str(),
                 CURLFORM_END);

    curl_formadd(&formpost,
                 &lastptr,
                 CURLFORM_COPYNAME, "version",
                 CURLFORM_COPYCONTENTS, version.toStdString().c_str(),
                 CURLFORM_END);

    curl_formadd(&formpost,
                 &lastptr,
                 CURLFORM_COPYNAME, "deviceid",
                 CURLFORM_COPYCONTENTS, deviceid.toStdString().c_str(),
                 CURLFORM_END);


//    curl_formadd(&formpost, &lastptr,

//                 CURLFORM_PTRNAME, "image",
//                 CURLFORM_FILENAME, "sample.png",
//                 CURLFORM_CONTENTTYPE, "image/png",
////                 CURLFORM_PTRCONTENTS, image.c_str(),
////                 CURLFORM_CONTENTSLENGTH, image.size(),
//                 CURLFORM_BUFFERPTR, image.c_str(),
//                 CURLFORM_BUFFERLENGTH, image.size(),

//                 CURLFORM_END);

//    curl_formadd(&formpost, &lastptr,

//                 CURLFORM_PTRNAME, "imgdata\0",
//                 CURLFORM_FILENAME, imageName.c_str(),
//                 CURLFORM_CONTENTTYPE, "image/png",
//                 CURLFORM_BUFFERPTR, imageData,
//                 CURLFORM_BUFFERLENGTH, fileLength,
//                 CURLFORM_END);

    curl_formadd(&formpost, &lastptr,

                 CURLFORM_COPYNAME, "imgdata",
                 CURLFORM_FILENAME, imageName.c_str(),
                 CURLFORM_CONTENTTYPE, "application/octet-stream",
                 CURLFORM_BUFFERPTR, imageData,
                 CURLFORM_BUFFERLENGTH, fileLength,
                 CURLFORM_END);

    curl = curl_easy_init();
    /* initalize custom header list (stating that Expect: 100-continue is not
         wanted */
//    headerlist = curl_slist_append(headerlist, "Accept: text/xml");
//    headerlist = curl_slist_append(headerlist, "Depth: infinity");
//    headerlist = curl_slist_append(headerlist, "Connection: Keep-Alive");
//    headerlist = curl_slist_append(headerlist, "Content-Type: multipart/form-data");
    headerlist = curl_slist_append(headerlist, "Expect:***");
    headerlist = curl_slist_append(headerlist, "User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.4)");

    if(!curl)
        return NULL;

    qDebug() << host.toStdString().c_str();

    /* what URL that receives this POST */

    curl_easy_setopt(curl, CURLOPT_URL, host.toStdString().c_str());

    curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);

    curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
    curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 120);
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 160);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
    curl_easy_setopt(curl, CURLOPT_HEADER, headerlist);


    res = curl_easy_perform(curl);

    if(res != CURLE_OK)
        fprintf(stderr, "curl_easy_perform() failed: %s\n",curl_easy_strerror(res));

    curl_easy_cleanup(curl);
    curl_formfree(formpost);
    curl_slist_free_all(headerlist);

Q1:我不知道为什么我无法设置curl_header成功?

Q2:我已经设置了curl_form,但我找不到网络捕获的所有内容,没有内容,但我已经设置了

screenshot of network capture

0 个答案:

没有答案
相关问题