Libghttp库使用指南

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值