当前位置: 首页 > 工具软件 > libghttp > 使用案例 >

libghttp

令狐经武
2023-12-01

这个库十分的方便使用,它能够轻松地实现同步和异步的Http请求。

 

src:

http://libghttp.sourcearchive.com/

 

example:

#include <ghttp.h>
int main(int argc, char *argv[])
{
    char *uri = "http://www.hao123.com";
    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(request);
    bytes_read = ghttp_get_body_len(request);
    return 0;
}

异步请求实例:
#include <ghttp.h>
int main(int argc, char *argv[])
{
    char *uri = "http://www.hao123.com";
    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(request);
        bytes_read = ghttp_get_body_len(request);
        if(status == ghttp_done) {
            /* NOTE: Ok, done */
            break;
        }
    }
    return 0;
}
 类似资料:

相关阅读

相关文章

相关问答