• <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)開(kāi)源的,客戶端url傳輸庫(kù),支持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)單的代碼例子:
            說(shuō)明:
            1.關(guān)鍵在curl_easy_setopt函數(shù)設(shè)置option,可以設(shè)置ftp,http,get,post等許多選項(xiàng),請(qǐng)根據(jù)具體使用情況設(shè)置。

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

            3.我有個(gè)問(wèn)題,就是想得到服務(wù)器上filename的具體名稱,verbose調(diào)試已經(jīng)返回了,但是我在getinfo的時(shí)候,試過(guò)好多選項(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 浪跡天涯 閱讀(79499) 評(píng)論(18)  編輯 收藏 引用 所屬分類: Lib

            評(píng)論

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

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

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

            有用這個(gè)寫(xiě)爬蟲(chóng)的  回復(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

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

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

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

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

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

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

            # re: libcurl使用心得[未登錄](méi) 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多線程效果并沒(méi)有multi handle效果好。原因是多線程之間還存在線程的切換,而multi handle是批處理,接收數(shù)據(jù)后一次性全部寫(xiě)入,不用像多線程一樣在多個(gè)文件之家來(lái)回移動(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)問(wèn)各位高手怎么解決?  回復(fù)  更多評(píng)論   

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

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

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

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

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

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

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

            謝謝博主,參考你的例程成功下載到了文件。萬(wàn)分感謝博主的分享精神。  回復(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)論   

            <2008年6月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            293012345

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿(22)

            隨筆分類(30)

            隨筆檔案(29)

            文章分類

            搜索

            積分與排名

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            996久久国产精品线观看| 国产精品久久永久免费| 久久免费小视频| 久久综合给合久久国产免费| 久久无码AV中文出轨人妻| 久久久久久国产精品美女| 久久精品国产福利国产秒| 久久国产精品久久精品国产| 97久久久久人妻精品专区| 国产V综合V亚洲欧美久久| 国产成人久久精品一区二区三区| 久久久久亚洲av无码专区喷水| 狠狠综合久久综合88亚洲| 伊人色综合久久天天人手人婷 | 国产∨亚洲V天堂无码久久久| 少妇人妻88久久中文字幕| 久久精品亚洲日本波多野结衣 | 色婷婷久久综合中文久久一本| 久久免费99精品国产自在现线| 午夜精品久久久内射近拍高清| 一本色道久久88综合日韩精品| 久久久噜噜噜久久中文字幕色伊伊| 国产A级毛片久久久精品毛片| 久久精品国产精品亚洲毛片| 99久久精品国产一区二区| 午夜视频久久久久一区| 精品久久久久久国产| 久久精品国产亚洲av影院| 久久精品免费观看| 久久一区二区三区免费| 久久精品国产亚洲AV蜜臀色欲| 久久精品人人做人人爽97| 91久久九九无码成人网站| 国产69精品久久久久APP下载| 日韩精品久久无码人妻中文字幕| 国产精品久久久久9999| 亚洲Av无码国产情品久久| 久久99精品久久久久久动态图 | 久久成人小视频| 97久久精品无码一区二区天美 | 亚洲欧美伊人久久综合一区二区|