我有以下函数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=00000002
、 nc=00000003
等)。
关于c - 如何使 libcurl 在每个 http 数据包中发送摘要身份验证 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13759452/