Chinaunix首页 | 论坛 | 博客
  • 博客访问: 370168
  • 博文数量: 181
  • 博客积分: 215
  • 博客等级: 民兵
  • 技术积分: 313
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-17 19:39
个人简介

王的男人

文章分类

全部博文(181)

文章存档

2016年(2)

2015年(35)

2014年(17)

2013年(84)

2012年(49)

我的朋友

分类:

2012-05-19 07:41:01

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

阅读(580) | 评论(0) | 转发(0) |
0

上一篇:linux下.vimrc文件配置

下一篇:string.c总结

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