Internet Connect在尝试连接到SFTP服务器时返回错误12002

InternetConnect returns error 12002 when attempting to connect to a sftp server

本文关键字:服务器 返回 错误 12002 SFTP Connect 连接 Internet      更新时间:2023-10-16

给定以下代码,该代码应连接到sftp服务器,并在该服务器中打印第一个文件的名称。看起来它返回了错误12002,这意味着请求时间已计时。我尝试使用2个不同的服务器运行它。我的代码中有根本上有问题吗?

#include "stdafx.h"
#include <Windows.h>
#include <Wininet.h>
#include <iostream>
using namespace std;
#pragma comment(lib, "wininet.lib")
int main()
{
    HINTERNET hInternet =  InternetOpen(NULL, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
    if (hInternet == NULL)
    {
        _tprintf(_T("An error has occured while trying to open the internet connectionn"));
        return 1;
    }
    HINTERNET hFTP = InternetConnect(
        hInternet,
        L"test.rebex.net", // this is a sftp server publicly available
        22,
        L"demo",
        L"password",
        INTERNET_SERVICE_FTP,
        INTERNET_FLAG_PASSIVE,
        0
    );
    if (hFTP == NULL)
    {
        cout << GetLastError() << endl;
        _tprintf(_T("Couldn't connect to the ftp servern"));
        return 1;
    }
    _tprintf(_T("%dn"), hFTP);
    WIN32_FIND_DATA fd;
    FtpFindFirstFile(hFTP, L".", &fd, INTERNET_FLAG_RELOAD, NULL);
    _tprintf(_T("%s"), fd.cFileName);
    InternetCloseHandle(hFTP);
    InternetCloseHandle(hInternet);
    return 0;
}

sftp(SSH上方的FTP)与FTP(ftp over ssl)非常不同,这可能是您想要的。

但是,无论哪种方式,Wininet根本不支持SFTP或FTPS(INTERNET_FLAG_SECURE标志仅支持HTTPS)。

您将必须使用另一个支持SFTP/FTP的FTP库。