• <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>

            浪跡天涯

            唯有努力...
            努力....再努力...

            libcurl使用心得

            Libcurl為一個(gè)免費(fèi)開源的,客戶端url傳輸庫,支持FTPFTPSTFTPHTTPHTTPSGOPHERTELNETDICTFILELDAP,跨平臺(tái),支持WindowsUnixLinux等,線程安全,支持Ipv6。并且易于使用。

            http://curl.haxx.se/libcurl/

            http://curl.haxx.se/libcurl/ 下載一個(gè)穩(wěn)定的版本,注意選擇OS
            在使用之前請(qǐng)大家多閱讀libcurl的文檔:因?yàn)槿绻獙?shí)際運(yùn)用到項(xiàng)目中,最好對(duì)libcurl有具體的了解,具體在
            http://curl.haxx.se/libcurl/c/
            curl_easy_setopt()
            curl_easy_perform()
            curl_easy_getinfo()
            這三個(gè)函數(shù)的使用上,需要多去鉆研,多看Samples,你才能靈活使用libcurl。
            感謝這篇文章:
            http://blog.163.com/xu_chao2000/blog/static/27770610200801303252802/
            給了我許多啟發(fā),再次感謝!

            給出我的一個(gè)簡(jiǎn)單的代碼例子:
            說明:
            1.關(guān)鍵在curl_easy_setopt函數(shù)設(shè)置option,可以設(shè)置ftp,http,get,post等許多選項(xiàng),請(qǐng)根據(jù)具體使用情況設(shè)置。

            2.對(duì)取回來的數(shù)據(jù)需要進(jìn)行判斷,比如http下載文件,如果文件不存在,需要進(jìn)行處理。因?yàn)閣riter是可以將buf填充404 not found等網(wǎng)頁內(nèi)容的,不能將這個(gè)內(nèi)容當(dāng)成文件內(nèi)容,所以需要判斷http web返回來的code,進(jìn)行判斷。

            3.我有個(gè)問題,就是想得到服務(wù)器上filename的具體名稱,verbose調(diào)試已經(jīng)返回了,但是我在getinfo的時(shí)候,試過好多選項(xiàng),但未找到這個(gè)存放真實(shí)服務(wù)器文件名的選項(xiàng),如果有知道的麻煩告訴我,謝謝了!

            #include "curl/curl.h"
            #pragma comment(lib, 
            "libcurl.lib")

            long writer(void
            *data, int size, int nmemb, string &content);
            bool  CurlInit(CURL 
            *&curl, const char* url,string &content);
            bool  GetURLDataBycurl(const char* URL, string 
            &content);

            void main()
            {
                char *url ="http://www.baidu.com/img/baidu.gif";
                string content;
                
            if ( GetURLDataBycurl(url,content))
                
            {
                    printf(
            "%s\n",content);

                }

                getchar();
            }


            bool CurlInit(CURL 
            *&curl, const char* url,string &content)
            {
                CURLcode code;
                string error;
                curl 
            = curl_easy_init();
                
            if (curl == NULL)
                
            {
                    printf( 
            "Failed to create CURL connection\n");
                    
            return false;
                }

                code 
            = curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
                
            if (code != CURLE_OK)
                
            {
                    printf( 
            "Failed to set error buffer [%d]\n", code );
                    
            return false;
                }

                curl_easy_setopt(curl, CURLOPT_VERBOSE, 
            1L);
                code 
            = curl_easy_setopt(curl, CURLOPT_URL, url);
                
            if (code != CURLE_OK)
                
            {
                    printf(
            "Failed to set URL [%s]\n", error);
                    
            return false;
                }

                code 
            = curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
                
            if (code != CURLE_OK)
                
            {
                    printf( 
            "Failed to set redirect option [%s]\n", error );
                    
            return false;
                }

                code 
            = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
                
            if (code != CURLE_OK)
                
            {
                    printf( 
            "Failed to set writer [%s]\n", error);
                    
            return false;
                }

                code 
            = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content);
                
            if (code != CURLE_OK)
                
            {
                    printf( 
            "Failed to set write data [%s]\n", error );
                    
            return false;
                }

                
            return true;
            }


            long writer(void
            *data, int size, int nmemb, string &content)
            {
                long sizes 
            = size * nmemb;
                string temp(data,sizes);
                content 
            += temp; 
                
            return sizes;
            }


            bool GetURLDataBycurl(const char* URL,  string
            &content)
            {
                CURL 
            *curl = NULL;
                CURLcode code;
                string error;

                code 
            = curl_global_init(CURL_GLOBAL_DEFAULT);
                
            if (code != CURLE_OK)
                
            {
                    printf( 
            "Failed to global init default [%d]\n", code );
                    
            return false;
                }
             
                
                
            if ( !CurlInit(curl,URL,content) )
                
            {
                    printf( 
            "Failed to global init default [%d]\n" );
                    
            return PM_FALSE;
                }

                code 
            = curl_easy_perform(curl);
                
            if (code != CURLE_OK)
                
            {
                    printf( 
            "Failed to get '%s' [%s]\n", URL, error);
                    
            return false;
                }

                long retcode 
            = 0;
                code 
            = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE , &retcode); 
                
            if ( (code == CURLE_OK) && retcode == 200 )
                
            {
                    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 true;
                }

                
            else
                
            {
                
            //    debug1( "%s \n ",getStatusCode(retcode));
                    return false;
                }

                curl_easy_cleanup(curl);
                
            return false;
            }

            posted on 2008-06-28 14:50 浪跡天涯 閱讀(79500) 評(píng)論(18)  編輯 收藏 引用 所屬分類: Lib

            評(píng)論

            # re: libcurl使用心得 2008-06-28 15:42 LOGOS

            看起來能用來做蜘蛛  回復(fù)  更多評(píng)論   

            # re: libcurl使用心得 2008-06-28 16:00 浪跡天涯

            有用這個(gè)寫爬蟲的  回復(fù)  更多評(píng)論   

            # re: libcurl使用心得 2008-07-01 12:48 企業(yè)即時(shí)通訊

            太厲害了。哈哈。  回復(fù)  更多評(píng)論   

            # re: libcurl使用心得 2009-09-04 23:07 紫楓閑人

            支持樓主,感謝分享,幫了大忙了。  回復(fù)  更多評(píng)論   

            # re: libcurl使用心得 2009-09-09 18:55 druselyy

            多線程的效果怎么樣  回復(fù)  更多評(píng)論   

            # re: libcurl使用心得 2010-08-02 12:10 ShereeHamilton22

            I think that to get the <a href="http://bestfinance-blog.com/topics/personal-loans">personal loans</a> from creditors you must have a good reason. Nevertheless, one time I have got a student loan, because I wanted to buy a building.   回復(fù)  更多評(píng)論   

            # re: libcurl使用心得 2010-08-22 13:49 able

            樓主的代碼高亮工具我一直沒找到
            不知道樓主方便傳一份不 謝過
            mail: 645989637@qq.com  回復(fù)  更多評(píng)論   

            # re: libcurl使用心得 2011-03-23 09:45 aaa

            樓主你確信你的可用么,代碼很有問題啊。。。。  回復(fù)  更多評(píng)論   

            # re: libcurl使用心得[未登錄] 2012-02-03 17:41 rock

            經(jīng)檢查運(yùn)行測(cè)試, 無法得到結(jié)果,顯示錯(cuò)誤:
            Failed to get ="http://www.baidu.com/img/baidu.gif" []

              回復(fù)  更多評(píng)論   

            # re: libcurl使用心得[未登錄] 2012-02-03 18:11 rock

            運(yùn)行正確,是我錯(cuò)了,不好意思!  回復(fù)  更多評(píng)論   

            # re: libcurl使用心得 2012-04-04 23:03 小寧

            樓主CURL代碼不能用于斷點(diǎn)續(xù)傳
            樓主如果想得到服務(wù)器上的文件名可以在響應(yīng)頭中提取
            希望能和使用LIBCURL的同學(xué)一起交流下
            QQ群49184341  回復(fù)  更多評(píng)論   

            # re: libcurl使用心得 2012-05-10 19:42 北漂一時(shí)

            @druselyy多線程效果并沒有multi handle效果好。原因是多線程之間還存在線程的切換,而multi handle是批處理,接收數(shù)據(jù)后一次性全部寫入,不用像多線程一樣在多個(gè)文件之家來回移動(dòng)磁頭——其實(shí)這個(gè)是比較耗時(shí)的。
              回復(fù)  更多評(píng)論   

            # re: libcurl使用心得 2012-05-23 16:02

            在多線程中調(diào)用easy handle, POST數(shù)據(jù)時(shí),有時(shí)根本發(fā)不出數(shù)據(jù)是怎么回事?總是有丟數(shù)據(jù)包的現(xiàn)象發(fā)生,請(qǐng)問各位高手怎么解決?  回復(fù)  更多評(píng)論   

            # re: libcurl使用心得 2012-11-15 10:30 P2P下載

            謝謝分享  回復(fù)  更多評(píng)論   

            # re: libcurl使用心得[未登錄] 2013-04-03 21:46 劉偉

            如果網(wǎng)頁中有中文呢?我這邊調(diào)試的是亂碼,該如何處理呢?  回復(fù)  更多評(píng)論   

            # re: libcurl使用心得[未登錄] 2013-12-06 15:18 曾是土木人

            不錯(cuò),對(duì)我使用libcurl挺有幫助的,感謝博主的分享精神!  回復(fù)  更多評(píng)論   

            # re: libcurl使用心得[未登錄] 2013-12-11 19:07 lin

            謝謝博主,參考你的例程成功下載到了文件。萬分感謝博主的分享精神。  回復(fù)  更多評(píng)論   

            # re: libcurl使用心得 2014-10-24 13:51 da

            http://www.seanyxie.com/libcurl%E4%B8%8B%E8%BD%BD%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95/  回復(fù)  更多評(píng)論   

            <2009年7月>
            2829301234
            567891011
            12131415161718
            19202122232425
            2627282930311
            2345678

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿(22)

            隨筆分類(30)

            隨筆檔案(29)

            文章分類

            搜索

            積分與排名

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            色天使久久综合网天天| 久久亚洲av无码精品浪潮| 欧美亚洲日本久久精品| 亚洲狠狠综合久久| 久久国产亚洲精品麻豆| 漂亮人妻被黑人久久精品| 超级碰碰碰碰97久久久久| 亚洲国产一成久久精品国产成人综合 | 久久久久99这里有精品10| 久久久久久亚洲精品不卡| 久久成人18免费网站| 精品国产一区二区三区久久蜜臀| 99久久精品九九亚洲精品| 久久免费视频观看| 国内精品久久久久久不卡影院| 亚洲综合久久综合激情久久 | 久久久久久狠狠丁香| 香港aa三级久久三级| 婷婷综合久久中文字幕| 99久久成人18免费网站| 久久e热在这里只有国产中文精品99| 国产成人精品久久一区二区三区av | 久久99国产精品久久99| 99久久成人18免费网站| 久久97久久97精品免视看| 午夜精品久久久久成人| 国产精品99久久久精品无码| 久久精品国产亚洲av日韩| 色偷偷888欧美精品久久久| 久久精品成人| 免费精品久久天干天干| 精品久久久久久无码专区| 99久久精品费精品国产| 久久大香萑太香蕉av| 丁香狠狠色婷婷久久综合| 久久久久久极精品久久久| 一本一本久久aa综合精品 | 国内精品久久久久影院网站| 欧美亚洲国产精品久久| 国产精品久久自在自线观看| 亚洲国产天堂久久综合|