c++ - 使用 libcurl 和 ntml 时出现 http 407 错误

标签 c++ c curl libcurl ntlm

我正在开发一个小型应用程序。 为了测试目的,我尝试在 ntlm 代理后面使用 libcurl 来“ping”google。 这是我的 C++ 代码:

CURLcode testConnection(void)
{
    CURL *curl;
    CURLcode res = CURLE_OK;
    res = curl_global_init(CURL_GLOBAL_ALL);
    curl = curl_easy_init();
    if (curl)
    {
        cout << "Url: " << curl_easy_strerror(curl_easy_setopt(curl, CURLOPT_URL, "http://www.google.com")) << "\n";
        cout << "T-out: " << curl_easy_strerror(curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3)) << "\n";
        cout << "No_body: " << curl_easy_strerror(curl_easy_setopt(curl, CURLOPT_NOBODY, true)) << "\n";
        cout << "Proxy Url: " << curl_easy_strerror(curl_easy_setopt(curl, CURLOPT_PROXY, "proxyrm.wind.root.it")) << "\n";
        cout << "Proxy Port: " << curl_easy_strerror(curl_easy_setopt(curl, CURLOPT_PROXYPORT, 8080)) << "\n";
        cout << "Ntml: " << curl_easy_strerror(curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_NTLM)) << "\n";
        cout << "Verbose: " << curl_easy_strerror(curl_easy_setopt(curl, CURLOPT_VERBOSE, true)) << "\n";

        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
    }
    else
        res = CURLE_FAILED_INIT;
    curl_global_cleanup();
    return res;
}

一切都很顺利,因为详细输出如下。

Url: No error
T-out: No error
No_body: No error
Proxy Url: No error
Proxy Port: No error
Ntml: No error
Verbose: No error
* About to connect() to proxy myroxy  port 8080 (#0)
*   Trying IP...
* Connected to myroxy   (IP) port 8080 (#0)
> HEAD http://www.google.com HTTP/1.1
Host: www.google.com
Accept: */*
Proxy-Connection: Keep-Alive

< HTTP/1.1 407 Proxy Authentication Required
< Proxy-Authenticate: NTLM
< Proxy-Authenticate: BASIC realm="windroot"
< Cache-Control: no-cache
< Pragma: no-cache
< Content-Type: text/html; charset=utf-8
* HTTP/1.1 proxy connection set close!
< Proxy-Connection: close
< Set-Cookie: BCSI-CS-602d36a7505d346e=2; Path=/
< Connection: close
< Content-Length: 989
<
* Closing connection 0

Curl Final: No error

但是,如果我尝试 ping https://google.com结果就变成这样了

Url: No error
T-out: No error
No_body: No error
Proxy Url: No error
Proxy Port: No error
Ntml: No error
Ntml: No error
Ntml: No error
Verbose: No error
* About to connect() to proxy myroxy port 8080 (#0
*   Trying IP...
* Connected to myroxy  (IP) port 8080 (#0)
* Establish HTTP proxy tunnel to www.google.com:443
> CONNECT www.google.com:443 HTTP/1.1
Host: www.google.com:443
Proxy-Connection: Keep-Alive

< HTTP/1.1 407 Proxy Authentication Required
< Proxy-Authenticate: NTLM
< Proxy-Authenticate: BASIC realm="windroot"
< Cache-Control: no-cache
< Pragma: no-cache
< Content-Type: text/html; charset=utf-8
< Proxy-Connection: close
< Set-Cookie: BCSI-CS-602d36a7505d346e=2; Path=/
< Connection: close
< Content-Length: 1135
<
* Ignore 1135 bytes of response-body
* Received HTTP code 407 from proxy after CONNECT
* Connection #0 to host myroxy left intact

Curl Final: Failure when receiving data from the peer

在命令行中使用curl允许我ping https(我必须指定-k参数),但我不知道这是否真的相关。有人可以帮我弄清楚发生了什么事吗?如何避免这种情况?

最佳答案

已解决:我错过了这条指令

curl_easy_setopt(ctx, CURLOPT_PROXYUSERPWD, ":");

这(我认为)告诉curl使用通过ntlm auth从SO检索的用户名和密码

关于c++ - 使用 libcurl 和 ntml 时出现 http 407 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28587817/

相关文章:

c++ - printf 会导致未定义的行为吗?

php - 推特过滤器流中不允许参数计数?

php - 什么是 "Request Headers From Upload Stream"

c++ - 在 C++11 中,如何在 vector 上调用 std::max?

c - 任何 memcpy() 实现是否使用多个处理器寄存器?

c - 如何在c编程中将用户输入与一个while循环的字符串进行比较

json - curl 到 libcurl http put json 格式的请求

c++ - 如何使用 c/c++ 将用户输入放入 SQL 语句中的变量

c++ - 就地数据操作

c++ - 为什么我不能像预期的那样初始化 long long?