文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>关于Libcurl 的windoows使用

关于Libcurl 的windoows使用

时间:2011-04-01  来源:K.chaos

首先到libcurl的官网上直接找最新的安装包,我用的是VS2010可以直接编译,编译好之后,可以得到3个文件 都是DEBUG模式下的,不影响使用
libcurld.dll,libcurld_imp.lib,libcurld_imp.exp

新建空的控制台工程 代码如下 并且包含libcurl里的include,还有生成libcurld.dll的目录

#include <iostream>
#include <stdio.h>
#include <string>
#include <curl/curl.h>

using namespace std;

#pragma comment(lib, "libcurld_imp.lib")

string httpdata;
size_t process_data(char *buffer, size_t size, size_t nmemb, char *user_p)  {

        //string rststr((char *)buffer);
        string rst(buffer);
        
        httpdata=httpdata+rst;
        //printf("We received Content-Type kongchaos: %s\n", (char *)buffer );
        return 0;
}

int main(){

    cout<<"Hello libcurl"<<endl;
    CURL *curl;
    CURLcode res;
    struct curl_slist *headers=NULL;
    static const char *postthis="did=Tree&xmltype=xmlResources&action=load&sql=&tablename=other&dbname=CONFIGESB.DB&data=";

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "http://www.gbsoa.com/clientParamRequest.action?");
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postthis);

    /* if we don't provide POSTFIELDSIZE, libcurl will strlen() by
       itself */
    

    //headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
    //headers = curl_slist_append(headers,"Accept: text/html, */*");  
    //headers = curl_slist_append(headers,"ContentEncoding:UTF-8");  
    //headers = curl_slist_append(headers,"ContentLength:-1");  
    headers = curl_slist_append(headers,"Accept-Encoding:gunzip|gzip");  
    //headers = curl_slist_append(headers,"BasicAuthentication:False");  
    //headers = curl_slist_append(headers,"User-Agent: Mozilla/4.0 (compatible; MSIE7.0; Windows NT5.1; Mozilla/4.0 (compatible; MSIE 6.0; WindowsNT 5.1; SV1) ; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NETCLR 3.5.30729; .NET4.0C; .NET4.0E)");
    //curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(postthis));
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &process_data);
    std::string buffer;
    //curl_easy_setopt(curl, CURLOPT_READDATA, &buffer);     
    
    //curl_easy_setopt(curl, CURLOPT_WRITEDATA,  &buffer);

    res = curl_easy_perform(curl);

    /* always cleanup */
    curl_easy_cleanup(curl);
    curl_slist_free_all(headers); /* free the header list */
    cout<<httpdata<<endl;
    //cout<<buffer<<endl;
  }
    int i;
    cin>>i;
    return 0;
}

 其中

curl_easy_setopt就是curl的精华所在,可以使用很多功能,因为我只需要POST后得到返回值,只需要写  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &process_data);

其中process_data就是回调函数,如果没写,curl里会自动调用它本身的回调函数

编译成功后,就可以获得你想要的结果了  ,这个是程序运行的时候是需要libcurld.DLL的

如果想静态调用,请在预处理器那里加入 

CURL_STATICLIB

连接器那里加入 附加依赖项里加入 

libcurld_imp.lib
Ws2_32.lib
Wldap32.lib
还有把Libcurl里的head,souces全部复制进去(嘿嘿偷懒)

这样就可以了,如果这个DEMO在其他机器上运行不了,加入两个DLL

msvcp100d.dll 
msvcr100d.dll

 

 

排行榜 更多 +
泡龙大闯关安卓版

泡龙大闯关安卓版

冒险解谜 下载
割草派对安卓版

割草派对安卓版

飞行射击 下载
堡垒攻防战安卓版

堡垒攻防战安卓版

飞行射击 下载