免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: mq110
打印 上一主题 下一主题

对CURL的一些研究 [复制链接]

论坛徽章:
0
31 [报告]
发表于 2009-06-11 21:17 |只看该作者

回复 #1 mq110 的帖子

补充一个抽取HTTP Header的例子,LZ别见怪哈:
  
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <curl/curl.h>
5 #include <curl/types.h>
6 #include <curl/easy.h>
7
8
9 size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
10 {
11     printf("%s--------------------------------------------------------\n",  ptr);
12     char* ptr1 = (char*)ptr;
13     if (ptr1[0] == '\r' || ptr1[1] == '\n')
14         return 0;
15
16     return nmemb;
17 }
18
19
20 int main(int argc, char *argv[])
21 {
22      CURL *curl;
23      curl_global_init(CURL_GLOBAL_ALL);
24      curl=curl_easy_init();
25      curl_easy_setopt(curl, CURLOPT_URL, argv[1]);
26
27      curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, write_data);   
28      curl_easy_perform(curl);
29      curl_easy_cleanup(curl);
30      
31      exit(0);
32 }


说明:在 cURL 网站的 curl_easy_setopt.3 -- man page 中可以看到如下字段:

CURLOPT_HEADERFUNCTION

Function pointer that should match the following prototype: size_t function( void *ptr, size_t size, size_t nmemb, void *stream);. This function gets called by libcurl as soon as it has received header data. The header callback will be called once for each header and only complete header lines are passed on to the callback. Parsing headers should be easy enough using this. The size of the data pointed to by ptr is size multiplied with nmemb. Do not assume that the header line is zero terminated! The pointer named stream is the one you set with the CURLOPT_WRITEHEADER option. The callback function must return the number of bytes actually taken care of , or return -1 to signal error to the library (it will cause it to abort the transfer with a CURLE_WRITE_ERROR return code).

  • 其中红色字段表明,即使使用了 CURLOPT_HEADERFUNCTION 标志,也不代表传给回调函数的仅仅是 Http Header部分的缓冲区,而是带 Http Header 的整个 response 报文缓冲区,所以在会调用函数中用 \r\n 来判断 Http Header 的结束。
  • 其中蓝色字段表明,回调函数应该返回实际传给回到函数的缓冲区总字节数 size * memb,否则回调函数第一次被调用后,就会失败。


[ 本帖最后由 frank_seng 于 2009-6-11 21:38 编辑 ]

论坛徽章:
0
32 [报告]
发表于 2009-11-17 12:52 |只看该作者
绝对好用,我用php的curl写过机器人,估计用c写效率更高,谢谢谢谢。。。

论坛徽章:
0
33 [报告]
发表于 2009-11-18 22:40 |只看该作者
又顶起来了?我再顶顶。。。。看来要多学习学习了!!!

论坛徽章:
0
34 [报告]
发表于 2011-08-23 15:17 |只看该作者
本帖最后由 herocsz 于 2011-08-23 15:19 编辑

写的不错。
有curl发送form表单的吗?

网页源码发送函数loginForm.submit();

不知道用curl如何发送?

论坛徽章:
0
35 [报告]
发表于 2011-10-22 13:00 |只看该作者
回复 1# mq110


    谢谢。。。。

论坛徽章:
0
36 [报告]
发表于 2012-07-12 17:46 |只看该作者
问下楼主,像002.c,
如果参数是www.baidu.com就可以保存页面内容
如果入参是http://news.sohu.com/20101201/n278039496.shtml 就下载不了页面。这是为什么呢?想要下载这页面该怎么办?

论坛徽章:
208
巨蟹座
日期:2013-09-02 09:16:36卯兔
日期:2013-09-02 20:53:59酉鸡
日期:2013-09-05 21:21:45戌狗
日期:2013-10-15 20:51:17寅虎
日期:2013-10-18 21:13:16白羊座
日期:2013-10-23 21:15:19午马
日期:2013-10-25 21:22:48技术图书徽章
日期:2013-11-01 09:11:32双鱼座
日期:2013-11-01 20:29:44丑牛
日期:2013-11-01 20:40:00卯兔
日期:2013-11-11 09:21:32酉鸡
日期:2013-12-04 19:56:39
37 [报告]
发表于 2012-07-13 09:03 |只看该作者
有些失望,以为是对源代码的理解,却只是应用

论坛徽章:
0
38 [报告]
发表于 2013-02-28 15:59 |只看该作者
有代理相关的总结吗?可以说说代理,多谢楼主了

论坛徽章:
0
39 [报告]
发表于 2013-04-16 21:04 |只看该作者
好帖子!还想请教LZ一些问题,不知道你还上不上论坛...
请问cURL可以实现http包的接收分片和写入缓存区吗?应该如何实现呢?
谢谢!

论坛徽章:
0
40 [报告]
发表于 2013-04-16 21:06 |只看该作者
回复 31# frank_seng

你好!
请问cURL可以实现http包的接收分片和写入缓存区吗?应该如何实现呢?
谢谢!

   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP