c++使用curl ftp上传 无法连接到站点 错误CURLE_COULDNT_CONNECT
代码
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