• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            成長的歲月我是風

            踏踏實實做人,勤勤懇懇做事.

            常用鏈接

            統計

            最新評論

            curl的使用實例 cpp封裝【轉】

            關于curl的基本用法請見curl的官網(http://curl.haxx.se/) 以及我的前一篇轉載別人的博文:http://blog.chinaunix.net/u/16292/showart_1951736.html 。

            本文給出一個 curl 的簡單 c++ 封裝類,以及測試用例。

            頭文件:

            /**
             * @file HCUrl.h
             *
             * @brief
             * The CCUrl class header file.</br>
             *
             * @author zieckey
             *
             * @date Jun. 3 2009
             *
             * @version 0.0.1
             *
             * Revision History
             * @if CR/PR ID Author    Date    Major Change @endif
             * @bug \n
            ***************************************************************************
            */

            #ifndef __CCUrl_H__
            #define __CCUrl_H__

            #include 
            <curl/curl.h>
            #include 
            <curl/types.h>
            #include 
            <curl/easy.h>

            #include 
            <string.h>

            namespace OSLib
            {
                
            //const long CLIENTTIMEOUT = 10;


                
            /*! @class CCUrl CCUrl.h "CCUrl.h"
                * @brief This is the CCUrl class, a wrapper of libcurl.
                *
                
            */
                
            class CCUrl
                {
                
            public:
                    CCUrl();
                    
            virtual ~CCUrl();
                
                    
            /**
                    * @brief <b>Summary:</b>
                    *    Initiate this CURL session
                    * @return    0, if success. <br>
                    *            -1, if failed.
                    ******************************************************************************
            */
                    
            int    init();
                
                
            protected:
                    
            /**
                    * @brief <b>Summary:</b>
                    *        This function is virtual function, which will be called by automatic, 
                    *    as soon as there is data received that needs to be processed.<br>
                    *        The size of the data pointed to by 'data' is 'size' multiplied with 'nmemb', 
                    *    it will not be zero terminated. <br>
                    * @note    You should override this function to do you own busyness. 
                    *        You save the data to a file, or process it anyway.
                    * @param[in] data, point to the data
                    * @param[in] size, the size        
                    * @param[in] nmemb, the unit in bytes        
                    * @return 
                    ****************************************************************************
            */
                    
            virtual int process( void* data, size_t size, size_t nmemb );

                
            public:
                    
            /**
                    * @brief <b>Summary:</b>
                    *    Post 'data' to 'url', and the server response data is restored in resultData.
                    *    The process function is 'pfun' 
                    * @param[in]    url
                    * @param[in]    data, the data to be sent to server. 
                    *                    eg. "id=admin&passwd=123456"
                    * @param[in]    timeout, the limit time in seconds to do this process.
                    *                    default value 10
                    * @param[in]    withheader, 
                    *                    1 indicate the server response data including header
                    *                    0 indicate the server response data excluding header
                    *                    default value 0
                    * @return    0, if success. </br>
                    *            -1, if failed.
                    ******************************************************************************
            */
                    
            int doHttpPost( const char* url, const char* data, long timeout = 10long withheader = 0 );

                    
            /**
                    * @brief <b>Summary:</b>
                    *    Get 'url' using 'data' to be set in the header, 
                    *    and the server response data is restored in resultData.
                    *    The process function is 'pfun' 
                    * @param[in]    url
                    * @param[in]    data, the data to be sent to server. 
                    *                    eg. "id=admin&passwd=123456"
                    * @param[in]    timeout, the limit time in seconds to do this process.
                    *                    default value 10
                    * @param[in]    withheader, 
                    *                    1 indicate the server response data including header
                    *                    0 indicate the server response data excluding header
                    *                    default value 0
                    * @return    0, if success. </br>
                    *            -1, if failed.
                    ******************************************************************************
            */
                    
            int doHttpGet( const char* url, const char* data, long timeout = 10long withheader = 0 );


                
            public:
                    
            //CURLcode setOpt( CURLoption option ); 


                    
            /**
                    * @brief <b>Summary:</b>
                    *    Reset all options of a libcurl session handle
                    * @return void, no return
                    ******************************************************************************
            */
                    
            void resetOpt(); 
                    
                
            private:
                    
            /**
                    * @brief <b>Summary:</b>
                    *        This function gets called by libcurl as soon as there is data received
                    *    that needs to be saved.<br>
                    *        The size of the data pointed to by ptr is size multiplied with nmemb, 
                    *    it will not be zero terminated. <br>
                    * @param[in] 
                    * @param[in]    
                    * @param[in]    
                    * @param[in] usrptr, the user ptr, 
                    *            set by option CURLOPT_READDATA or CURLOPT_WRITEDATA
                    * @return    the number of bytes actually taken care of. <br>
                    *        If that amount differs from the amount passed to your function,
                    *    it'll signal an error to the library and it will abort the transfer 
                    *    and return CURLE_WRITE_ERROR. 
                    ****************************************************************************
            */
                    
            static size_t processFunc( void* ptr, size_t size, size_t nmemb, void *usrptr );

                
            private:
                    CURL 
            * m_curl;
                };    
            };

            #endif

            實現文件:

            #include "HCUrl.h"

            namespace OSLib
            {

                CCUrl::CCUrl() : m_curl(NULL)
                {
                }
                
                CCUrl::
            ~CCUrl()
                {
                    ::curl_easy_cleanup( m_curl ); 
                    ::curl_global_cleanup(); 
                }

                
            int CCUrl::init()
                {
                    CURLcode res 
            = ::curl_global_init( CURL_GLOBAL_ALL );
                    
            if( CURLE_OK != res ) 
                    {
                        fprintf( stderr, 
            "curl_global_init failed: %d \n", res ); 
                        
            return -1;
                    }
                
                    m_curl 
            = ::curl_easy_init(); 
                    
            if( NULL == m_curl )
                    {
                        fprintf( stderr, 
            "curl_easy_init failed\n" ); 
                        
            return -1;
                    }
                    
            return 0;
                }


                
            int CCUrl::doHttpPost( const char* url, const char* data, long timeout, long withheader )
                {
                    resetOpt();
                    ::curl_easy_setopt( m_curl, CURLOPT_HEADER, withheader );
                    ::curl_easy_setopt( m_curl, CURLOPT_URL, url );
                    ::curl_easy_setopt( m_curl, CURLOPT_POST, 
            1 );
                    ::curl_easy_setopt( m_curl, CURLOPT_POSTFIELDS, data );
                    ::curl_easy_setopt( m_curl, CURLOPT_TIMEOUT, timeout );
                    ::curl_easy_setopt( m_curl, CURLOPT_WRITEFUNCTION, CCUrl::processFunc ); 
                    ::curl_easy_setopt( m_curl, CURLOPT_WRITEDATA, 
            this );
                    CURLcode res 
            = ::curl_easy_perform( m_curl);
                    
            return ( 0 == res ) ? 0 : -1;
                }

                
            int CCUrl::doHttpGet( const char* url, const char* data, long timeout, long withheader )
                {
                    resetOpt();
                    size_t urllen 
            = strlen( url );
                    size_t datalen 
            = strlen( data );
                    
            char* surl = new char[ urllen + datalen + 1 + 1];
                    
            if!surl )
                    {
                        fprintf( stderr, 
            "doHttpGet failed, unable to malloc memery\n" ); 
                        
            return -1;
                    }
                    strncpy( surl, url, urllen );
                    surl[ urllen ] 
            = '?';
                    strcpy( surl 
            + urllen + 1, data );
                    ::curl_easy_setopt( m_curl, CURLOPT_URL, surl );
                    ::curl_easy_setopt( m_curl, CURLOPT_HTTPGET, 
            1 );
                    ::curl_easy_setopt( m_curl, CURLOPT_HEADER, withheader );
                    ::curl_easy_setopt( m_curl, CURLOPT_TIMEOUT, timeout );
                    ::curl_easy_setopt( m_curl, CURLOPT_WRITEFUNCTION, CCUrl::processFunc ); 
                    ::curl_easy_setopt( m_curl, CURLOPT_WRITEDATA, 
            this ); 

                    CURLcode res 
            = ::curl_easy_perform( m_curl);

                    delete [] surl;
                    
            return ( 0 == res ) ? 0 : -1;
                }

                
            void CCUrl::resetOpt()
                {
                    ::curl_easy_reset( m_curl );
                    
            //::curl_easy_setopt( m_curl, CURLOPT_VERBOSE, 1 );//set this to show debug message

                }

                size_t CCUrl::processFunc( 
            void* ptr, size_t size, size_t nmemb, void *usrptr )
                {
                    CCUrl
            * p =(CCUrl*) usrptr;
                    
            return p->process( ptr, size, nmemb );
                }
                


                
            int CCUrl::process( void* data, size_t size, size_t nmemb ) 
                {
                    printf( 
            "\n ===== CCUrl::process( void* data, size_t size, size_t nmemb ) ===== \n" ); 
                    
            return size * nmemb; 
                }

            //     int CCUrl::uploadFile( const char* url, const char* localfilename )

            //     {

            //         m_upfilename = (char*)localfilename;

            //         resetOpt();

            //         ::curl_easy_setopt( m_curl, CURLOPT_URL, url );/**<Upload to this url place*/

            //         ::curl_easy_setopt( m_curl, CURLOPT_UPLOAD, 1 );

            //         ::curl_easy_setopt( m_curl, CURLOPT_READFUNCTION, CCUrl::uploadFileReadFunc ); 

            //         ::curl_easy_setopt( m_curl, CURLOPT_READDATA, this ); 

            //         CURLcode res = ::curl_easy_perform( m_curl);

            //         return ( 0 == res ) ? 0 : -1;

            //     }

            //

            //     size_t CCUrl::uploadFileReadFunc( void* ptr, size_t size, size_t nmemb, void *usrptr )

            //     {

            //         CCUrl* p =(CCUrl*) usrptr;

            //         static FILE* pf = NULL;

            //         if( !pf ) 

            //         {

            //             pf = fopen( p->m_upfilename, "r" );

            //             if( !pf )    

            //             {

            //                 fprintf( stderr, "cannot open file: %s \n", p->m_upfilename ); 

            //                 return 0;

            //             }

            //         }

            // 

            //         if( pf )

            //         {

            //             int nread = fread( ptr, size, nmemb, pf );

            //             if( !nread )

            //             {

            //                 fclose( pf );

            //                 pf = NULL;    

            //             }

            //             

            //             return nread;

            //         }

            // 

            //         return 0;

            //     }



            }; 
            //end namespace

            測試程序:

            #include "HCUrl.h"

            #include 
            <string.h>

            using namespace OSLib;

            class MyCCUrl : public CCUrl
            {
            protected:
                
            int process( void* data, size_t size, size_t nmemb )
                {
                    
            //printf( "\n================entering size=%d================\n", size * nmemb );

                    printf( 
            "%s", (char*)data );
                    
            return size * nmemb;
                }    
            };


            void testCCUrlDoHttpPost( const char* username, const char* password );
            void testCCUrlDoHttpGet( const char* name, const char* age );
            void testGlobalCleanup( const char* username, const char* password );
            void testCCUrlUploadFile( const char* filename );

            int main( int argc, char* argv[] )
            {
            //     if( argc < 3 )        

            //     {

            //         printf( "usage:%s username password\n", argv[0] );

            //         return 0;

            //     }

            //     testCCUrlDoHttpPost( argv[1], argv[2] );



                
            if( argc < 3 )        
                {
                    printf( 
            "usage:%s name age\n", argv[0] );
                    
            return 0;
                }
                testCCUrlDoHttpGet( argv[
            1], argv[2] );

            //     if( argc < 3 )        

            //     {

            //         printf( "usage:%s username password\n", argv[0] );

            //         return 0;

            //     }

            //     testGlobalCleanup( argv[1], argv[2] );

                
            //     if( argc < 2 )        

            //     {

            //         printf( "usage:%s filename\n", argv[0] );

            //         return 0;

            //     }

            //     testCCUrlUploadFile( argv[1] );

                
                
            return 0;
            }


            void testCCUrlDoHttpPost( const char* username, const char* password )
            {
                MyCCUrl cu;
                cu.init();
                
            char data[1024];
                sprintf( data, 
            "id=%s&passwd=%s", username, password );
                
            char buf[1024 * 1024];
                
            char* url = "http://forum.byr.edu.cn/wForum/logon.php";
                cu.doHttpPost( url, data, 
            10 );


            void testCCUrlDoHttpGet( const char* name, const char* age )
            {
                MyCCUrl cu;
                cu.init();
                
            char data[1024];
                sprintf( data, 
            "name=%s&age=%s", name, age );
                
            char buf[1024 * 1024];
                
            char url[1024];
                sprintf( url, 
            "http://192.168.3.200:8080/action.get.php", name, age );
                cu.doHttpGet( url, data, 
            101 );


            // void testCCUrlUploadFile( const char* filename )

            // {

            //     CCUrl cu;

            //     cu.init();

            //     char url[1024];

            //     sprintf( url, "http://192.168.3.200:8080/%s", filename );

            //     cu.uploadFile( url, filename );

            // } 



            void testGlobalCleanup( const char* username, const char* password )
            {
                
            for ( int i = 0; i < 10; i ++ )
                {
                    MyCCUrl
            * cu = new MyCCUrl;
                    cu
            ->init();
                    
            char data[1024];
                    sprintf( data, 
            "id=%s&passwd=%s", username, password );
                    
            char* url = "http://forum.byr.edu.cn/wForum/logon.php";
                    cu
            ->doHttpPost( url, data, 10 );
                
                    printf( 
            "\n\n\n=============================================\n\n\n" );
                
                    MyCCUrl
            * cu2 = new MyCCUrl;
                    
                    cu2
            ->init();
                    sprintf( data, 
            "name=%s&age=%s", username, password );
                    url 
            = "http://192.168.3.200:8080/action.get.php";
                    
                    delete cu;    
            //check after deleting cu, whether cu2 is ok 


                    cu2
            ->doHttpGet( url, data, 10 );
                    
                    delete cu2;
                }

                
            for ( int i = 0; i < 10; i ++ )
                {
                    MyCCUrl
            * cu = new MyCCUrl;
                    cu
            ->init();
                    
            char data[1024];
                    
            char* url = 0;
                    
                
                    printf( 
            "\n\n\n=============================================\n\n\n" );
                
                    MyCCUrl
            * cu2 = new MyCCUrl;
                    
                    cu2
            ->init();
                    sprintf( data, 
            "name=%s&age=%s", username, password );
                    url 
            = "http://192.168.3.200:8080/action.get.php";
                    cu2
            ->doHttpGet( url, data, 10 );
                    delete cu2;
                    
                    url 
            = "http://forum.byr.edu.cn/wForum/logon.php";
                    sprintf( data, 
            "id=%s&passwd=%s", username, password );
                    cu
            ->doHttpPost( url, data, 10 );
                    delete cu;    
            //check after deleting cu, whether cu2 is ok 

                }
            }

            測試中用到了一個php腳本,用于web服務器端解析:

            <html>
                 
            <head>
                  
            <title>PHP action</title>
                 
            </head>
                 
            <body>
                    your name is: 
            <?php echo htmlspecialchars($_GET['name']); ?> </br>
                    your age is: 
            <?php echo (int)$_GET['age']; ?>
            </body>
            </html>

            可以把main函數中的測試get的方法注釋掉,把測試post的部分代碼打開,就可以測試post了。post的測試例子用到的url是一個論壇的登錄url。直接用就可以了。


            原文地址:http://blog.chinaunix.net/uid-20384806-id-1954334.html

            posted on 2013-01-12 13:11 若風之觴 閱讀(3390) 評論(0)  編輯 收藏 引用

            久久精品水蜜桃av综合天堂| 日本久久中文字幕| 亚洲?V乱码久久精品蜜桃| 久久久噜噜噜久久中文字幕色伊伊| 亚洲国产精品无码成人片久久| 99久久精品免费看国产一区二区三区 | 久久久久99精品成人片| 99精品久久久久久久婷婷| 久久精品国产黑森林| 久久国产成人精品麻豆| 久久精品aⅴ无码中文字字幕不卡 久久精品aⅴ无码中文字字幕重口 | 精品久久久无码人妻中文字幕豆芽 | 久久99精品国产麻豆宅宅| 浪潮AV色综合久久天堂| 成人久久免费网站| 久久国产色AV免费观看| 国产精品无码久久综合| 久久久久亚洲AV成人片| 久久天天躁狠狠躁夜夜躁2O2O| 久久强奷乱码老熟女网站| 久久久久久久波多野结衣高潮| 久久久国产亚洲精品| 97香蕉久久夜色精品国产| 久久精品国产久精国产一老狼| 久久国产劲爆AV内射—百度| 久久伊人五月天论坛| 欧美性猛交xxxx免费看久久久 | 色综合久久88色综合天天 | 久久99精品国产麻豆婷婷| 精品国产乱码久久久久久浪潮| 国产香蕉97碰碰久久人人| 日韩久久无码免费毛片软件| 99久久国产综合精品五月天喷水| 欧美亚洲另类久久综合| 久久久WWW免费人成精品| 久久这里的只有是精品23| 久久无码人妻一区二区三区| 狠狠色婷婷久久综合频道日韩| 久久精品aⅴ无码中文字字幕不卡 久久精品aⅴ无码中文字字幕重口 | 日产精品久久久一区二区| 国产精品久久自在自线观看|