简体   繁体   English

C ++使用WinINet上传到FTP服务器

[英]C++ Uploading to an FTP server with WinINet

I have been searching for 2 hours now, and I can't seem to figure out how to simply upload a file with an FTP server. 我已经搜索了2个小时,但似乎无法弄清楚如何使用FTP服务器简单地上传文件。

I'd prefer to use WinINet, since I'm new to C++ and Microsoft Visual Studio (I'm not new to programming, just to C++) 我更喜欢使用WinINet,因为我是C ++和Microsoft Visual Studio的新手(我对编程并不陌生,仅对C ++不熟悉)

All I really need is a working example of how to upload a file with an FTP server. 我真正需要的只是一个如何通过FTP服务器上传文件的有效示例。 I've been all over the web for the last 2 hours, and I can't find anything. 最近两个小时我一直在网上浏览,但找不到任何东西。

I've tried a lot of different libraries and stuff, but none seem to work, and a lot are outdated. 我尝试了很多不同的库和东西,但是似乎都没有用,而且很多已经过时了。

Anyways, if you'd share your knowledge, links, or experience about using FTP to upload a file with C++, I'd really appreciate it. 无论如何,如果您分享使用FTP通过C ++上传文件的知识,链接或经验,我将非常感激。

Thanks -Alex Benoit 谢谢-Alex Benoit

Figured it out. 弄清楚了。 Here's the code I'm using 这是我正在使用的代码

#include <wininet.h> 
#pragma comment(lib, "Wininet")
void FileSubmit()
    {
        HINTERNET hInternet;
        HINTERNET hFtpSession;
        hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
        if (hInternet == NULL)
        {
            cout << "Error: " << GetLastError();
        }
        else
        {
            hFtpSession = InternetConnect(hInternet, "server", INTERNET_DEFAULT_FTP_PORT, "user", "pass", INTERNET_SERVICE_FTP, 0, 0);
            if (hFtpSession == NULL)
            {
                cout << "Error: " << GetLastError();
            }
            else
            {
                if (!FtpPutFile(hFtpSession, "C://file.txt", "/file.txt", FTP_TRANSFER_TYPE_BINARY, 0))
                {
                    cout << "Error: " << GetLastError();
                }
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM