libcurl windows使用
2011-04-28 18:59
编译libcurl,以VS2005为例
将curl-7.16.2.tar.bz2解压到某目录,例如C:\curl,进入C:\curl\lib目录。
将zlib123-dll.zip解压到某目录,例如C:\zlib
设Openssl的目录为C:\openssl
进入Visual Studio 2005命令提示,进入C:\curl\lib
编译Debug版本。
set CFG=debug-dll-ssl-dll-zlib-dll
set OPENSSL_PATH=C:/openssl
set ZLIB_PATH=C:/zlib/include
nmake -f Makefile.vc8
其输出:libcurld_imp.lib, libcurld.dll
编译Release版本。
set CFG=release-dll-ssl-dll-zlib-dll
set OPENSSL_PATH=C:/openssl
set ZLIB_PATH=C:/zlib/include
nmake -f Makefile.vc8
其输出:libcurl_imp.lib, libcurl.dll
如果需要编译其他版本,可查看设定相应的CFG参数即可。
libcurl配置 vc windows vs2008/2010
www.libcurl.org下载
解压
打开根目录下的curlxxx.dsw
选择libcurl,单编译这个release
=======================
使用的时候
指定VC路径
include到目录里面的indclude
lib库指到刚编译的lib\release_lib\
1、给工程添加依赖的库:项目->属性->链接器->输入->附加依赖项,把libcurl.lib ws2_32.lib winmm.lib wldap32.lib添加进去
注意,debug配置用libcurld.lib
2、加入预编译选项:项目->属性->c/c++ ->预处理器->预处理器,把 ;BUILDING_LIBCURL;HTTP_ONLY复制进去(注意不要丢了";")
就可以静态编译了.
关于Libcurl 的windoows使用
推荐一个我自己做的一个电影站,绝对没弹窗:-) www.fastdy.com 速看电影吧,每天都更新,
最新的电影电视剧都在速看电影吧!~
首先到libcurl的官网上直接找最新的安装包,我用的是VS2010可以直接编译,编译好之后,可以得到3个文件 都是DEBUG模式下的,不影响使用
libcurld.dll,libcurld_imp.lib,libcurld_imp.exp
新建空的控制台工程 代码如下 并且包含libcurl里的include,还有生成libcurld.dll的目录
#include <iostream>
#include <stdio.h>
#include <string>
#include <curl/curl.h>
using namespace std;
#pragma comment(lib, "libcurld_imp.lib")
string httpdata;
size_t process_data(char *buffer, size_t size, size_t nmemb, char *user_p) {
//string rststr((char *)buffer);
string rst(buffer);
httpdata=httpdata+rst;
//printf("We received Content-Type kongchaos: %s\n", (char *)buffer );
return 0;
}
int main(){
cout<<"Hello libcurl"<<endl;
CURL *curl;
CURLcode res;
struct curl_slist *headers=NULL;
static const char *postthis="did=Tree&xmltype=xmlResources&action=load&sql=&tablename=other&dbname=CONFIGESB.DB&data=";
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://www.gbsoa.com/clientParamRequest.action?");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postthis);
/* if we don't provide POSTFIELDSIZE, libcurl will strlen() by
itself */
//headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
//headers = curl_slist_append(headers,"Accept: text/html, */*");
//headers = curl_slist_append(headers,"ContentEncoding:UTF-8");
//headers = curl_slist_append(headers,"ContentLength:-1");
headers = curl_slist_append(headers,"Accept-Encoding:gunzip|gzip");
//headers = curl_slist_append(headers,"BasicAuthentication:False");
//headers = curl_slist_append(headers,"User-Agent: Mozilla/4.0 (compatible; MSIE7.0; Windows NT5.1; Mozilla/4.0 (compatible; MSIE 6.0; WindowsNT 5.1; SV1) ; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NETCLR 3.5.30729; .NET4.0C; .NET4.0E)");
//curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(postthis));
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &process_data);
std::string buffer;
//curl_easy_setopt(curl, CURLOPT_READDATA, &buffer);
//curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
curl_slist_free_all(headers); /* free the header list */
cout<<httpdata<<endl;
//cout<<buffer<<endl;
}
int i;
cin>>i;
return 0;
}
其中
curl_easy_setopt就是curl的精华所在,可以使用很多功能,因为我只需要POST后得到返回值,只需要写
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &process_data);
其中process_data就是回调函数,如果没写,curl里会自动调用它本身的回调函数
编译成功后,就可以获得你想要的结果了 ,这个是程序运行的时候是需要libcurld.DLL的
如果想静态调用,请在预处理器那里加入
CURL_STATICLIB
连接器那里加入 附加依赖项里加入
libcurld_imp.lib
Ws2_32.lib
Wldap32.lib
还有把Libcurl里的head,souces全部复制进去(嘿嘿偷懒)
这样就可以了,如果这个DEMO在其他机器上运行不了,加入两个DLL
msvcp100d.dll
msvcr100d.dll