【问题标题】:Use curl with a webpage with no security protocol对没有安全协议的网页使用 curl
【发布时间】:2020-05-21 18:08:10
【问题描述】:

我有一个网页:10.1.1.165,我正在尝试从中获取数据。我已经在 linux 中构建了这个程序,它运行良好,但是,在为 windows 系统开发时,我遇到了问题。

    if (filename.substr(filename.length() - 4, 4) == "html" || filename.substr(filename.length() - 3, 3) == "htm") 
    {
        CURL *curl;
        CURLcode res;
        curl_global_init(CURL_GLOBAL_DEFAULT);
        curl = curl_easy_init();
        string s;

        if (curl)
        {
            curl_easy_setopt(curl, CURLOPT_URL, filename.c_str());

            curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
            curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);

            if (gLogger->isDebug()) curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); 

            curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout/1000);

                //curl_easy_setopt (curl, CURLOPT_VERBOSE, 1L); //remove this to disable verbose output


            // Perform the request, res will get the return code 
            res = curl_easy_perform(curl);

            // Check for errors 
            if (res != CURLE_OK)
            {
                while (res == CURLE_OPERATION_TIMEDOUT)
                {
                    res = curl_easy_perform(curl);
                }

                if (res != CURLE_OK)
                {
                    gLogger->error("curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
                    curl_easy_cleanup(curl);
                    return -1;
                }
            }
            curl_easy_cleanup(curl); 

在我的 linux 系统上,这非常有效。在我的 Windows 系统上,我得到: curl_easy_perform() failed: Unsupported protocol。如果我访问我的网页 10.1.1.165,我会看到它是在没有安全性的情况下托管的。我无法更改该网页的托管方式。

根据此资源:https://curl.haxx.se/libcurl/c/CURLOPT_PROTOCOLS.html

libcurl 默认接受所有协议

有谁知道为什么 curl 不喜欢我在 windows 上抓取这个网页?如果是这样,我可能需要调整哪些设置?

【问题讨论】:

  • 添加这一行不会改变任何东西:curl_easy_setopt(curl, CURLOPT_PROTOCOLS, CURLPROTO_ALL);
  • filename.substr(filename.length() - 4, 4) == "html" 看起来不像 C
  • curl_easy_setopt(curl, CURLOPT_URL, filename.c_str()) - filename.c_str() 的值是多少?
  • @qrdl 是的,抱歉,这是嵌入在 c++ 程序中的。
  • @qrdl 文件名 = "10.1.1.165/index.htm"。这是有效的,适用于我的 linux 机器。

标签: c linux windows curl protocols


【解决方案1】:

我已经通过切换到 curl-7.55.1 解决了我的问题。以前我使用 curl-7.68.0。我不确定该功能是否已被删除,或者是否涉及更多使用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-28
    • 2017-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多