libghttp安装及使用封装案例(httpClient)
libghttp库下载地址:http://lfs.linuxsir.org/htdocs/blfscvs/gnome/libghttp.html官方API参考文档:https://sourcecodebrowser.com/libghttp/1.0.9/ghttp_8h.html将源码包下载下来,进入例行安装流程:先解压:tar -xzvf libghttp-1.0.9.tar.gzc
·
libghttp库下载地址:http://lfs.linuxsir.org/htdocs/blfscvs/gnome/libghttp.html
官方API参考文档:https://sourcecodebrowser.com/libghttp/1.0.9/ghttp_8h.html
将源码包下载下来,进入例行安装流程:
先解压:
tar -xzvf libghttp-1.0.9.tar.gz
cd libghttp-1.0.9
安装:
./configure
make
make install
在安装的过程中可能会遇到一些问题:
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
解决这个问题其实很简单:
# 首先查找一下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 .
如果找不到则:
sudo apt-get install automake
下面是我自己封装的一个http客户端,拥有post和get方法:
头文件httpClient.h
/*=========================================================
*文件名称: httpClient.h
*创建日期: 2017-7-6
*创建者 : 冷月莫负
*修改记录:
* 2017-7-6 created
===========================================================*/
#ifndef HTTPCLIENT_H
#define HTTPCLIENT_H
#include <map>
#include <string>
using namespace std;
int Get(string url, string params, int timeout, string&result,map<string,string>&m) ;
int Post(string uri, string params, int timeout, string&result, map<string,string>&m) ;
#endif
cpp实现文件httpClient.cpp
#include "ghttp.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "httpClient.h"
#include "ulaneLog.h"
int Get(string url, string params, int timeout, string& result,map<string,string>&m)
{
ghttp_status status;
char* temresult = NULL;
ghttp_request *request = NULL;
request = ghttp_request_new();
char szUrl[256];
memset(szUrl,0x00,sizeof(szUrl));
strcpy(szUrl,url.c_str());
if (ghttp_set_uri(request, szUrl) == -1)
{
ghttp_clean(request);
return -1;
}
if (ghttp_set_type(request, ghttp_type_get) == -1)
{
ghttp_clean(request);
return -1;
}
map<string,string>::iterator it = m.begin();
for(it;it!=m.end();it++)
{
ghttp_set_header(request, it->first.c_str(),it->second.c_str());
}
char timeout_str[10];
sprintf(timeout_str, "%d", timeout);
ghttp_set_header(request, http_hdr_Timeout, timeout_str);
ghttp_set_sync(request, ghttp_sync);
ghttp_prepare(request);
status=ghttp_process(request);
if (status == ghttp_error)
{
ghttp_request_destroy(request);
return -1;
}
temresult = ghttp_get_body(request);
result += temresult;
ghttp_request_destroy(request);
return 0;
}
int Post(string uri, string params, int timeout, string& result, map<string,string>&m)
{
char* temresult = NULL;
ghttp_request *request = NULL;
ghttp_status status;
int len;
char szUri[256];
memset(szUri,0x00,sizeof(szUri));
strcpy(szUri,uri.c_str());
request = ghttp_request_new();
if (ghttp_set_uri(request, szUri) == -1)
{
ghttp_clean(request);
return -1;
}
if (ghttp_set_type(request, ghttp_type_post) == -1)
{
ghttp_clean(request);
return -1;
}
map<string,string>::iterator it = m.begin();
for(it;it!=m.end();it++)
{
ghttp_set_header(request, it->first.c_str(),it->second.c_str());
}
ghttp_set_sync(request, ghttp_sync);
char timeout_str[10];
sprintf(timeout_str, "%d", timeout);
ghttp_set_header(request, "Content-Type", "application/json");
ghttp_set_header(request, http_hdr_Timeout, timeout_str);
char szParams[8192];
memset(szParams,0x00,sizeof(szParams));
strcpy(szParams,params.c_str());
len = strlen(szParams);
ghttp_set_body(request, szParams, len); //
ghttp_prepare(request);
status = ghttp_process(request);
if (status == ghttp_error)
{
return -1;
}
result = ghttp_get_body(request); //test
temresult = ghttp_get_body(request);
result += temresult;
ghttp_request_destroy(request);
return 0;
}
post和get方法中最后一个参数是一个map<string,string>类型的map容器,使用的时候只需要将你要设置的header都已key:value的形式存到容器中,就都会被设置。
参考文章:
http://blog.csdn.net/earbao/article/details/39007549
http://www.phpjiayuan.com/109/270.html
欢迎关注问我团队公众号:
更多推荐
已为社区贡献1条内容
所有评论(0)