【问题标题】:Unsupported Protocol using Curl in C在 C 中使用 Curl 的不受支持的协议
【发布时间】:2014-02-06 19:27:07
【问题描述】:

我目前正在从事一个需要使用 C 来发出 http get 请求的项目。我正在尝试使用 curl 来做到这一点。但是,我收到一条回复说

error: unable to request data from https://coinex.pw/api/v2/currencies:
Unsupported protocol

我不确定错误是来自 curl 还是来自服务器。这是我的代码,借鉴自示例代码:

#include <curl/curl.h>

static char *request(const char *url)
{
CURL *curl = NULL;
CURLcode status;
struct curl_slist *headers = NULL;
char *data = NULL;
long code;

curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if(!curl)
    goto error;

data = malloc(BUFFER_SIZE);
if(!data)
    goto error;

struct write_result write_result = {
    .data = data,
    .pos = 0
};

curl_easy_setopt(curl, CURLOPT_URL, url);

headers = curl_slist_append(headers, "Content-type: application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_response);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &write_result);

status = curl_easy_perform(curl);
if(status != 0)
{
    fprintf(stderr, "error: unable to request data from %s:\n", url);
    fprintf(stderr, "%s\n", curl_easy_strerror(status));
    goto error;
}

curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
if(code != 200)
{
    fprintf(stderr, "error: server responded with code %ld\n", code);
    goto error;
}

curl_easy_cleanup(curl);
curl_slist_free_all(headers);
curl_global_cleanup();

/* zero-terminate the result */
data[write_result.pos] = '\0';

return data;

error:
if(data)
    free(data);
if(curl)
    curl_easy_cleanup(curl);
if(headers)
    curl_slist_free_all(headers);
curl_global_cleanup();
return NULL;
}

欢迎任何提示/提示。

【问题讨论】:

    标签: c http curl


    【解决方案1】:

    curl(和libcurl)在无法解释 URL 的协议部分时给出不受支持的协议错误。在你的情况下,这意味着https:,这有点奇怪。

    首先检查您是否可以使用命令行中的curl 工具来检索 URL。

    curl -V 将为您提供curl(因此libcurl)将支持的协议列表:

    $ curl -V
    curl 7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
    Protocols: dict file ftp ftps gopher http https imap imaps ldap pop3 pop3s rtmp rtsp smtp smtps telnet tftp 
    Features: GSS-Negotiate IDN IPv6 Largefile NTLM NTLM_WB SSL libz TLS-SRP 
    

    检查https 是否存在。可能是您的libcurl 不是针对 SSL 库构建的,或者是未安装 SSL 库。

    最后在这个页面上:http://curl.haxx.se/libcurl/c/example.html 你会注意到这是一个简单的 https 示例(第二个)。请确认适用于您的libcurl。如果是这样,我会找出你的程序在做什么不同。如果没有,我会找出你的libcurl 安装有什么问题。

    【讨论】:

    • 我在安装 curl 后安装了 open ssl。我重新制作了 curl 并且它有效。谢谢。
    【解决方案2】:

    尝试在您的网址中使用“http”但“https”。

    【讨论】:

    • 您的回答存在一些问题。 1. 它会创建与服务器的未加密连接,这并不理想,也可能不想要。 2. 并非所有服务器都允许未加密的 HTTP 连接。
    猜你喜欢
    • 2014-08-10
    • 1970-01-01
    • 2022-08-02
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    • 2011-10-16
    • 2013-12-05
    • 2015-09-11
    相关资源
    最近更新 更多