【问题标题】:Libcurl C++ Ldap RequestLibcurl C++ Ldap 请求
【发布时间】:2021-08-11 13:41:59
【问题描述】:

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

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

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

我的代码为 sn-p:

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

我感谢每一个帮助! 感谢您抽出宝贵时间阅读本文。

【问题讨论】:

  • 使用 --libcurl 命令行选项让 C 代码执行与 curl 命令相同的操作。

标签: c++ ldap libcurl


【解决方案1】:

您不能将所有内容都塞入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");

【讨论】:

  • 非常感谢!您的解决方案是对的。
  • @Elleshar 如果此答案对您有所帮助,请考虑将其标记为accepted。这样做也可以帮助其他试图解决相同问题的人。
  • @IWonderWhatThisAPI谢谢,完成。
猜你喜欢
  • 2011-05-13
  • 2014-02-26
  • 2012-08-12
  • 2023-01-13
  • 2014-06-05
  • 1970-01-01
  • 2021-11-22
  • 1970-01-01
  • 2015-02-28
相关资源
最近更新 更多