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

c libghttp ghttp 库使用指南的更多相关文章

  1. Linux静态库生成指南

    Linux静态库生成指南   Linux上的静态库,其实是目标文件的归档文件.在Linux上创建静态库的步骤如下: 写源文件,通过 gcc -c xxx.c 生成目标文件. 用 ar 归档目标文件,生 ...

  2. C++的XML编程经验――LIBXML2库使用指南[转]

    C++的XML编程经验――LIBXML2库使用指南 写这篇文章的原因有如下几点:1)C++标准库中没有操作XML的方法,用C++操作XML文件必须熟悉一种函数库,LIBXML2是其中一种很优秀的XML ...

  3. 北大POJ题库使用指南

    原文地址:北大POJ题库使用指南 北大ACM题分类主流算法: 1.搜索 //回溯 2.DP(动态规划)//记忆化搜索 3.贪心 4.图论 //最短路径.最小生成树.网络流 5.数论 //组合数学(排列 ...

  4. C++的XML编程经验――LIBXML2库使用指南

    C++的XML编程经验――LIBXML2库使用指南 写这篇文章的原因有如下几点:1)C++标准库中没有操作XML的方法,用C++操作XML文件必须熟悉一种函数库,LIBXML2是其中一种很优秀的XML ...

  5. 现代前端库开发指南系列(二):使用 webpack 构建一个库

    前言 在前文中,我说过本系列文章的受众是在现代前端体系下能够熟练编写业务代码的同学,因此本文在介绍 webpack 配置时,仅提及构建一个库所特有的配置,其余配置请参考 webpack 官方文档. 输 ...

  6. Blazor 组件库开发指南

    翻译自 Waqas Anwar 2021年5月21日的文章 <A Developer's Guide To Blazor Component Libraries> [1] Blazor 的 ...

  7. LIBXML2库使用指南2

    3. 简单xml操作例子 http://blog.sina.com.cn/s/blog_4673bfa50100b0xj.html 了解以上基本知识之后,就可以进行一些简单的xml操作了.当然,还没有 ...

  8. Python的dnspython库使用指南

    因为平时在测试DNS的时候有些操作手动完成不方便,所以需要用到脚本,而在Python里dnspython这个用于DNS操作的库十分强大,但是无奈网上大部分资料只列举了少部分的用法,所以记录一下我平时使 ...

  9. POCO文档翻译:POCO C++库入门指南

    内容目录 介绍 Foundation库 XML库 Util库 Net库 将这些东西组合到一起 介绍 POCO C++库是一组开源C++类库的集合,它们简化及加速了用C++来开发以网络功能为核心的可移植 ...

随机推荐

  1. 取石子 (四)_nyoj_161(博弈-奇异矩阵).java

    取石子 (四) 时间限制: 1000 ms  |  内存限制: 65535 KB 难度: 4   描述 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是 ...

  2. 算法笔记_044:表达式计算求值(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 输入一个只包含加减乖除和括号的合法表达式,求表达式的值.其中除表示整除. 输入格式 输入一行,包含一个表达式. 输出格式 输出这个表达式的 ...

  3. 搭建ssm+maven环境遇到的问题

    1. @RunWith(SpringJUnit4ClassRunner.class) gives error 在使用junit测试过程中 2.com.google.gson.JsonIOExcepti ...

  4. 揭秘ThreadLocal(转)

    转载自:掘金大闲人柴毛毛博客. ThreadLocal是开发中最常用的技术之一,也是面试重要的考点.本文将由浅入深,介绍ThreadLocal的使用方式.实现原理.内存泄漏问题以及使用场景. Thre ...

  5. 深入理解JavaScript系列(转载)

    深入理解JavaScript系列 深入理解JavaScript系列(1):编写高质量JavaScript代码的基本要点 深入理解JavaScript系列(2):揭秘命名函数表达式 深入理解JavaSc ...

  6. KISS My YAGNI,KISS (Keep It Simple, Stupid)和 YAGNI (You Ain’t Gonna Need It)软件开发原则

    http://www.aqee.net/kiss-my-yagni/我们都知道KISS (Keep It Simple, Stupid)和 YAGNI (You Ain’t Gonna Need It ...

  7. Android:EditText限制文字输入

    Android的编辑框控件EditText在平常编程时会经常用到,有时候会对编辑框增加某些限制,如限制只能输入数字,最大输入的文字个数,不能输入 一些非法字符等,这些需求有些可以使用android控件 ...

  8. MFC的CListCtrl双击获取单元格的行列

    首先要把ClistCtrl设置为整选中模式和网格模式 SetExtendedStyle(LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT); 重写方法如下: void CAl ...

  9. 安卓编译报错:Dex Loader] Unable to execute dex: java.nio.BufferOverflowException. Check the Eclipse log for stack trace.

    今天在编译别人的project时遇到了这个错误,心想应该不是代码的问题.网上搜了一下,应该是sdk版本设置的问题,要设置为最新sdk版本才可以. 解决办法就是修改project.properties里 ...

  10. numpy和matplotlib绘制直方图

    使用 Matplotlib Matplotlib 中有直方图绘制函数:matplotlib.pyplot.hist()它可以直接统计并绘制直方图.你应该使用函数 calcHist() 或 np.his ...