有关 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;
}
//获取
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( pCurl, CURLOPT_TIMEOUT, 3L ); //请求超时时长
curl_easy_setopt( pCurl, 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
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();
标签:setopt,libcurl,有关,例子,easy,curl,response,CURLOPT,size 来源: https://blog.csdn.net/wzxiaodu/article/details/122867705