如何正确重复使用卷曲句柄 [英] how to properly reuse a curl handle

查看:454
本文介绍了如何正确重复使用卷曲句柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想要正确地重复使用curl句柄,以便它不会给我错误和正常的功能。假设我有这段代码:

  CURL * curl; 

curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();

curl_easy_setopt(curl,CURLOPT_USERAGENT,Mozilla / 5.0 ...);
curl_easy_setopt(curl,CURLOPT_URL,http://www.google.com);
curl_easy_perform(curl);

curl_easy_setopt(curl,CURLOPT_URL,http://www.bbc.com);
curl_easy_perform(curl);

curl_easy_cleanup(curl);
curl_global_cleanup();

这是一个好的或正确的重用curl句柄的方法吗?或者我需要在该句柄上使用 curl_easy_reset()吗?



应该避免做卷曲。也许有人可以给我一个已经存在的信息源的链接。

解决方案

当你使用环境libcurl的easy界面,你必须先调用:




  • curl_easy_init() $ ,大多数情况下标志选项必须 CURL_GLOBAL_ALL c $ c>



这两个函数在开头只调用一次,需要相反的清除:




  • curl_easy_cleanup()当您完成已声明的句柄后,

  • curl_global_cleanup()当您完成libcurl时,



为了更好的结果,尽可能多地检查错误。 Libcurl提供 curl_easy_strerror()函数。它返回一个描述CURLcode错误的字符串。另外,如果一切正常,一些函数返回值
CURL_OK或一个特定的整数。



例如,下面是使用CURLOPT_URL选项的正确方法: p>

  #include< curl.h> 

int main(void)
{
/ *对象的声明CURL * /
CURL * handle;

/ *整个过程的结果* /
CURLcode result;

/ *第一个函数* /
/ *设置libcurl需要的程序环境* /
curl_global_init(CURL_GLOBAL_ALL);
/ * curl_easy_init()返回一个CURL的简单句柄,你将在其他简单的函数中重用* /
handle = curl_easy_init();



/ *如果一切顺利,使用easy handle ... * /
if(handle)
{
/ * ...你可以列出简单的函数* /
/ *这里我们试图获取http://example.com的源代码* /
curl_easy_setopt(handle,CURLOPT_URL,http: //example.com);

/ *但在这种情况下,我们还告诉libcurl跟随重定向* /
curl_easy_setopt(handle,CURLOPT_FOLLOWLOCATION,1L);

/ *执行,然后将期望的代码存储在'result'* /
result = curl_easy_perform(handle);

/ *检查错误* /
if(result!= CURLE_OK)
{
/ *如果错误发生,告诉我们wath'wrong' * /
fprintf(stderr,curl_easy_perform()failed:%s\\\
,curl_easy_strerror(result));

return 1;
}
}
/ *如果在开始时curl出现错误,我们会授予这段代码* /
else
{
fprintf(stderr,Curl init failed!\\\
);

return 1;
}

/ *清除,因为你使用了curl_easy_init * /
curl_easy_cleanup(handle);

/ *此函数释放curl_global_init()获取的资源* /
curl_global_cleanup();

/ *让程序停止,以避免控制台关闭befor你可以看到任何* /
系统(PAUSE);

return 0;
}

如果你想重用这个句柄完全不同的目的,使用不同的CURL
简单句柄。
仍然是你的代码应该工作正常,但我会使用不同的句柄,因为它显然是两个单独的操作。



但是有时你需要使用相同的句柄如果您不想自动重置,请使用相应的函数:

  void curl_easy_reset(CURL * handle); 

请注意,它不会更改实时连接,会话ID缓存,DNS缓存,



我没有尝试过,但是你的代码应该给我们这样的东西:

  #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\\\
,curl_easy_strerror(result));

错误++;
}

Sleep(5000); //如果您在控制台应用程序上工作,请暂停

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); //必须再写一次
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\\\
,curl_easy_strerror(result));

error2 ++;
}

if(error == 1 || error2 == 1)
{
return 1;
}
}
else
{
fprintf(stderr,Curl init failed!\\\
);

return 1;
}

curl_easy_cleanup(handle);
curl_global_cleanup();

系统(PAUSE);

return 0;
}

如果您在 Sleep ,尝试用 sleep _sleep 替换它或将5000替换为5。


I want to properly reuse a curl handle, so that it won't give me errors and function normally.

Suppose I have this piece of code:

    CURL *curl;

    curl_global_init(CURL_GLOBAL_ALL);
    curl = curl_easy_init();

    curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/5.0...");
    curl_easy_setopt(curl, CURLOPT_URL, "http://www.google.com");
    curl_easy_perform(curl);

    curl_easy_setopt(curl, CURLOPT_URL, "http://www.bbc.com");
    curl_easy_perform(curl);

    curl_easy_cleanup(curl);
    curl_global_cleanup();

Would this be a good or correct way of reusing a curl handle? Or do I need to use curl_easy_reset() on that handle?

I would also appreciate if anyone suggested what you should avoid doing in curl. Maybe someone could give me a link to an already existing source of information?

解决方案

When you use the environment libcurl on the easy interface, you first have to call :

  • curl_easy_init(), which init the easy handle,
  • curl_global_init(), most of the case the flag option has to be CURL_GLOBAL_ALL

Each of those two functions is called just once at the beginning and need their opposite clean up :

  • curl_easy_cleanup() when you've finished handles you've declare,
  • curl_global_cleanup() when you're done with libcurl,

For better results check errors as much as you can. Libcurl provides curl_easy_strerror() function for that. It returns a string describing the CURLcode error. Also, some functions return the value CURL_OK or a specific integer if everything is OK.

For instance, here's the proper way to use CURLOPT_URL option :

#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;
}

If you want to reuse that handle for a totally different purpose you'd better use different CURL easy handles. Still your code should work fine but i would use different handles because it's obviously two seperate operations.

However sometimes you'll need to work with the same handle and if you don't want to do reset it automatically, use the appropriate function :

void curl_easy_reset(CURL *handle); 

Note that it does not change live connections, the Session ID cache, the DNS cache, the cookies and shares from the handle.

I haven't tried it but with your code it should give us something like that :

#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;
}

If you have any problem with Sleep, try to replace it by sleep or _sleep or replace 5000 by 5.

这篇关于如何正确重复使用卷曲句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆