libcurl问题

linux_6 2010-10-24 08:23:29
用libcurl访问mezeo的webservice, 我看它的API上有一个example是这样写的:
Example curl command:
curl -u $cred -H ‘X-Client-Specification: 2’ –H ‘X-Cloud-Key: $Key’\
-X GET https://api.example.com/v2 | xml fo

现在它给的是用命令行的方式来发送请求,我现在用程序来发送请求,用的libcurl库该怎么实现啊?
...全文
327 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
mymtom 2010-10-26
  • 打赏
  • 举报
回复
楼主自己好好看看手册吧。
如果要用POST,可以用curl_easy_setopt(handle, CURLOPT_POST, 1);

http://curl.haxx.se/libcurl/c/curl_easy_setopt.html

#include <stdio.h>

#include <curl/curl.h>

int
main(int argc, char *argv[])
{
CURL *handle;
char url[] = "http://127.0.0.1:20001";
struct curl_slist *slist;


handle = curl_easy_init();
curl_easy_setopt(handle, CURLOPT_URL, url);
curl_easy_setopt(handle, CURLOPT_POST, 1);
slist = NULL;
slist = curl_slist_append(slist, "X-Client-Specification: 2");
slist = curl_slist_append(slist, "X-Cloud-Key: 0123456789ABCDEF");
curl_easy_setopt(handle, CURLOPT_HTTPHEADER, slist);
curl_easy_perform(handle);

curl_slist_free_all(slist);
return 0;
}


服务器受到的信息为

POST / HTTP/1.1
Host: 127.0.0.1:20001
Pragma: no-cache
Accept: */*
X-Client-Specification: 2
X-Cloud-Key: 0123456789ABCDEF
Content-Length: 0
Content-Type: application/x-www-form-urlencoded

linux_6 2010-10-26
  • 打赏
  • 举报
回复
高手快快现身啊
linux_6 2010-10-26
  • 打赏
  • 举报
回复
没人回音了?
linux_6 2010-10-26
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 mymtom 的回复:]

自己看看例子不好么:
C/C++ code

#include <stdio.h>

#include <curl/curl.h>

int
main(int argc, char *argv[])
{
CURL *handle;
char url[] = "http://127.0.0.1:20001";
struct cu……
[/Quote]

好想这样不行吧,你直接这样弄:
       handle = curl_easy_init();
curl_easy_setopt(handle, CURLOPT_URL, url);
slist = NULL;
slist = curl_slist_append(slist, "X-Client-Specification: 2");
slist = curl_slist_append(slist, "X-Cloud-Key: 0123456789ABCDEF");
curl_easy_setopt(handle, CURLOPT_HTTPHEADER, slist);
curl_easy_perform(handle);

服务器怎么会知道你是GET,PUT还是POST请求呢?
linux_6 2010-10-25
  • 打赏
  • 举报
回复
没人冒个泡吗?
linux_6 2010-10-25
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 mymtom 的回复:]

curl_easy_setopt

http://curl.haxx.se/libcurl/c/curl_easy_setopt.html

CURLOPT_HTTPHEADER

Pass a pointer to a linked list of HTTP headers to pass to the server in your HTTP request. The linked……
[/Quote]

这个我看过了啊,看了以后也不知道怎么弄啊?
mymtom 2010-10-25
  • 打赏
  • 举报
回复
自己看看例子不好么:

#include <stdio.h>

#include <curl/curl.h>

int
main(int argc, char *argv[])
{
CURL *handle;
char url[] = "http://127.0.0.1:20001";
struct curl_slist *slist;


handle = curl_easy_init();
curl_easy_setopt(handle, CURLOPT_URL, url);
slist = NULL;
slist = curl_slist_append(slist, "X-Client-Specification: 2");
slist = curl_slist_append(slist, "X-Cloud-Key: 0123456789ABCDEF");
curl_easy_setopt(handle, CURLOPT_HTTPHEADER, slist);
curl_easy_perform(handle);

curl_slist_free_all(slist);
return 0;
}

发送的信息为:

GET / HTTP/1.1
Host: 127.0.0.1:20001
Pragma: no-cache
Accept: */*
X-Client-Specification: 2
X-Cloud-Key: 0123456789ABCDEF

]



[Quote=引用 8 楼 linux_6 的回复:]

引用 7 楼 mymtom 的回复:

curl_easy_setopt

http://curl.haxx.se/libcurl/c/curl_easy_setopt.html

CURLOPT_HTTPHEADER

Pass a pointer to a linked list of HTTP headers to pass to the server in your HT……
[/Quote]
mymtom 2010-10-24
  • 打赏
  • 举报
回复
curl_easy_setopt

http://curl.haxx.se/libcurl/c/curl_easy_setopt.html

CURLOPT_HTTPHEADER

Pass a pointer to a linked list of HTTP headers to pass to the server in your HTTP request. The linked list should be a fully valid list of struct curl_slist structs properly filled in. Use curl_slist_append(3) to create the list and curl_slist_free_all(3) to clean up an entire list. If you add a header that is otherwise generated and used by libcurl internally, your added one will be used instead. If you add a header with no content as in 'Accept:' (no data on the right side of the colon), the internally used header will get disabled. Thus, using this option you can add new headers, replace internal headers and remove internal headers. To add a header with no content, make the content be two quotes: "". The headers included in the linked list must not be CRLF-terminated, because curl adds CRLF after each header item. Failure to comply with this will result in strange bugs because the server will most likely ignore part of the headers you specified.

The first line in a request (containing the method, usually a GET or POST) is not a header and cannot be replaced using this option. Only the lines following the request-line are headers. Adding this method line in this list of headers will only cause your request to send an invalid header.

Pass a NULL to this to reset back to no custom headers.
linux_6 2010-10-24
  • 打赏
  • 举报
回复
群里的高手呢?
linux_6 2010-10-24
  • 打赏
  • 举报
回复
没人回复,自己顶
linux_6 2010-10-24
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ww2000e 的回复:]

官网有很多程序例子
[/Quote]我知道啊?可是但就这个:
curl -u $cred -H ‘X-Client-Specification: 2’ –H ‘X-Cloud-Key: $Key’\
-X GET https://api.example.com/v2 | xml fo
该怎么发送http请求啊?
ww2000e 2010-10-24
  • 打赏
  • 举报
回复
官网有很多程序例子
linux_6 2010-10-24
  • 打赏
  • 举报
回复
没人回复? 甩郁闷
linux_6 2010-10-24
  • 打赏
  • 举报
回复
用libcurl库该怎么设置请求的url和请求的头部啊?

23,118

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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