libcurl库的使用

libcurl是一个关于http,ftp等协议的开源库,通过该库你能够模拟出这些协议的过程;可在 http://curl.haxx.se/download.html 下载符合你要求的平台;

你也可以阅读该 http://curl.haxx.se/libcurl/c/libcurl-tutorial.html ,来对libcurl有一个整体上的感觉,你可以知道libcurl提供了什么功能,最典型的应用是什么,以及一些关键的概念解析;

以下是我的一个实例,我需要得到URL  http://192.168.1.1/m.htm ,但是该网站需要Basic的Authentication,用户名和秘密是:admin:admin

 

#include <stdio.h>
#include <curl/curl.h>

int main(void)
{
CURL *curl;
CURLcode res;

curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.1.1/m.htm");
curl_easy_setopt(curl,CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
curl_easy_setopt(curl,CURLOPT_USERPWD , "admin:admin" );


/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);

/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));

/* always cleanup */
curl_easy_cleanup(curl);
}

return 0;
}

 

posted on 2012-10-17 11:22  bluebbc  阅读(2628)  评论(0编辑  收藏  举报

导航