linux运行脚本提交put请求,linux使用libcurl实现put访问服务器

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

FILE *fp; //定义FILE类型指针

//重写把数据读入上传数据流函数

size_t read_file(void* buff, size_t size, size_t nmemb, void* userp)

{

size_t sizes = fread(buff, size, nmemb, (FILE *)userp);

return sizes;

}

int main(int argc,char **argv)

{

CURLcode res; //easy_handle定义的一些错误码

const char url[2096];

//初始化libcurl

if(argc!=3)

{

printf("please input url and filename");

exit(-1);

}

res = curl_global_init(CURL_GLOBAL_ALL);

if (res != CURLE_OK)

{

printf("init libcurl failed.\n" );

return -1;

}

//获取要上传的文件指针

FILE* r_file = fopen(argv[2], "rb");

if (0 == r_file)

{

printf( "the file %s isnot exit\n",argv[2]);

return -1;

}

CURL* easy_handle;

easy_handle = curl_easy_init();

if (0 == easy_handle)

{

printf( "get a easy_handle handle fail!");

fclose(r_file);

curl_global_cleanup();

curl_easy_cleanup(easy_handle);

return -1;

}

// 获取文件大小

fseek(r_file, 0, 2);

int file_size = ftell(r_file);

rewind(r_file);

curl_easy_setopt(easy_handle, CURLOPT_URL, argv[1]); //获取URL地址

curl_easy_setopt(easy_handle, CURLOPT_UPLOAD, 1); //告诉easy_handle是做上传操作

curl_easy_setopt(easy_handle, CURLOPT_READFUNCTION, &read_file); //调用重写的读文件流函数

curl_easy_setopt(easy_handle, CURLOPT_READDATA, r_file); //往read_file()函数中传入用户自定义的数据类型

curl_easy_setopt(easy_handle, CURLOPT_INFILE, r_file); //定位作为上传的输入文件

curl_easy_setopt(easy_handle, CURLOPT_VERBOSE, 1); //打印出具体http协议字段

//curl_easy_setopt(easy_handle, CURLOPT_HEADER, 1);

curl_easy_setopt(easy_handle, CURLOPT_INFILESIZE, file_size); //上传的字节数

//执行设置好的操作

res = curl_easy_perform(easy_handle);

//获取HTTP错误码

int HTTP_flag = 0;

curl_easy_getinfo(easy_handle, CURLINFO_RESPONSE_CODE , &HTTP_flag);

//一般清理动作

fclose(r_file);

curl_global_cleanup();

curl_easy_cleanup(easy_handle);

printf("url :%s localfile: %s\n",argv[1],argv[2]);

printf("HTTP_flag is %d\n",HTTP_flag);

// printf( "操作本地文件: file_name url:\n");

//检测HTTP错误码和执行操作结果

if (CURLE_OK != res)

{

printf("failure\n");

return -1;

}

else

{

printf("success\n");

return 0;

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值