簡単なインターフェースで環境 libcurl を使用する場合、最初に以下を呼び出す必要があります。
curl_easy_init()
、イージー ハンドルを開始します。
curl_global_init()
、ほとんどの場合、フラグオプションはCURL_GLOBAL_ALL
これら 2 つの関数はそれぞれ、最初に 1 回だけ呼び出され、反対のクリーンアップが必要です。
curl_easy_cleanup()
宣言したハンドルが終了したら、
curl_global_cleanup()
libcurl を使い終わったら、
より良い結果を得るには、できる限りエラーをチェックしてください。libcurl はcurl_easy_strerror()
そのための機能を提供します。CURLcode エラーを説明する文字列を返します。また、一部の関数は値 CURL_OK を返すか、すべて問題がない場合は特定の整数を返します。
たとえば、 CURLOPT_URL オプションを使用する適切な方法は次のとおりです。
#include <curl.h>
int main(void)
{
/* declaration of an object CURL */
CURL *handle;
/* result of the whole process */
CURLcode result;
/* the first functions */
/* set up the program environment that libcurl needs */
curl_global_init(CURL_GLOBAL_ALL);
/* curl_easy_init() returns a CURL easy handle that you're gonna reuse in other easy functions*/
handle = curl_easy_init();
/* if everything's all right with the easy handle... */
if(handle)
{
/* ...you can list the easy functions */
/* here we just gonna try to get the source code of http://example.com */
curl_easy_setopt(handle, CURLOPT_URL, "http://example.com");
/* but in that case we also tell libcurl to follow redirection */
curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1L);
/* perform, then store the expected code in 'result'*/
result = curl_easy_perform(handle);
/* Check for errors */
if(result != CURLE_OK)
{
/* if errors have occured, tell us wath's wrong with 'result'*/
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(result));
return 1;
}
}
/* if something's gone wrong with curl at the beginning, we'll appriciate that piece of code */
else
{
fprintf(stderr, "Curl init failed!\n");
return 1;
}
/* cleanup since you've used curl_easy_init */
curl_easy_cleanup(handle);
/* this function releases resources acquired by curl_global_init() */
curl_global_cleanup();
/* make the programme stopping for avoiding the console closing befor you can see anything */
system("PAUSE");
return 0;
}
そのハンドルをまったく別の目的で再利用したい場合は、別の CURL easy ハンドルを使用することをお勧めします。それでもコードは正常に動作するはずですが、明らかに 2 つの別々の操作であるため、別のハンドルを使用します。
ただし、同じハンドルで作業する必要がある場合があり、自動的にリセットしたくない場合は、適切な関数を使用してください。
void curl_easy_reset(CURL *handle);
ハンドルからのライブ接続、セッション ID キャッシュ、DNS キャッシュ、Cookie および共有は変更されないことに注意してください。
私はそれを試していませんが、あなたのコードでは次のようなものが得られるはずです:
#include <curl.h>
int main(void)
{
CURL *handle;
CURLcode result;
int error = 0;
int error2 = 0;
curl_global_init(CURL_GLOBAL_ALL);
handle = curl_easy_init();
if(handle)
{
curl_easy_setopt(handle, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; fr; rv:1.9.2) Gecko/20100115 Firefox/3.6");
curl_easy_setopt(handle, CURLOPT_URL, "http://www.google.com");
result = curl_easy_perform(handle);
if(result != CURLE_OK)
{
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(result));
error++;
}
Sleep(5000); // make a pause if you working on console application
curl_easy_reset(handle);
curl_easy_setopt(handle, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; fr; rv:1.9.2) Gecko/20100115 Firefox/3.6"); // have to write it again
curl_easy_setopt(handle, CURLOPT_URL, "http://www.bbc.com");
result = curl_easy_perform(handle);
if(result != CURLE_OK)
{
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(result));
error2++;
}
if(error == 1 || error2 == 1)
{
return 1;
}
}
else
{
fprintf(stderr, "Curl init failed!\n");
return 1;
}
curl_easy_cleanup(handle);
curl_global_cleanup();
system("PAUSE");
return 0;
}
に問題がある場合は、5000 を 5 に置き換えるか、5000 を 5Sleep
に置き換えてみてください。sleep
_sleep