建站工具wordpress,外贸网站推广招聘,开发公司五证包括什么,织梦做网站想用C语言写个采集程序#xff0c;涉及到http相关的东西#xff0c;找了找#xff0c;有现成的libghttp库。 libghttp库的官方网址google一下第一条结果一般就是的#xff1a;http://lfs.linuxsir.org/htdocs/blfscvs/gnome/libghttp.html
将源码包下载下来#xff0c;进…想用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 recognizedchecking build system type... Invalid configuration x86_64-unknown-linux-gnu: machine x86_64-unknown not recognizedchecking 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安装成功啦
#includelibghttp.h
#includesthio.hmain()
{// 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?titleLibghttp库使用指南 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!NULLstrlen(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_lenghttp_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] lanjavaPOSTDATA15fghttp; 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?heheyy,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