ibghttp是一個很好用的 http 庫,能夠輕松地實現同步和異步的HTTP請求
目錄[隱藏] |
安裝
庫文件下載:藝搜下載
在64位機器下configure時出現錯誤信息:
...
checking host system type... Invalid configuration `x86_64-unknown-linux-gnu': machine `x86_64-unknown' not recognized checking build system type... Invalid configuration `x86_64-unknown-linux-gnu': machine `x86_64-unknown' not recognized ... ltconfig: you must specify a host type if you use `--no-verify' Try `ltconfig --help' for more information. configure: error: libtool configure failed ...
即configure無法識別系統的類型, 所以提示you must specify a host type.
解決方法:
用 /usr/share/libtool/config/config.guess 覆蓋源碼包中的config.guess
cp /usr/share/libtool/config/config.guess ./config.guess
用 /usr/share/libtool/config/config.sub 覆蓋源碼包中的 config.sub
cp /usr/share/libtool/config/config.sub ./config.sub
這樣configure就可以猜出系統的類型了.
開始安裝:
./configure --prefix=/usr/local make sudo make install
GET示例
/*
* libghttp_get.c
* http get test
* Created on: 2013年10月25日
* Author: elesos.com
*/
#include <stdio.h> #include <string.h> #include <ghttp.h> int main(int argc, char **argv) { char *uri = "http://www.elesos.com/%E9%A6%96%E9%A1%B5"; ghttp_request *request = NULL; ghttp_status status; FILE * pFile; char *buf; int bytes_read; int size; pFile = fopen ( "elesos.html" , "wb" ); request = ghttp_request_new(); if(ghttp_set_uri(request, uri) == -1) return -1; if(ghttp_set_type(request, ghttp_type_get) == -1)//get return -1; ghttp_prepare(request); status = ghttp_process(request); if(status == ghttp_error) return -1; printf("Status code -> %d\n", ghttp_status_code(request)); buf = ghttp_get_body(request); bytes_read = ghttp_get_body_len(request); size = strlen(buf);//size == bytes_read fwrite (buf , 1 ,size , pFile ); fclose(pFile); return 0; }
POST示例
int post_test() { char szXML[2048]; char szVal[256]; ghttp_request *request = NULL; ghttp_status status; char *buf; char retbuf[128]; int len; strcpy(szXML, "POSTDATA="); sprintf(szVal, "%d", 15); strcat(szXML, szVal); printf("%s\n", szXML); //test request = ghttp_request_new(); if (ghttp_set_uri(request, uri) == -1) return -1; if (ghttp_set_type(request, ghttp_type_post) == -1) //post return -1; ghttp_set_header(request, http_hdr_Content_Type, "application/x-www-form-urlencoded"); //ghttp_set_sync(request, ghttp_sync); //set sync len = strlen(szXML); ghttp_set_body(request, szXML, len); // ghttp_prepare(request); status = ghttp_process(request); if (status == ghttp_error) return -1; buf = ghttp_get_body(request); //test sprintf(retbuf, "%s", buf); ghttp_clean(request); return 0; }
源代碼打包下載:藝搜下載
一般在do{}while(1)中,選擇使用同步的方式;
如果是set(callback)的方式,這時可以使用異步的方式。如果是異步的方式,一般涉及到對接收包的排序問題。參見同步和異步的區別
相關函數
ghttp_set_sync(request, ghttp_sync); //設置同步 // This is the http request object ghttp_request *request = NULL; // Allocate a new empty request object request = ghttp_request_new(); // Set the URI for the request object ghttp_set_uri(request, "http://localhost:8080/index.html"); // Close the connection after you are done. ghttp_set_header(request, http_hdr_Connection, "close"); //Prepare the connection ghttp_prepare(request); // Process the request ghttp_process(request); // Write out the body. Note that the body of the request may not be null terminated so we have to be careful of the length. fwrite(ghttp_get_body(request), ghttp_get_body_len(request), 1, stdout); //Destroy the request. This closes any file descriptors that may be open and will free any memory associated with the request. ghttp_request_destroy(request);
藝搜參考
http://lfs.linuxsir.org/htdocs/blfscvs/gnome/libghttp.html
http://oss.org.cn/ossdocs/gnu_linux/lfs/blfs-1.0/gnome/libghttp.html