Chinaunix首页 | 论坛 | 博客
  • 博客访问: 290209
  • 博文数量: 34
  • 博客积分: 1400
  • 博客等级: 上尉
  • 技术积分: 433
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-23 13:16
文章分类

全部博文(34)

文章存档

2011年(3)

2010年(4)

2009年(6)

2008年(21)

我的朋友

分类: LINUX

2008-11-13 10:30:48

前些时间,由于找不到一个比较好使用的http库,自己封装了一个,不过时间紧迫,也没有完整分析HTTP协议,因此心里总不塌实地使用它,一次偶然的机会,让我在网上找到一个好用的http库 -- libghttp,目前的版本因该是libghttp-1.0.9.
 
这个库十分的方便使用,它能够轻松地实现同步和异步的Http请求。

简单使用实例:

#include
int main(int argc, char *argv[])
{
    char *uri = "";
    ghttp_request *request = NULL;
    ghttp_status status;
    char *buf;
    int bytes_read;
   
    request = ghttp_request_new();
    if(ghttp_set_uri(request, uri) == -1)
        exit(-1);
    if(ghttp_set_type(request, ghttp_type_get) == -1)
        exit(-1);
    ghttp_prepare(request);
    status = ghttp_process(request);
    if(status == ghttp_error)
        exit(-1);
    /* OK, done */
    printf("Status code -> %d\n", ghttp_status_code(request));
    buf = ghttp_get_body(loader->request);
    bytes_read = ghttp_get_body_len(loader->request);
    return 0;
}

异步请求实例:

#include
int main(int argc, char *argv[])
{
    char *uri = "";
    ghttp_request *request = NULL;
    ghttp_status status;
    char *buf;
    int bytes_read;
   
    request = ghttp_request_new();
    if(ghttp_set_uri(request, uri) == -1)
        exit(-1);
    if(ghttp_set_type(request, ghttp_type_get) == -1)
        exit(-1);
    /* NOTE: Set async request */
    ghttp_set_sync(request, ghttp_async);
    ghttp_prepare(request);
   
    while(1) {
        status = ghttp_process(request);
        if(status == ghttp_error)
            break;
        /* NOTE: buf may NULL, notice it */
        buf = ghttp_get_body(loader->request);
        bytes_read = ghttp_get_body_len(loader->request);
        if(status == ghttp_done) {
            /* NOTE: Ok, done */
            break;
        }
    }
    return 0;
}



文件:libghttp-1.0.9.tar.gz
大小:143KB
下载:下载

阅读(12393) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~