Libcurl C++ Ldap 请求

Libcurl C++ Ldap Request

我是 cUrl 的新手。 我需要做的是向 ldap 服务器发出 curl 请求。

到目前为止,请求在命令行上运行良好:
$curl ldap://XXXXXXXXX:389/DC=XXXXXXXXX,DC=DE?mail?sub?(sAMAccountName=XXXXXXXXX) --user s_ad_XXXXXXXXX:Password

尝试在我的 C++ CMake 项目中完成此操作后。我从 libcurl:
libcurl: (39) LDAP local: ldap_search_ext 错误的搜索过滤器

我的代码片段:

std::string readBuffer;
CURL* curl = curl_easy_init();
if (curl) {
        CURLcode res;
        char errbuf[CURL_ERROR_SIZE];
        curl_easy_setopt(curl, CURLOPT_URL, "ldap://XXXXXXXXX:389/DC=XXXXXXXXX,DC=DE?mail?sub?(sAMAccountName=XXXXXXXXX) --user s_ad_XXXXXXXXX:Password");

        curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
        errbuf[0] = 0;

        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);        // this line prints into console, what curl is trying to do
        curl_easy_setopt(curl, CURLOPT_FORBID_REUSE, 1L);   // closes the connection, if not here the connection can be used more than once

        // curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
        
        res = curl_easy_perform(curl);

        std::cout << res << std::endl;

        curl_easy_cleanup(curl);

        std::cout << readBuffer << std::endl;

        if (res != CURLE_OK) {
            size_t len = strlen(errbuf);
            fprintf(stderr, "\nlibcurl: (%d) ", res);
            if (len)
                fprintf(stderr, "%s%s", errbuf,
                    ((errbuf[len - 1] != '\n') ? "\n" : ""));
            else
                fprintf(stderr, "%s\n", curl_easy_strerror(res));
        }
}

我的输出:

Test curl library
*   Trying XXXXXXXXXXXXXX
* TCP_NODELAY set
* Connected to XXXXXXXXXXXXXX
* LDAP local: ldap://XXXXXXXXX:389/DC=XXXXXXXXX,DC=DE?mail?sub?(sAMAccountName=XXXXXXXXX) --user s_ad_XXXXXXXXX:Password
* LDAP local: ldap_search_ext Bad search filter
* Closing connection 0
39


libcurl: (39) LDAP local: ldap_search_ext Bad search filter

我愿意提供一切帮助! 感谢您花时间阅读本文。

您不能将所有内容都塞进 CURLOPT_OPT_URL 字段。 --user 位应分隔为 separate parameter:

curl_easy_setopt(curl, CURLOPT_URL, "ldap://XXXXXXXXX:389/DC=XXXXXXXXX,DC=DE?mail?sub?(sAMAccountName=XXXXXXXXX)");
curl_easy_setopt(curl, CURLOPT_USERPWD, "s_ad_XXXXXXXXX:Password");