0.初次使用curl

windows10 + vs2010使用curl(command URL)库
curl库的编译可以参考:http://blog.youkuaiyun.com/neverup_/article/details/21961017
openssl解压要使用管理员权限解压(不会请百度),不然会出现符号链接错误。
具体的使用可以看curl官网提供的API和example: https://curl.haxx.se/libcurl/c/example.html

把一个txt文件上传我的ftp

#include <iostream>
#include <string>
#include <curl/curl.h>
using namespace std;
#pragma comment(lib, "libcurld_imp.lib")
int main()
{
    CURL *curl;
    CURLcode res;
    FILE *fd;
    double speed_upload, total_time;
    fd = fopen("test.txt", "rb");
    if(!fd){
        printf("read failed!");
        return 1;
    }
    curl = curl_easy_init();
    if(curl){
        curl_easy_setopt(curl, CURLOPT_URL, "ftp://192.168.0.100/myfile.txt");

        curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);

        curl_easy_setopt(curl, CURLOPT_READDATA, fd);

        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

        res = curl_easy_perform(curl);

        if(res != CURLE_OK){
            fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
        }
        else {
            curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD, &speed_upload);
            curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total_time);
            fprintf(stderr, "Speed: %.3f bytes/sec during %.3f secondes\n", speed_upload, total_time);
        }
        curl_easy_cleanup(curl);
    }
    fclose(fd);
    return 0;
}

一个注意点就是
curl_easy_setopt(curl, CURLOPT_URL,”ftp://192.168.0.100/myfile.txt“);直接填写url会出现Uploading to a URL without a file name!
解决方案就是在后面加上文件在ftp上的名字
参考:http://stackoverflow.com/questions/16429849/libcurl-code-not-working-says-im-uploading-to-the-server-w-out-a-file-name

成功上传这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值