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.pswp.cn/news/274040.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/274040.shtml
英文地址,請注明出處:http://en.pswp.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貝葉斯算法拓展下載&#…