Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4729444
  • 博文数量: 930
  • 博客积分: 12070
  • 博客等级: 上将
  • 技术积分: 11448
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-15 16:57
文章分类

全部博文(930)

文章存档

2011年(60)

2010年(220)

2009年(371)

2008年(279)

分类: LINUX

2010-04-20 10:17:08

前些时间,由于找不到一个比较好使用的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
下载: 下载

阅读(2738) | 评论(1) | 转发(1) |
0

上一篇:ftw遍历目录树

下一篇:mysql中文排序

给主人留下些什么吧!~~

chinaunix网友2010-06-01 06:28:20

MBT is based on Africa's indigenous people to create innovative walking barefoot, and wear this shoe can stimulate the body to relax the muscles so that the muscles of heat to be released, consumed calories, thus to achieve weight loss results. MBT's pursuit of beauty in order to satisfy customers, design a variety of styles, such as Mbt Chapa shoe, MBT Lami Shoe