[英]Accessing server with authentication using libcurl
我正在嘗試訪問需要使用 libcurl 進行身份驗證的公司內部網頁。 當代碼運行時,它顯示“401 UNAUTHORIZED”。 我通過 curl_easy_setopt(curl, CURLOPT_USERPWD, "usr:pwd") 提供登錄憑據,但仍然收到上述消息。
我讀了這個,發現 401 意味着它需要一個 SERVER 身份驗證。 但是,我沒有收到任何可用的身份驗證方案。 我的 winhttp 配置設置為“自動檢測”。
int wmain()
{
std::string content;
curl_global_init(CURL_GLOBAL_ALL);
CURL *curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, "company internal URL");
curl_easy_setopt(curl, CURLOPT_USERPWD, "usr:pwd");
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
CURLcode code = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
curl_global_cleanup();
std::cout << content;
std::cin.get();
return 0;
}
我對使用 libcurl 非常陌生,並且對 C/C++ 的工作經驗有限。 感謝您的幫助以確定問題。 謝謝!
嘗試添加curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
當然"usr:pwd"
應該被替換,但我假設你這樣做了。
下面的例子
https://curl.haxx.se/libcurl/c/CURLOPT_HTTPAUTH.html
CURL *curl = curl_easy_init();
if(curl) {
CURLcode ret;
curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/");
/* allow whatever auth the server speaks */
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_easy_setopt(curl, CURLOPT_USERPWD, "james:bond");
ret = curl_easy_perform(curl);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.