c++使用curl ftp上传 无法连接到站点 错误CURLE_COULDNT_CONNECT

sdzangxin123 2012-12-19 11:49:55
代码

size_t read_data(void *buffer, size_t size, size_t nmemb, void *user_p)
{
return fread(buffer, size, nmemb, fp);

}
int main(int argc, char **argv)
{ // 初始化libcurl CURLcode code;
CURLcode code = curl_global_init(CURL_GLOBAL_WIN32);
if (code != CURLE_OK)
{
cerr << "init libcurl failed." << endl;
return -1;
}
fp = fopen("aaa.txt", "rb");
if (NULL == fp)
{

cout << "can't open file." << endl;
curl_global_cleanup();
return -1;
}
//fseek(fp, 0L, SEEK_END);
//size_t file_size = ::ftell(fp);
//fseek(fp, 0L, SEEK_SET);


// 获取文件大小
fseek(fp, 0, SEEK_END);
int file_size = ftell(fp);
rewind(fp);
// 获取easy handle
CURL *easy_handle = NULL;
easy_handle = curl_easy_init();
if (NULL == easy_handle)
{
cerr << "get a easy handle failed." << endl; fclose(fp);
curl_global_cleanup();
return -1;
}

// 设置eash handle属性
//curl_easy_setopt(easy_handle, CURLOPT_URL, "ftp://192.168.2.141/1002.mp3");
curl_easy_setopt(easy_handle, CURLOPT_URL, "ftp://174.129.25.97/aaa.txt");
curl_easy_setopt(easy_handle, CURLOPT_USERPWD, "admindev:**********");
curl_easy_setopt(easy_handle, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(easy_handle, CURLOPT_READFUNCTION, &read_data);
curl_easy_setopt(easy_handle, CURLOPT_FTP_CREATE_MISSING_DIRS, 0);
curl_easy_setopt(easy_handle, CURLOPT_READDATA, fp);
curl_easy_setopt(easy_handle, CURLOPT_INFILESIZE_LARGE, file_size);
// 执行上传操作

code = curl_easy_perform(easy_handle);
if (code == CURLE_OK)
{
cout << "upload successfully." << endl;
}

// 释放资源
fclose(fp);
curl_easy_cleanup(easy_handle);
curl_global_cleanup();
system("pause");
return 0;
}

如果用户密码错误 会返回 用户名 密码错误的错误信息,
但是用户名密码没错,却提示CURLE_COULDNT_CONNECT
...全文
481 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
xengine-qyt 2012-12-24
  • 打赏
  • 举报
回复
你上传文件CURL是怎么知道的?
pengyw 2012-12-19
  • 打赏
  • 举报
回复
三年没整curl了,很不错的东西

3,882

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧