求大佬解决关于curl库的问题

欲儿 2019-08-19 12:23:04
最近C++遇到了点麻烦,我用curl发送get请求,收到的结果却没有办法存入string,编译器提示缺少从curlcode到string的转换。。 代码如下: #include <iostream> #include <string> #include "curl/curl.h" using namespace std; int main() { system("chcp 65001"); CURL *hnd = curl_easy_init(); curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "GET"); curl_easy_setopt(hnd, CURLOPT_URL, "www.anyuer.club"); struct curl_slist *headers = NULL; headers = curl_slist_append(headers, "Postman-Token: 0c5393e1-a955-4e62-a5ec-6658eadc57dd"); curl_easy_setopt(hnd, CURLOPT_HEADER, 1); CURLcode ret = curl_easy_perform(hnd); string aa = ret;//就是这里编译器提示无法转换 while (1); return 0; }
...全文
221 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
欲儿 2019-08-19
  • 打赏
  • 举报
回复
虽然各位大佬,答案给的有点多,但是问题却成功解决,所以我结贴咯
CppCoder 2019-08-19
  • 打赏
  • 举报
回复
引用 4 楼 欲儿 的回复:
那如果我要截取get请求返回的数据我该如何操作呢
可以参考这个例子(getinmemory.c)
  • 打赏
  • 举报
回复
引用 5 楼 欲儿 的回复:
可是这里面并没有把接受到的数据转换为string类型呀,。……难道是我没看见吗
这是是char* 你如果想转行成string那就直接等于就好了变量叫ct 上面程序的第20行,这句输出的内容

 printf("We received Content-Type: %s\n", ct);
CppCoder 2019-08-19
  • 打赏
  • 举报
回复
引用 4 楼 欲儿 的回复:
[quote=引用 1 楼 CppCoder 的回复:]

typedef enum {
  CURLE_OK = 0,
  CURLE_UNSUPPORTED_PROTOCOL,    /* 1 */

  // 此处省略N个字
                                    
  CURL_LAST /* never use! */
} CURLcode;
CURLcode 是枚举类型,不能直接转为string
那难道就没有办法转换为string吗,那如果我要截取get请求返回的数据我该如何操作呢[/quote]

/*
 * NAME curl_easy_strerror()
 *
 * DESCRIPTION
 *
 * The curl_easy_strerror function may be used to turn a CURLcode value
 * into the equivalent human readable error string.  This is useful
 * for printing meaningful error messages.
 */
CURL_EXTERN const char *curl_easy_strerror(CURLcode);
curl_easy_strerror 这个可以根据CURLcode返回中文
欲儿 2019-08-19
  • 打赏
  • 举报
回复
引用 3 楼 zjq9931的回复:
官网的示例

#include <stdio.h>
#include "curl/curl.h"

int main(void)
{
	CURL *curl;
	CURLcode res;

	curl = curl_easy_init();
	if(curl) {
		curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com/");
		res = curl_easy_perform(curl);

		if(CURLE_OK == res) {
			char *ct;
			/* ask for the content-type */ 
			res = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);

			if((CURLE_OK == res) && ct)
				printf("We received Content-Type: %s\n", ct);
		}
		else
		{
			printf("Result = %d", res);
		}

		/* always cleanup */ 
		curl_easy_cleanup(curl);
	}
	return 0;
}
可是这里面并没有把接受到的数据转换为string类型呀,。……难道是我没看见吗
欲儿 2019-08-19
  • 打赏
  • 举报
回复
引用 1 楼 CppCoder 的回复:

typedef enum {
  CURLE_OK = 0,
  CURLE_UNSUPPORTED_PROTOCOL,    /* 1 */

  // 此处省略N个字
                                    
  CURL_LAST /* never use! */
} CURLcode;
CURLcode 是枚举类型,不能直接转为string
那难道就没有办法转换为string吗,那如果我要截取get请求返回的数据我该如何操作呢
  • 打赏
  • 举报
回复
官网的示例

#include <stdio.h>
#include "curl/curl.h"

int main(void)
{
	CURL *curl;
	CURLcode res;

	curl = curl_easy_init();
	if(curl) {
		curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com/");
		res = curl_easy_perform(curl);

		if(CURLE_OK == res) {
			char *ct;
			/* ask for the content-type */ 
			res = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);

			if((CURLE_OK == res) && ct)
				printf("We received Content-Type: %s\n", ct);
		}
		else
		{
			printf("Result = %d", res);
		}

		/* always cleanup */ 
		curl_easy_cleanup(curl);
	}
	return 0;
}
CppCoder 2019-08-19
  • 打赏
  • 举报
回复
具体使用方法可以参考CURL的示例程序 ..\docs\examples 其中httpcustomheader.c 这个例子可能比较适合你的需求
CppCoder 2019-08-19
  • 打赏
  • 举报
回复

typedef enum {
  CURLE_OK = 0,
  CURLE_UNSUPPORTED_PROTOCOL,    /* 1 */

  // 此处省略N个字
                                    
  CURL_LAST /* never use! */
} CURLcode;
CURLcode 是枚举类型,不能直接转为string

64,645

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧