使用C中的卷曲不支持协议 [英] Unsupported Protocol using Curl in C

查看:114
本文介绍了使用C中的卷曲不支持协议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前的工作在所有需要使用C,使一个HTTP GET请求的项目。我试图做到这一点使用卷曲。不过,我得到,说

I am currently working on a project that requires using C to make an http get request. I am trying to do this using curl. However, I get a response that says

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

我不知道如果错误是由卷曲或来自服务器。这里是我的code,从例如code借来的:

I am not sure if the error is coming from curl or from the server. Here is my code, borrowed from example code:

#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;
}

任何提示/提示是值得欢迎的。

Any tips / hints are welcome.

推荐答案

卷曲(和的libcurl )给出了一个不支持的协议错误,当他们不能跨preT URL的协议部分。在你的情况,这意味着 HTTPS:,这是一个有点奇怪

curl (and libcurl) gives an unsupported protocol error when they can't interpret the protocol part of the URL. In your case that means https:, which is a bit odd.

首先检查你,你可以使用卷曲工具通过命令行来检索URL。

First check you can you use the curl tool from the command line to retrieve the URL.

卷曲-V 会给你的协议卷曲(因而列表libcurl的)将支持:

curl -V will give you a list of the protocols curl (and thus libcurl) will support:

$ 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库没有安装。内置

Check that https is there. It may be that your libcurl is not built against an SSL library or if it is that the SSL library is not installed.

最后这个页面上: http://curl.haxx.se/libcurl/c/example.html 你会注意到这是一个简单的例子HTTPS(第二个向下)。请确认您的的libcurl 的作品。如果是的话,我会找出你的程序在做不同的。如果没有,我会发现什么是错了你的的libcurl 安装。

Finally on this page: http://curl.haxx.se/libcurl/c/example.html you will note that is a simple https example (second one down). Please confirm that works with your libcurl. If so, I'd find out what your program is doing different. If not, I would find out what's wrong with your libcurl installation.

这篇关于使用C中的卷曲不支持协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆