c - 如何使 libcurl 在每个 http 数据包中发送摘要身份验证 header ?

标签 c libcurl

我有以下函数http_send_message(),每次我想发送http消息时都会使用它:

http_send_message(char *msg_out, char **msg_in)
{
    CURLcode res;
    CURL *curl;

    curl = curl_easy_init();
    if (!curl) return -1;

    curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.1.133:8080/tawtaw");
    curl_easy_setopt(curl, CURLOPT_USERNAME, "tawtaw");
    curl_easy_setopt(curl, CURLOPT_PASSWORD, "tawtaw");
    curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC|CURLAUTH_DIGEST);
        .
        .
        .
   curl_easy_cleanup(curl); 
}

但我在每次函数发送 http 消息时都指出,它会尝试发送不带摘要身份验证 header 的请求,然后发送带有摘要身份验证 header 的请求。在正常情况下,它应该仅在第一条消息中执行此行为。对于后续消息,它应该记住身份验证 header 并在每条消息中发送

最佳答案

要获得这样的行为,您需要重新使用您的curl句柄以便后续调用充分利用持久连接和Digest Access Authentication请求计数器:

[...] the client may make another request, reusing the server nonce value (the server only issues a new nonce for each "401" response) but providing a new client nonce (cnonce). For subsequent requests, the hexadecimal request counter (nc) must be greater than the last value it used

实际上,不要清理你的卷发 handle 。相反,一旦您需要执行另一个请求,就维护它:

  • 使用curl_easy_reset重置它功能:curl_easy_reset(curl);
  • 然后重新设置您的选项。

如果您使用CURLOPT_VERBOSE选项,您将看到对于后续请求,您将获得 Authorization请求计数器不断增加的 header ( nc=00000002nc=00000003 等)。

关于c - 如何使 libcurl 在每个 http 数据包中发送摘要身份验证 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13759452/

相关文章:

c++ - 如何在 C++ 中用 UTF-8 字符解码 URI

c - 如何使用Curlpp写入json数据文件?

c - 为什么我总是得到8?

c - 在 C11 中是否未定义修改函数调用的结果,或者在下一个序列点之后访问它?

php - 如何编写一个简单的 PHP 透明代理?

php - cURL SSL PUT/POST (php) 的延迟响应(NSS 与 OpenSSL)

检查一个元素是否在数组中或者不是无限循环

c - 如何结合使用哈希和堆的数据结构

编译器不执行代码块中的程序

c++ - 如何判断 libcurl 是否正确处理了我的 SSL 文件