c libghttp ghttp 库使用指南
2024-08-31 08:08:52
libghttp是一个很好用的 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 typeif you use `--no-verify' Try `ltconfig --help'formore information. configure: error: libtool configure failed ...
即configure无法识别系统的类型, 所以提示you must specify a host type.
解决方法:
用 /usr/share/libtool/config/config.guess 覆盖源码包中的config.guess
tar -zxvf libghttp-1.0.9.tar.gz
cd libghttp-1.0.9
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/localmakesudomakeinstall
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)//getreturn-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_readfwrite(buf ,1,size , pFile );fclose(pFile);return0;}
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)//postreturn-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);//testsprintf(retbuf,"%s", buf); ghttp_clean(request);return0;}
源代码打包下载:艺搜下载
一般在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
最新文章
- Discuz中解决jquery 冲突的方法 绝对简单
- nginx参数说明
- C语言 预处理一(文件包含--#include)
- KMP模式匹配
- ARM字节对齐问题详解
- MySQL存储过程详解 mysql 存储过程(二)
- jsp 常用9大内置对象
- CMake快速入门教程-实战
- Hadoop实战实例
- 【SEO 决胜网络索引】 课程大纲及第一部分第一课:网络营销战略中的索引
- PHP 18:data_valid_fns.php 看正则表达式
- Android中的分页加载
- php面向对象4
- Spring Boot整合Spring Security
- 在VS2017中安装OpenGL
- 解决每次从cmd进入sqlplus,都得重新设置pagesize、linesize的问题
- CactiEZ安装与配置-监控网卡流量
- (转载)Unity学习笔记:关于Dropdown的学习
- 两个有序数组的中位数(第k大的数)
- c#继承中的函数调用实例
热门文章
- Activiti 流程启动及节点流转源代码分析
- Hibernate_8_Person和IdCard实例_一对一关系:基于外键
- netlink error: too many arguments to function &#39;netlink_kernel_create&#39;
- GTID数据库备份
- 导入exce表格中的数据l到数据库
- Number Sequence_hdu_1005(规律)
- KineticJS教程(11)
- TP框架M方法 create方法丢失字段问题
- MySQL中group_concat函数
- MySQL —— 如何快速对比数据?