有关 libcurl 例子


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

#include <string>
using std::string;

struct memory
{
    char *response;
    size_t size;

//    memory()
//    {
//        response = ( char * )calloc( 1, 1 );
//        size = 0;
//    }
//
//    ~memory()
//    {
//        free( response );
//        response = NULL;
//        size = 0;
//    }
};

size_t cb ( void *data, size_t size, size_t nmemb, void *userp )
{
    size_t realsize = size * nmemb;
    struct memory *mem = ( struct memory * ) userp;

    char *ptr = ( char* ) realloc ( mem->response, mem->size + realsize + 1 );
    if ( ptr == NULL )
        return 0;  /* out of memory! */

    mem->response = ptr;
    memcpy ( & ( mem->response[mem->size] ), data, realsize );
    mem->size += realsize;
    mem->response[mem->size] = 0;

    return realsize;
}

struct memory chunk = {0};
curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, cb );
curl_easy_setopt ( curl, CURLOPT_WRITEDATA, ( void * ) &chunk );
free ( chunk.response );



//获取
size_t writefile_callback ( char *buffer, size_t size, size_t nmemb, void *userdata )
{
    return fwrite ( buffer, size, nmemb, ( FILE * ) userdata );
}

size_t writemem_callback ( char *buffer, size_t size, size_t nmemb, string &userdata )
{
    size_t rsize = size * nmemb;
    userdata.append ( buffer, rsize );
    return rsize;
}

size_t readfile_callback ( char *buffer, size_t size, size_t nmemb, void *userdata )
{
    return fread ( buffer, size, nmemb, ( FILE * ) userdata );
}




CURL *curl;
CURLcode res;
curl_global_init ( CURL_GLOBAL_ALL );
curl = curl_easy_init();

if ( curl )
{

    curl_easy_setopt ( curl, CURLOPT_URL, "https://example.com" );


// https://
    curl_easy_setopt ( curl, CURLOPT_SSL_VERIFYHOST, 0L ); // set peer and host verify false
    curl_easy_setopt ( curl, CURLOPT_SSL_VERIFYPEER, 0L ); // if want to use https



    curl_easy_setopt ( curl, CURLOPT_COOKIEFILE, "./cookies.txt" ); /* read cookie */
    curl_easy_setopt ( curl, CURLOPT_COOKIEJAR, "./cookies.txt" ); /* write cookie */

    curl_easy_setopt ( curl, CURLOPT_COOKIE, "tool=curl; fun=yes;" );

    curl_easy_setopt ( curl, CURLOPT_PROXY, "127.0.0.1:8888" ); //代理


    curl_easy_setopt ( curl, CURLOPT_TIMEOUT, 3L ); //请求超时时长
    curl_easy_setopt ( curl, CURLOPT_CONNECTTIMEOUT, 10L ); //连接超时时长


    curl_easy_setopt ( curl, CURLOPT_HEADER, 1L ); // 头部和主体一起获取
    curl_easy_setopt ( curl, CURLOPT_NOBODY, 1L );

//             disable progress meter, set to 0L to enable it
    curl_easy_setopt ( curl, CURLOPT_NOPROGRESS, 0L );
//             Switch on full protocol/debug output
    curl_easy_setopt ( curl, CURLOPT_VERBOSE, 1L );
//             example.com is redirected, so we tell libcurl to follow redirection
    curl_easy_setopt ( curl, CURLOPT_FOLLOWLOCATION, 0L );

    struct memory chunk = {0};
    curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, cb );
    curl_easy_setopt ( curl, CURLOPT_WRITEDATA, ( void * ) &chunk );
    free ( chunk.response );


    string rdata;
    curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, writemem_callback );
    curl_easy_setopt ( curl, CURLOPT_WRITEDATA, &rdata );


    FILE* fp = fopen ( filename, "wb" );
    curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, writefile_callback() );
    curl_easy_setopt ( curl, CURLOPT_WRITEDATA, fp );



    FILE* fp = fopen ( filename, "rb" );
    fseek ( fp, 0, SEEK_END );
    long len = ftell ( fp );
    fseek ( fp, 0, SEEK_SET );
    // fclose( fp );

    curl_easy_setopt ( curl, CURLOPT_READFUNCTION, readfile_callback );
    curl_easy_setopt ( curl, CURLOPT_READDATA, fp );
    curl_easy_setopt ( curl, CURLOPT_INFILESIZE, len );

    curl_easy_setopt ( curl, CURLOPT_UPLOAD, 1L );

    curl_easy_setopt ( curl, CURLOPT_POST, 1L );
    curl_easy_setopt ( curl, CURLOPT_POSTFIELDS, postdata );
    curl_easy_setopt ( curl, CURLOPT_POSTFIELDSIZE, postdatalen );

    struct curl_slist *list = NULL;
    list = curl_slist_append ( list, "origin: https://example.com" );
    list = curl_slist_append ( list, "Content-Type: multipart/form-data" );
    list = curl_slist_append ( list, "Expect:" );
    curl_easy_setopt ( curl, CURLOPT_HTTPHEADER, list );

    fclose ( fp );


#ifndef DISABLE_SSH_AGENT
    curl_easy_setopt ( curl, CURLOPT_URL, "sftp://user:pass@example.com/path/filename" );
//   curl_easy_setopt( curl, CURLOPT_URL, "sftp://example.com/path/filename" );
//   curl_easy_setopt( curl, CURLOPT_USERPWD, "USER:PWD" );

    /* We activate ssh agent. For this to work you need
       to have ssh-agent running (type set | grep SSH_AGENT to check) or
       pageant on Windows (there is an icon in systray if so) */
    // curl_easy_setopt(curl, CURLOPT_SSH_AUTH_TYPES, CURLSSH_AUTH_AGENT);
    curl_easy_setopt ( curl, CURLOPT_SSH_AUTH_TYPES, CURLSSH_AUTH_PASSWORD );
    //      curl_easy_setopt ( curl, CURLOPT_SSH_AUTH_TYPES, CURLSSH_AUTH_DEFAULT );
#endif

    res = curl_easy_perform (curl);
    if ( res == CURLE_OK )
    {
        long response_code = 0;
        curl_easy_getinfo ( curl, CURLINFO_RESPONSE_CODE, &response_code );
        if ( response_code == 200 )
        {

        }
    }
    else
        fprintf ( stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror ( res ) );

    curl_slist_free_all ( list );
    curl_easy_cleanup ( curl );

}

curl_global_cleanup();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值