locked
FtpGetFile Error 6 RRS feed

  • Question

  • I'm trying to download a file off a website using the windows API and have been unable to do so, getting an "Error 6" Any ideas what I'm doing wrong?

    userver="http://www.actualurl.com";
    remotefile="http://www.actualurl.com/subdirectoryname/filename.dat";
    localfile="\\localsubdirectory\\filename.dat";

    The connection to the internet returns fine with no errors:

    dwflags=0;  //INTERNET_FLAG_ASYNC
    hiopen=InternetOpen(wagent.c_str(),INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,dwflags);

    if(hiopen=NULL)
    {
         stxt="Could not open an Internet connection";
         showtextmessage(stxt);
         return 0;
    }

    Then the connection to the windows web interface returns fine with no errors:

    wserver=strtowstr(userver);
    dwflags=INTERNET_FLAG_RELOAD;
    hiconnect=InternetConnect(hiopen,wserver.c_str(),INTERNET_DEFAULT_FTP_PORT,NULL,NULL,           INTERNET_SERVICE_FTP,dwflags,NULL);

    if(hiconnect=NULL)
    {
           e=GetLastError();
           stxt="Data Download Connection Error "+trim(str(e))+" !";
           showtextmessage(stxt);

           InternetCloseHandle(hiopen);
           return 0;
    }

    But then trying to actually download the file, I get an error 6:

    wlocalfile=strtowstr(localfile);
    wremotefile=strtowstr(remotefile);
    dwflagsandattr=FILE_ATTRIBUTE_NORMAL;
    dwflags=FTP_TRANSFER_TYPE_BINARY;

    retval=FtpGetFile(hiconnect,wremotefile.c_str(),wlocalfile.c_str(),TRUE,dwflagsandattr,dwflags,0);
    InternetCloseHandle(hiconnect);
    InternetCloseHandle(hiopen);

    if(retval == 0)
            showtextmessage("Error "+str(GetLastError()));

    Thursday, August 27, 2015 5:11 PM

Answers

  • On 8/27/2015 1:11 PM, TallGuy63 wrote:

    userver="http://www.actualurl.com";

    The server name should not include the protocol: make it "www.actualurl.com"

    Also - is your server an FTP server or an HTTP server? Your URLs make it sound like the latter, but of course FtpGetFile would only work with the former.

    remotefile="http://www.actualurl.com/subdirectoryname/filename.dat";

    Similarly, the file name you pass to FtpGetFile shouldn't include the protocol or the host. Make it "/subdirectoryname/filename.dat"

    if (hiopen=NULL)
    if (hiconnect=NULL)

    These are assignments, not comparisons. Even if the handle was valid before this line, you discard it and reset it to NULL. The condition is also never true, so you don't even know if any of your calls fail.


    Igor Tandetnik
    • Marked as answer by TallGuy63 Thursday, August 27, 2015 7:06 PM
    Thursday, August 27, 2015 6:42 PM
  • On 8/27/2015 6:09 PM, TallGuy63 wrote:

    Error    2    error LNK2019: unresolved external symbol URLDownloadToFileW

    Had you chosen to read the documentation, you would have discovered that you should link with Urlmon.lib


    Igor Tandetnik
    • Marked as answer by TallGuy63 Thursday, August 27, 2015 10:15 PM
    Thursday, August 27, 2015 10:14 PM

All replies

  • Your code is causing the error.  Instead of checking the handle it is setting it to NULL.  You typed an "=" instead of "=="
    if(hiconnect=NULL)
    {
           e=GetLastError();
           stxt="Data Download Connection Error "+trim(str(e))+" !";
           showtextmessage(stxt);
    
           InternetCloseHandle(hiopen);
           return 0;
    }
    That's why you receive an invalid handle error code


    • Edited by RLWA32 Thursday, August 27, 2015 6:42 PM
    Thursday, August 27, 2015 6:40 PM
  • On 8/27/2015 1:11 PM, TallGuy63 wrote:

    userver="http://www.actualurl.com";

    The server name should not include the protocol: make it "www.actualurl.com"

    Also - is your server an FTP server or an HTTP server? Your URLs make it sound like the latter, but of course FtpGetFile would only work with the former.

    remotefile="http://www.actualurl.com/subdirectoryname/filename.dat";

    Similarly, the file name you pass to FtpGetFile shouldn't include the protocol or the host. Make it "/subdirectoryname/filename.dat"

    if (hiopen=NULL)
    if (hiconnect=NULL)

    These are assignments, not comparisons. Even if the handle was valid before this line, you discard it and reset it to NULL. The condition is also never true, so you don't even know if any of your calls fail.


    Igor Tandetnik
    • Marked as answer by TallGuy63 Thursday, August 27, 2015 7:06 PM
    Thursday, August 27, 2015 6:42 PM
  • Thanks for the feedback!

    I'm switching to Winhttp instead...

    Only problem is I get 6 errors in winhttp.h when I #include <Winhttp.h>

    Is there an updated version or something?  I'm running Visual Studio 2013

    Thursday, August 27, 2015 7:06 PM
  • On 8/27/2015 3:06 PM, TallGuy63 wrote:

    I'm switching to Winhttp instead...

    Again, what kind of server are you working with, HTTP or FTP? WinHTTP only supports the former, as should be clear from the name.


    Igor Tandetnik
    Thursday, August 27, 2015 7:12 PM
  • I'm using HTTP....

    But regardless, just #include ing the winhttp.h should not cause errors in winhttp.h ??

    Thursday, August 27, 2015 7:15 PM
  • On 8/27/2015 3:15 PM, TallGuy63 wrote:

    But regardless, just #include ing the winhttp.h should not cause errors in winhttp.h ??

    What errors are you seeing?


    Igor Tandetnik
    Thursday, August 27, 2015 7:17 PM
  • Warning    1    warning C4005: 'BOOLAPI' : macro redefinition    C:\Program Files (x86)\Windows Kits\8.1\Include\um\Winhttp.h    49
    Warning    2    warning C4005: 'SECURITY_FLAG_IGNORE_CERT_DATE_INVALID' : macro redefinition    C:\Program Files (x86)\Windows Kits\8.1\Include\um\Winhttp.h    82
    Warning    3    warning C4005: 'SECURITY_FLAG_IGNORE_CERT_CN_INVALID' : macro redefinition    C:\Program Files (x86)\Windows Kits\8.1\Include\um\Winhttp.h    83
    Error    4    error C2371: 'HTTP_VERSION_INFO' : redefinition; different basic types    C:\Program Files (x86)\Windows Kits\8.1\Include\um\Winhttp.h    114
    Error    5    error C2371: 'LPHTTP_VERSION_INFO' : redefinition; different basic types    C:\Program Files (x86)\Windows Kits\8.1\Include\um\Winhttp.h    114
    Error    6    error C2371: 'INTERNET_SCHEME' : redefinition; different basic types    C:\Program Files (x86)\Windows Kits\8.1\Include\um\Winhttp.h    121
    Error    7    error C2371: 'LPINTERNET_SCHEME' : redefinition; different basic types    C:\Program Files (x86)\Windows Kits\8.1\Include\um\Winhttp.h    121
    Error    8    error C2371: 'URL_COMPONENTS' : redefinition; different basic types    C:\Program Files (x86)\Windows Kits\8.1\Include\um\Winhttp.h    166
    Error    9    error C2371: 'LPURL_COMPONENTS' : redefinition; different basic types    C:\Program Files (x86)\Windows Kits\8.1\Include\um\Winhttp.h    166
    Thursday, August 27, 2015 7:18 PM
  • On 8/27/2015 3:18 PM, TallGuy63 wrote:

    Warning    1    warning C4005: 'BOOLAPI' : macro redefinition    C:\Program Files (x86)\Windows Kits\8.1\Include\um\Winhttp.h    49

    It doesn't look like you can include both wininet.h and winhttp.h into the same source file. They define a lot of the same names.


    Igor Tandetnik
    Thursday, August 27, 2015 7:31 PM
  • Since I have to have wininet in this app, Do you think it's safe to delete the duplicate definitions in the winhttp.ini ?

    Thursday, August 27, 2015 7:34 PM
  • On 8/27/2015 3:34 PM, TallGuy63 wrote:

    Since I have to have wininet in this app, Do you think it's safe to delete the duplicate definitions in the winhttp.ini ?

    No, I don't think so.

    You can use both in the same application (though it's unclear why you would want to) - just not in the same source file. Split the part that works with WinHTTP into a separate source file.


    Igor Tandetnik
    Thursday, August 27, 2015 7:42 PM
  • The reason is that I need to upload files to the http site as well, so that requires ftp...
    Thursday, August 27, 2015 7:58 PM
  • On 8/27/2015 3:58 PM, TallGuy63 wrote:

    The reason is that I need to upload files to the http site as well, so that requires ftp...

    Why can't you use WinInet both for HTTP and FTP?


    Igor Tandetnik
    Thursday, August 27, 2015 8:42 PM
  • That's what I was originally doing, but you said the FtpGetFile should not be used for http, so I switched to winhttp....
    Thursday, August 27, 2015 9:00 PM
  • On 8/27/2015 5:00 PM, TallGuy63 wrote:

    That's what I was originally doing, but you said the FtpGetFile should not be used for http, so I switched to winhttp....

    And that's supposed to help how?

    If all you need is to download data from a URL to a file on disk, there's URLDownloadToFile


    Igor Tandetnik
    Thursday, August 27, 2015 9:04 PM
  • OK I abandoned winhttp and am just trying to use URLDownloadToFile.....

           hres=URLDownloadToFile((LPUNKNOWN)NULL,(LPCTSTR)wremotefile.c_str(),(LPCTSTR)wlocalfile.c_str(),0,(LPBINDSTATUSCALLBACK)NULL);

           if(hres == S_OK)
             return TRUE;

    Now I get this error when I compile:

    Error    2    error LNK2019: unresolved external symbol URLDownloadToFileW referenced in function "int __cdecl downloadfile(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,long)" (?downloadfile@@YAHV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@00J@Z)    D:\Wendes\WenPipe\WenPipe\WenPipe.obj

    Thursday, August 27, 2015 10:03 PM
  • Hmmmm

    Changed it to:

    hres=URLDownloadToFile(NULL,wremotefile.c_str(),wlocalfile.c_str(),0,NULL);

    and it still gives error 2:

    Error    2    error LNK2019: unresolved external symbol URLDownloadToFileW referenced in function "int __cdecl downloadfile(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,long)" (?downloadfile@@YAHV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@00J@Z)    D:\Wendes\WenPipe\WenPipe\WenPipe.obj


    • Edited by TallGuy63 Thursday, August 27, 2015 10:12 PM
    Thursday, August 27, 2015 10:09 PM
  • On 8/27/2015 6:09 PM, TallGuy63 wrote:

    Error    2    error LNK2019: unresolved external symbol URLDownloadToFileW

    Had you chosen to read the documentation, you would have discovered that you should link with Urlmon.lib


    Igor Tandetnik
    • Marked as answer by TallGuy63 Thursday, August 27, 2015 10:15 PM
    Thursday, August 27, 2015 10:14 PM
  • Added

    #pragma comment(lib,"Urlmon.lib")

    That got rid of the problem

    Thursday, August 27, 2015 10:15 PM
  • Did you include urlmon.lib in the linker input?
    Thursday, August 27, 2015 10:15 PM