c语言easy,【EasyDL】C语言请求示例代码

此文给出的为 C语言调用百度EasyDL的示例代码 图片转base64请自行处理

在用sprintf这个函数的时候 把json数据格式化输入的时候 要自己添加结束符

其他语言调用336001错误解决方案或示例代码 http://aixiaoshuai.mydoc.io/?t=236073

C语言示例代码:

来源于热心开发者

#include

#include

#include

#include

#include

#include

#include

//图片的base64数据 不需要urlencode处理

const char * base64char = "";

unsigned long get_file_size(const char * path)

{

unsigned long filesize =-1;

struct stat statbuff;

if(stat(path,&statbuff) < 0)

{

return filesize;

}

else

filesize = statbuff.st_size;

return filesize;

}

char * base64_encode( const unsigned char * bindata, char * base64, int binlength )

{

int i, j;

unsigned char current;

for ( i = 0, j = 0 ; i < binlength ; i += 3 )

{

current = (bindata[i] >> 2) ;

current &= (unsigned char)0x3F;

base64[j++] = base64char[(int)current];

current = ( (unsigned char)(bindata[i] << 4 ) ) & ( (unsigned char)0x30 ) ;

if ( i + 1 >= binlength )

{

base64[j++] = base64char[(int)current];

base64[j++] = '=';

base64[j++] = '=';

break;

}

current |= ( (unsigned char)(bindata[i+1] >> 4) ) & ( (unsigned char) 0x0F );

base64[j++] = base64char[(int)current];

current = ( (unsigned char)(bindata[i+1] << 2) ) & ( (unsigned char)0x3C ) ;

if ( i + 2 >= binlength )

{

base64[j++] = base64char[(int)current];

base64[j++] = '=';

break;

}

current |= ( (unsigned char)(bindata[i+2] >> 6) ) & ( (unsigned char) 0x03 );

base64[j++] = base64char[(int)current];

current = ( (unsigned char)bindata[i+2] ) & ( (unsigned char)0x3F ) ;

base64[j++] = base64char[(int)current];

}

base64[j] = '\0';

return base64;

}

int base64_decode( const char * base64, unsigned char * bindata )

{

int i, j;

unsigned char k;

unsigned char temp[4];

for ( i = 0, j = 0; base64[i] != '\0' ; i += 4 )

{

memset( temp, 0xFF, sizeof(temp) );

for ( k = 0 ; k < 64 ; k ++ )

{

if ( base64char[k] == base64[i] )

temp[0]= k;

}

for ( k = 0 ; k < 64 ; k ++ )

{

if ( base64char[k] == base64[i+1] )

temp[1]= k;

}

for ( k = 0 ; k < 64 ; k ++ )

{

if ( base64char[k] == base64[i+2] )

temp[2]= k;

}

for ( k = 0 ; k < 64 ; k ++ )

{

if ( base64char[k] == base64[i+3] )

temp[3]= k;

}

bindata[j++] = ((unsigned char)(((unsigned char)(temp[0] << 2))&0xFC)) |

((unsigned char)((unsigned char)(temp[1]>>4)&0x03));

if ( base64[i+2] == '=' )

break;

bindata[j++] = ((unsigned char)(((unsigned char)(temp[1] << 4))&0xF0)) |

((unsigned char)((unsigned char)(temp[2]>>2)&0x0F));

if ( base64[i+3] == '=' )

break;

bindata[j++] = ((unsigned char)(((unsigned char)(temp[2] << 6))&0xF0)) |

((unsigned char)(temp[3]&0x3F));

}

return j;

}

void encode(FILE * fp_in, FILE * fp_out)

{

unsigned char bindata[2050];

char base64[4096];

size_t bytes;

while ( !feof( fp_in ) )

{

bytes = fread( bindata, 1, 2049, fp_in );

base64_encode( bindata, base64, bytes );

fprintf( fp_out, "%s", base64 );

}

}

void decode(FILE * fp_in, FILE * fp_out)

{

int i;

unsigned char bindata[2050];

char base64[4096];

size_t bytes;

while ( !feof( fp_in ) )

{

for ( i = 0 ; i < 2048 ; i ++ )

{

base64[i] = fgetc(fp_in);

if ( base64[i] == EOF )

break;

else if ( base64[i] == '\n' || base64[i] == '\r' )

i --;

}

bytes = base64_decode( base64, bindata );

fwrite( bindata, bytes, 1, fp_out );

}

}

FILE *fp;

//这个函数是为了符合CURLOPT_WRITEFUNCTION而构造的

//完成数据保存功能

size_t WriteData(void *ptr, size_t size, size_t nmemb, void *stream)

{

int written = fwrite(ptr, size, nmemb, (FILE *)fp);

return written;

}

int postUrl()

{

CURL *curl;

CURLcode res;

char * imagedatapoint = NULL;

char szJsonData[65535] = {0};

sprintf(szJsonData,"{\"image\":\"%s\",\"top_num\":5}",imagedatapoint);//imagedatapoint 是base64编码好的图像数据编码函数上面有

char strCurlOpt_url[] ="https://aip.baidubce.com/rpc/2.0/ai_custom/v1/classification/ncpsb?access_token=24.6871ee410bf772d.......";

//等号后面换成你自己的access_token. 接口地址记得替换成自己的哦

if((fp=fopen("/usr/local/x86_curl/1.txt","w"))==NULL)

{

printf("fopen( /opt/1.txt) fail\n");

exit(1);

}

struct curl_slist * header = NULL;

header = curl_slist_append(header,"Content-Type: application/json;charset=UTF-8");

if(NULL == header)

{

printf("append is error\n");

exit(-1);

}

curl = curl_easy_init();

if (curl)

{

curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);

curl_easy_setopt(curl, CURLOPT_URL,strCurlOpt_url); // 指定url

curl_easy_setopt(curl, CURLOPT_HTTPPOST, 1L);

curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);

curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);

curl_easy_setopt(curl, CURLOPT_POSTFIELDS,szJsonData);

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteData);

res = curl_easy_perform(curl);

printf("%d %s\n",res, curl_easy_strerror(res));

curl_easy_cleanup(curl);

curl_slist_free_all(header);

}

fclose(fp);

return 1;

}

int main(void)

{

curl_global_init(CURL_GLOBAL_SSL);

postUrl();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值