想用C語言寫個采集程序,涉及到http相關的東西,找了找,有現成的libghttp庫。
libghttp庫的官方網址google一下第一條結果一般就是的:http://lfs.linuxsir.org/htdocs/blfscvs/gnome/libghttp.html
將源碼包下載下來,進入例行安裝流程:
1、解壓
# tar -xzvf libghttp-1.0.9.tar.gz
# cd libghttp-1.0.9
2、安裝
./configure
make
make install
安裝過種中小插曲:
在執行./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
checking for ranlib... ranlib
checking for ld used by GCC... /usr/bin/ld
checking if the linker (/usr/bin/ld) is GNU ld... yes
checking for BSD-compatible nm... /usr/bin/nm -B
checking whether ln -s works... yes
updating cache ./config.cache
ltconfig: you must specify a host type if you use `--no-verify'
Try `ltconfig --help' for more information.
configure: error: libtool configure failed
聽說在32位操作系統上一般不會有這樣的錯誤,在64位系統上比較常見,原因是軟件自身的config.guess和config.sub文件有問題,從系統中復制這兩個文件到軟件目錄覆蓋一下重新./configure就可以了(如果不知道這兩個文件在哪里,用find查找一下就OK了):
# 首先查找一下config.guess和config.sub文件的目錄
find / -name config.guess
find / -name config.sub# 將查找出來的文件隨便選擇一個覆蓋到軟件目錄
cp /usr/share/automake-1.11/config.guess .
cp /usr/share/automake-1.11/config.sub .
下面是用libghttp搞了一個測試代碼,成功編譯和運行,證明libhttp安裝成功啦:
#include<libghttp.h>
#include<sthio.h>main()
{// This is the http request objectghttp_request *request = NULL;// Allocate a new empty request objectrequest = ghttp_request_new();// Set the URI for the request objectghttp_set_uri(request, "http://www.phpjiayuan.com/");// Close the connection after you are done.ghttp_set_header(request, http_hdr_Connection, "close");// Prepare the connectionghttp_prepare(request);// Process the requestghttp_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 reque
st. ghttp_request_destroy(request);
}
另一個網絡上的實例:
netutil.h
/* * File: netutil.h * Author: Administrator * * Created on 2014年9月2日, 下午3:51 */ #ifndef NETUTIL_H #define NETUTIL_H #ifdef __cplusplus extern "C" { #endif int isFileExist(char * savePath); int download(char *uri, char *savePath) ; //result地址參數傳遞 int netGet(char* url, char* params, int timeout, char **result, int result_len) ; int netPost(char* uri, char* params, int timeout, char **result, int result_len) ; #ifdef __cplusplus } #endif #endif /* NETUTIL_H */
netutil.c
#include "ghttp.h" #include "http_hdrs.h" #include <stdio.h> #include <string.h> #include <stdlib.h> #include <io.h> #include <unistd.h> #define GET_ARRAY_LEN(array,len) {len = (sizeof(array) / sizeof(array[0]));} int isFileExist(char * savePath) { if (!access(savePath, F_OK)) { return 1; } else { return 0; } } //http://www.elesos.com/index.php?title=Libghttp庫使用指南 int download(char *uri, char *savePath) { ghttp_request *request = NULL; ghttp_status status; FILE * pFile; char *buf; int bytes_read; int size; if(!isFileExist(savePath)) { printf("savePath not exist "); } pFile = fopen(savePath, "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 //這里是錯誤的,當返回的數據為文本時,strlen的長度是正確,二進制數據如圖片/apk時的長度是錯誤的。 size = bytes_read; //size != bytes_read printf("buf :%s \n size :%d \n" , buf,size); fwrite(buf, 1, size, pFile); fclose(pFile); ghttp_clean(request); ghttp_request_destroy(request); return 0; } //result地址參數傳遞 int netGet(char* url, char* params, int timeout, char **result, int result_len) { ghttp_request *request = NULL; request = ghttp_request_new(); if(params!=NULL&&strlen(params)>0) { char tmp[1024]; strcpy(tmp,url); if(strchr(tmp, '?') == NULL)//url不存在 { strcat(tmp,"?") ; } strcat(tmp,params) ; printf("%s\n",tmp); ghttp_set_uri(request, tmp); }else{ ghttp_set_uri(request, url); } ghttp_set_type(request, ghttp_type_get); //get方法 ghttp_set_header(request, http_hdr_Connection, "close"); char timeout_str[10]; sprintf(timeout_str, "%d", timeout); ghttp_set_header(request, http_hdr_Timeout, timeout_str); ghttp_prepare(request); ghttp_process(request); *result = ghttp_get_body(request); result_len = ghttp_get_body_len(request); ghttp_request_destroy(request); return 0; } int netPost(char* uri, char* params, int timeout, char **result, int result_len) { char szVal[1024]; ghttp_request *request = NULL; ghttp_status status; int len; printf("%s\n", params); //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"); char timeout_str[10]; sprintf(timeout_str, "%d", timeout); ghttp_set_header(request, http_hdr_Timeout, timeout_str); //ghttp_set_sync(request, ghttp_sync); //set sync len = strlen(params); ghttp_set_body(request, params, len); // ghttp_prepare(request); status = ghttp_process(request); if (status == ghttp_error) return -1; *result = ghttp_get_body(request); //test result_len=ghttp_get_body_len(request); ghttp_clean(request); return 0; }
main.c
#include <stdlib.h> #include "netutil.h" int main(int argc, char *argv[]) { char *result; int len; result=(char*)malloc(sizeof(char*)*8096); memset(result, 0, sizeof(char*)*8096); char param[2048]= "&lan=java&POSTDATA=15&f=ghttp"; netPost("http://127.0.0.1:8080/server/index.do",param, 5000, &result, len); printf("%s\n%d\n", result, len); netGet("http://127.0.0.1:8080/server/index.do?hehe=yy",param, 5000, &result, len); printf("%s\n%d\n", result, len); download("http://www.baidu.com/","test//fuck.html"); return 0; }
轉自:http://www.phpjiayuan.com/109/270.html
轉自:http://blog.csdn.net/earbao/article/details/39007549