#include "../curl-7.14.0/include/curl/curl.h"
#pragma comment(lib, "../curl-7.14.0/lib/libcurl_imp.lib")
int main(void)
{
curl = curl_easy_init();
if(curl) {
CURLcode res;
res = curl_easy_setopt(curl, CURLOPT_PROXY, "Test-pxy08:8080");
res = curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
res = curl_easy_setopt(curl, CURLOPT_URL, "http://www.vckbase.com");
res = curl_easy_perform(curl);
if(CURLE_OK == res) {
char *ct;
/* ask for the content-type */
/* http://curl.haxx.se/libcurl/c/curl_easy_getinfo.html */
res = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);
if((CURLE_OK == res) && ct)
printf("We received Content-Type: %s ", ct);
}
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}
=======================================================================
from: http://www.cppblog.com/qiujian5628/archive/2008/06/28/54873.html
在使用之前请大家多阅读libcurl的文档:因为如果要实际运用到项目中,最好对libcurl有具体的了解,具体在
http://curl.haxx.se/libcurl/c/
curl_easy_setopt()
curl_easy_perform()
curl_easy_getinfo()
这三个函数的使用上,需要多去钻研,多看Samples,你才能灵活使用libcurl。
感谢这篇文章:
http://blog.163.com/xu_chao2000/blog/static/27770610200801303252802/
给了我许多启发,再次感谢!
给出我的一个简单的代码例子:
说明:
1.PM...等参数,没有具体意义,就是long,bool,int,char*等进行特殊封装过,你可以用普通类型取代。
2.关键在curl_easy_setopt函数设置option,可以设置ftp,http,get,post等许多选项,请根据具体使用情况设置。
3.对取回来的数据需要进行判断,比如http下载文件,如果文件不存在,需要进行处理。因为writer是可以将buf填充404 not found等网页内容的,不能将这个内容当成文件内容,所以需要判断http web返回来的code,进行判断。
4.我有个问题,就是想得到服务器上filename的具体名称,verbose调试已经返回了,但是我在getinfo的时候,试过好多选项,但未找到这个存放真实服务器文件名的选项,如果有知道的麻烦告诉我,谢谢了!
#pragma comment(lib, "libcurl.lib")
PM_ULONG writer(PM_VOID *data, PM_INT size, PM_INT nmemb, PmString &content);
PM_BOOL CurlInit(CURL *&curl, PM_LPCSTR url,PmString &content);
PM_BOOL GetURLDataBycurl(PM_LPCSTR URL, PmString &content);
void main()
{
//PM_LPSTR url="http://down1.chinaunix.net/distfiles/libpcap-1.0.20050129.tar.gz";
PM_LPCSTR url ="http://www.baidu.com/img/baidu.gif";
PmString content;
if ( GetURLDataBycurl(url,content))
{
printf("%s/n",content);
}
getchar();
}
PM_BOOL CurlInit(CURL *&curl, PM_LPCSTR url,PmString &content)
{
CURLcode code;
PmString error;
curl = curl_easy_init();
if (curl == NULL)
{
debug0( "Failed to create CURL connection/n");
return PM_FALSE;
}
code = curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
if (code != CURLE_OK)
{
debug1( "Failed to set error buffer [%d]/n", code );
return PM_FALSE;
}
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
code = curl_easy_setopt(curl, CURLOPT_URL, url);
if (code != CURLE_OK)
{
debug1("Failed to set URL [%s]/n", error);
return PM_FALSE;
}
code = curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
if (code != CURLE_OK)
{
debug1( "Failed to set redirect option [%s]/n", error );
return PM_FALSE;
}
code = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
if (code != CURLE_OK)
{
debug1( "Failed to set writer [%s]/n", error);
return PM_FALSE;
}
code = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content);
if (code != CURLE_OK)
{
debug1( "Failed to set write data [%s]/n", error );
return PM_FALSE;
}
return PM_TRUE;
}
PM_ULONG writer(PM_VOID *data, PM_INT size, PM_INT nmemb, PmString &content)
{
PM_ULONG sizes = size * nmemb;
PmString temp(data,sizes);
content += temp;
return sizes;
}
PM_BOOL GetURLDataBycurl(PM_LPCSTR URL, PmString &content)
{
CURL *curl = NULL;
CURLcode code;
PmString error;
code = curl_global_init(CURL_GLOBAL_DEFAULT);
if (code != CURLE_OK)
{
debug1( "Failed to global init default [%d]/n", code );
return PM_FALSE;
}
if ( !CurlInit(curl,URL,content) )
{
debug0( "Failed to global init default [%d]/n" );
return PM_FALSE;
}
code = curl_easy_perform(curl);
if (code != CURLE_OK)
{
debug2( "Failed to get '%s' [%s]/n", URL, error);
return PM_FALSE;
}
PM_ULONG retcode = 0;
code = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE , &retcode);
if ( (code == CURLE_OK) && retcode == 200 )
{
PM_DOUBLE length = 0;
code = curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD , &length);
printf("%d",retcode);
FILE * file = fopen("1.gif","wb");
fseek(file,0,SEEK_SET);
fwrite(content.c_str(),1,length,file);
fclose(file);
//struct curl_slist *list;
//code = curl_easy_getinfo(curl,CURLINFO_COOKIELIST,&list);
//curl_slist_free_all (list);
return PM_TRUE;
}
else
{
// debug1( "%s /n ",getStatusCode(retcode));
return PM_FALSE;
}
curl_easy_cleanup(curl);
return PM_FALSE;
}
=======================================================================
from:http://topic.csdn.net/u/20081224/17/80bb810e-9b74-429f-8282-2c224f8fe372.html
使用libcurl库如何才能够得到重定向之后的url?
首先调用curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1)使其跟踪重定向,
然后调用curl_getinfo()获得返回的内容,返回的内容中便会包含重定向之后的url。
你试试看行不行?
=======================================================================
from: http://www.maycode.com/index.php/linux/54-linuxdevelop/1151-curl.html
- 摘要:本文详细说了了如何使用curl,libcurl进行网络程序的编写,并给出了代码实例。
这个函数只能用一次。(其实在调用curl_global_cleanup 函数后仍然可再用)
如果这个函数在curl_easy_init函数调用时还没调用,它讲由libcurl库自动完成。
参数:flags
CURL_GLOBAL_ALL //初始化所有的可能的调用。
CURL_GLOBAL_SSL //初始化支持 安全套接字层。
CURL_GLOBAL_WIN32 //初始化win32套接字库。
CURL_GLOBAL_NOTHING //没有额外的初始化。
描述:在结束libcurl使用的时候,用来对curl_global_init做的工作清理。类似于close的函数。
3 char *curl_version( );
描述: 打印当前libcurl库的版本。
4 CURL *curl_easy_init( );
描述:
curl_easy_init用来初始化一个CURL的指针(有些像返回FILE类型的指针一样). 相应的在调用结束时要用curl_easy_cleanup函数清理.
一般curl_easy_init意味着一个会话的开始. 它的返回值一般都用在easy系列的函数中.
5 void curl_easy_cleanup(CURL *handle);
描述:
这个调用用来结束一个会话.与curl_easy_init配合着用.
参数:
CURL类型的指针.
6 CURLcode curl_easy_setopt(CURL *handle, CURLoption option, parameter);
描述: 这个函数最重要了.几乎所有的curl 程序都要频繁的使用它.它告诉curl库.程序将有如何的行为. 比如要查看一个网页的html代码等.(这个函数有些像ioctl函数)
参数:
1 CURL类型的指针
2 各种CURLoption类型的选项.(都在curl.h库里有定义,man 也可以查看到)
3 parameter 这个参数 既可以是个函数的指针,也可以是某个对象的指针,也可以是个long型的变量.它用什么这取决于第二个参数.
CURLoption 这个参数的取值很多.具体的可以查看man手册.
7 CURLcode curl_easy_perform(CURL *handle);
描述:这个函数在初始化CURL类型的指针 以及curl_easy_setopt完成后调用. 就像字面的意思所说perform就像是个舞台.让我们设置的
option 运作起来.
参数:
CURL类型的指针.
设置访问URL
回调函数原型为:size_t function( void *ptr, size_t size, size_t nmemb, void *stream); 函数将在libcurl接收到数据后被调用,因此函数多做数据保存的功能,如处理下载文件。CURLOPT_WRITEDATA 用于表明CURLOPT_WRITEFUNCTION函数中的stream指针的来源。
回调函数原型为 size_t function( void *ptr, size_t size,size_t nmemb, void *stream); libcurl一旦接收到http 头部数据后将调用该函数。CURLOPT_WRITEDATA 传递指针给libcurl,该指针表明CURLOPT_HEADERFUNCTION 函数的stream指针的来源。
libCurl需要读取数据传递给远程主机时将调用CURLOPT_READFUNCTION指定的函数,函数原型是:size_t function(void *ptr, size_t size, size_t nmemb,void *stream). CURLOPT_READDATA 表明CURLOPT_READFUNCTION函数原型中的stream指针来源。
跟数据传输进度相关的参数。CURLOPT_PROGRESSFUNCTION 指定的函数正常情况下每秒被libcurl调用一次,为了使CURLOPT_PROGRESSFUNCTION被调用,CURLOPT_NOPROGRESS必须被设置为false,CURLOPT_PROGRESSDATA指定的参数将作为 CURLOPT_PROGRESSFUNCTION指定函数的第一个参数
CURLOPT_TIMEOUT 由于设置传输时间,CURLOPT_CONNECTIONTIMEOUT 设置连接等待时间
设置重定位URL
断点续传相关设置。CURLOPT_RANGE 指定char *参数传递给libcurl,用于指明http域的RANGE头域,例如:
表示头500个字节:bytes=0-499
表示第二个500字节:bytes=500-999
表示最后500个字节:bytes=-500
表示500字节以后的范围:bytes=500-
第一个和最后一个字节:bytes=0-0,-1
CURLOPT_RESUME_FROM 传递一个long参数给libcurl,指定你希望开始传递的
1. CURLE_OK
任务完成一切都好
读本地文件错误
curl = curl_easy_init(); //初始化一个CURL类型的指针
#include <curl/curl.h>;
FILE *fp; //定义FILE类型指针
int main(int argc, char *argv[])
curl_global_init(CURL_GLOBAL_ALL);
if((fp=fopen(argv[2],"w"))==NULL)
GtkWidget *Bar;
void *my_thread(void *ptr)
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, url);
res = curl_easy_perform(curl);
fclose(outfile);
return NULL;
int main(int argc, char **argv)
/* Must initialize libcurl before any threads are started */
/* Init thread */
gtk_init(&argc, &argv);
if (!g_thread_create(&my_thread, argv[1], FALSE, NULL) != 0)
gdk_threads_enter();
gcc progress.c –o progress ` pkg-config --libs –cflags gtk+-2..0` -lcurl –lgthread-2.0
#include <curl/curl.h>
/* _snscanf() is Win32 specific */
return size * nmemb;
/* 保存下载文件 */
/*读取上传文件 */
if (ferror(f))
n = fread(ptr, size, nmemb, f) * size;
return n;
// 下载 或者上传文件函数
//curl_easy_setopt(curlhandle, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath);
curl_easy_setopt(curlhandle, CURLOPT_CONNECTTIMEOUT, timeout); // 设置连接超时,单位秒
curl_easy_setopt(curlhandle, CURLOPT_WRITEDATA, f);
//curl_easy_setopt(curlhandle, CURLOPT_READFUNCTION, readfunc);
fclose(f);
if (r == CURLE_OK)
int main(int c, char **argv) {
curl_global_init(CURL_GLOBAL_ALL);
//download(curlhandle, "ftp://user:pass@host/path/file", "C://file", 0, 3);
return 0;
3.5 LibCurl调试实例
struct data {
static
unsigned int width=0x10;
if(nohex)
fprintf(stream, "%s, %zd bytes (0x%zx)/n", text, size, size);
for(i=0; i<size; i+= width) {
fprintf(stream, "%04zx: ", i);
if(!nohex) {
for(c = 0; (c < width) && (i+c < size); c++) {
static
switch (type) {
case CURLINFO_HEADER_OUT:
dump(text, stderr, (unsigned char *)data, size, config->trace_ascii);
int main(void)
config.trace_ascii = 1; /* enable ascii tracing */