libghttp 编译及封装使用实例

想用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 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

听说在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安装成功啦:

#include<libghttp.h>
#include<sthio.h>main()
{// 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?title=Libghttp库使用指南  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!=NULL&&strlen(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_len=ghttp_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]= "&lan=java&POSTDATA=15&f=ghttp";    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?hehe=yy",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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/274040.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java IO 节点流与处理流类型

处理流类型&#xff1a;1、处理流之首先缓冲流&#xff1a;解释&#xff1a;例子&#xff1a;TestBufferStream1.java package com.zhj.www;import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException;public class TestBufferStream1 …

高级浏览器-SRWare Iron 29.0.1600.0 版本发布

SRWare Iron是德国一安全公司srware改造的Chrome&#xff08;铬&#xff09;命名为铁&#xff08;iron&#xff09;的浏览器。于2008年9月18日首次发布。 据官方介绍&#xff0c;Iron浏览器砍掉了Chromium原程序中的很多有碍“隐私”问题的代码。 “iron中去除的功能包括&#…

shell中的${},##和%%的使用

假设我们定义了一个变量为&#xff1a; file/dir1/dir2/dir3/my.file.txt 可以用${ }分别替换得到不同的值&#xff1a; ${file#*/}&#xff1a;删掉第一个 / 及其左边的字符串&#xff1a;dir1/dir2/dir3/my.file.txt ${file##*/}&#xff1a;删掉最后一个 / 及其左边的字…

Java 线程多线程编程1---基础

1、线程的基本概念例子&#xff1a;分析&#xff1a;2、线程的创建和启动第一种线程的创建&#xff1a;定义一个线程类来实现Runner接口 例子&#xff1a; package com.zhj.www; import java.lang.Thread; public class TestThread1 {public static void main(String[] args) {…

移动互联网下一步:“深度学习”配合大数据

随着电子商务不断深入&#xff0c;百度、腾讯、阿里巴巴的移动互联网战略的可比性越来月低&#xff0c;如今百度的移动互联网的战略也面临挑战&#xff0c;最大的因素在于数据的来源。 对于互联网的公司最近的动态是什么&#xff1f;这个不是很难的&#xff0c;主要看一下公司的…

windows挂载linux网络文件系统NFS

ubuntu上安装配置nfs服务 #apt-get install nfs-kernel-server #mkdir /home/nfs #vim /etc/exports 在文档的最后一行加入/home/nfs *(rw,sync,no_root_squash,no_subtree_check)&#xff0c;保存退出。 #/etc/init.d/rpcbind restart 重启rpcbind #/etc/init.d/nfs-kern…

SQL的连接分为三种:内连接、外连接、交叉连接。

先给出两张表&#xff1a;一、内连接&#xff1a;内连接&#xff08;INNER JOIN&#xff09;&#xff1a;有两种&#xff0c;显式的和隐式的&#xff0c;返回连接表中符合连接条件和查询条件的数据行。&#xff08;所谓的链接表就是数据库在做查询形成的中间表&#xff09;。1、…

如何在使用摩托罗拉上的RSS阅读器应用进行一次订阅

订阅一个CSDN的RSS为例。 1、打开RSS阅读器。 2、设置->新增订阅->手动新增 订阅URL:输入http://articles.csdn.net/api/rss.php?tid1000 &#xff08;可以先在PC上打开下该网页&#xff0c;发现他是一个xml网页。&#xff09; 订阅名称&#xff1a;自己起一个名字&…

RTP与RTCP协议介绍

本文转自&#xff1a;http://blog.51cto.com/zhangjunhd/25481 1&#xff0e;流媒体( Streaming Media) 1.1流媒体概念 流媒体技术是网络技术和多媒体技术发展到一定阶段的产物。术语流媒体既可以指在网上传输连续时基媒体的流式技术,也可以指使用流式技术的连续时基媒体本身…

JSP学习

一、JSP 简介 什么是Java Server Pages? JSP全称Java Server Pages&#xff0c;是一种动态网页开发技术。它使用JSP标签在HTML网页中插入Java代码。标签通常以<%开头以%>结束。 JSP是一种Java servlet&#xff0c;主要用于实现Java web应用程序的用户界面部分。网页开发…

Java给定一个字符串数组,判断每个字符出现次数

题目要求&#xff1a;给定一个字符串&#xff0c;判断每个字符出现多少次&#xff1f; 解决思路&#xff1a;利用Map的特性&#xff1a;即Map集合中如果两个key&#xff08;键&#xff09;值是一样相同的&#xff0c;那么&#xff0c;后放&#xff08;put&#xff09;入的值会将…

Java-n个人报数

题目&#xff1a; 有n个人围成一圈&#xff0c;顺序排号。从第一个人开始报数&#xff08;从1到3报数&#xff09;&#xff0c;凡报到3的人退出圈子&#xff0c;问最后留下的是原来第几号的哪一位&#xff1f; 大概思路&#xff1a;假设有3个人&#xff0c;它们围成一圈&#x…

100个直接可以拿来用的JavaScript实用功能代码片段

http://www.cnblogs.com/wxydigua/p/3314274.html转载于:https://www.cnblogs.com/kevinge/p/3316315.html

[转]JS Cookie 中文乱码

首先是一个解析cookie的函数&#xff1a; <script type"text/javascript"> function getCookie(name) { var cookies document.cookie.split(";"); for(var i0;i<cookies.length;i) { var cookie cookies[i]; var cookieStr cookie.…

Java线程中关于Synchronized的用法

synchronized是Java中的关键字&#xff0c;是一种同步锁。它修饰的对象有以下几种&#xff1a; 1. 修饰一个代码块&#xff0c;被修饰的代码块称为同步语句块&#xff0c;其作用的范围是大括号{}括起来的代码&#xff0c;作用的对象是调用这个代码块的对象&#xff1b; 2. 修饰…

cmd命令行设置环境变量

http://blog.sciencenet.cn/blog-51026-566742.html 1、查看当前所有可用的环境变量&#xff1a;输入 set 即可查看。 2、查看某个环境变量&#xff1a;输入 “set 变量名”即可&#xff0c;比如想查看path变量的值&#xff0c;即输入 set path。 3、修改环境变量 &#xff1a;…

Java线程之多线程与多进程(1)——以操作系统的角度述说线程与进程

任务调度 线程是什么&#xff1f;要理解这个概念&#xff0c;须要先了解一下操作系统的一些相关概念。大部分操作系统(如Windows、Linux)的任务调度是采用时间片轮转的抢占式调度方式&#xff0c;也就是说一个任务执行一小段时间后强制暂停去执行下一个任务&#xff0c;每个任务…

用 PS 调整服务器时间

用 PS 调整服务器时间 Powershell 有一个命令用来调整计算机的时间&#xff0c; Set-Date -Adjust&#xff0c;但是&#xff0c;只能调整本地时间&#xff0c;不能调整远程的计算机时间。 function AdjustDCTime ( $Server, $addTime ){ $Svr Get-WmiObject Win32_Operatin…

Java线程之多线程与多进程(2)——线程优先级与线程安全

线程优先级 现在主流操作系统(如Windows、Linux、Mac OS X)的任务调度除了具有前面提到的时间片轮转的特点外&#xff0c;还有优先级调度(Priority Schedule)的特点。优先级调度决定了线程按照什么顺序轮流执行&#xff0c;在具有优先级调度的系统中&#xff0c;线程拥有各自的…

mahout贝叶斯算法开发思路(拓展篇)1

首先说明一点&#xff0c;此篇blog解决的问题是就下面的数据如何应用mahout中的贝叶斯算法&#xff1f;&#xff08;这个问题是在上篇&#xff08;。。。完结篇&#xff09;blog最后留的问题&#xff0c;如果想直接使用该工具&#xff0c;可以在mahout贝叶斯算法拓展下载&#…