FTP Transfer with Dotnet

asmilie2basmilie2b Member Posts: 122
edited 2013-06-20 in NAV Three Tier
THis may be more of a webservice related issue than NAV, but if anyone had any ideas I would love to hear them. I am using the method explained here to upload a file by FTP:

viewtopic.php?f=32&t=51988

It gives the below error:

This message is for C/AL programmers: A call to System.Net.FtpWebRequest.GetRequestStream failed with this message: The requested FTP command is not supported when using HTTP proxy.

I have tried what other forums recommend which is using one of either of the below two commands, but these don't seem to be supported params (none / nothing)

request.Proxy := None;
request.Proxy := Nothing;

Bruce Anderson

Comments

  • asmilie2basmilie2b Member Posts: 122
    I have taken a different approach with this. I have used the tool "WebDrive" which maps an ftp location as a shared drive. So then all I need to do is save the file from NAV and Windows takes care of the file transfers. Actually working so well I might just stick with it, but I would like to resolve the FTP in dotnet sometime.

    Bruce Anderson
  • StLiStLi Member Posts: 83
    Hi!

    here is some more in depth insight on the Proxy attribute: http://msdn.microsoft.com/en-us/library ... xy(v=vs.90).aspx

    apparently your system has a proxy preset. If you want to ignore those settings you need to define a new variable to call GloblaProxySelection:
    defaultProxy	DotNet	'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.GlobalProxySelection
    
    which has a method called "GetEmptyWebProxy".

    This hopefully does the trick. I haven't tested it on my system.
  • asmilie2basmilie2b Member Posts: 122
    Hello StLi,

    Many thanks for the info! I wil give that a try.

    Bruce Anderson
  • asmilie2basmilie2b Member Posts: 122
    Hello StLi,

    I was taking a look into this. Unfortunately your link seems broken?

    If I add the dotnet, I can call:

    DefaultProxy.GetEmptyWebProxy,

    but I am not sure what to do with it.

    I tried adding this:
    FTPRequest.Proxy := DefaultProxy.GetEmptyWebProxy;

    Then I get this:
    This message is for C/AL programmers: A DotNet variable has not been instantiated. Attempting to call System.Net.FtpWebRequest.Proxy in CodeUnit FTP Transfer: Testdotnetftp

    I didn't know we had to instantiate a dotnet variable.. (new to dotnet in NAV suffice it to say). I tried CREATE(DefaultProxy), but that gives an error that dotnet variables cannot be used with create. ](*,)

    Bruce Anderson
  • deV.chdeV.ch Member Posts: 543
    .Net Variables need to be instantiated like you do in normal .net code, you need to call their constructor (of course this only applies to non static types).
    You do this like this:
    <VariableName> := <VariableName>.<ClassName>(<parameter1>,<parameter2>);

    If you hit F5 you will get a list of constructors in the Symbol Menu. You will find in this exmaple that only the default constructor with empty parameters is available.
    The call would look like this:
    DefaultProxy := DefaultProxy.GlobalProxySelection;
    


    btw here is the correct link: http://msdn.microsoft.com/de-ch/library/system.net.globalproxyselection.aspx
    But you can always type the class name in google and 99% of the times the msdn reference about it will come up as first result ;)
  • asmilie2basmilie2b Member Posts: 122
    Thank you!!
    With your info I was able to get it to work. Much appreciated.

    defaultProxy := DefaultProxy.GlobalProxySelection;
    URL := 'ftp://myserver.com/test/testfile.txt';
    
    FTPRequest := WebRequest.Create(URL);
    Credentials := Credentials.NetworkCredential('***', '***');
    FTPRequest.Proxy := defaultProxy.GetEmptyWebProxy;
    FTPRequest.Credentials := Credentials;
    FTPRequest.KeepAlive := FALSE;
    FTPRequest.UseBinary := TRUE;
    FTPRequest.Method := 'STOR';
    FTPRequest.UsePassive := TRUE;
    FileStream := FileA.OpenRead('c:\testfile.txt');
    
    FTPRequest.GetRequestStream.Write(FileA.ReadAllBytes('c:\testfile.txt'), 0, FileStream.Length);
    

    Bruce Anderson
  • asmilie2basmilie2b Member Posts: 122
    Now the file upload is working, if someone would be that much of a saviour and give me a hint on how I can get the file download working it would be much appreciated.

    After logging into the server with this:
    defaultProxy := DefaultProxy.GlobalProxySelection;
    URL := 'ftp://myserver.com/test/testfile.txt';

    FTPRequest := WebRequest.Create(URL);
    Credentials := Credentials.NetworkCredential('***', '***');
    FTPRequest.Proxy := defaultProxy.GetEmptyWebProxy;
    FTPRequest.Credentials := Credentials;
    FTPRequest.KeepAlive := FALSE;
    FTPRequest.UseBinary := TRUE;
    FTPRequest.Method := 'STOR';
    FTPRequest.UsePassive := TRUE;
    FileStream := FileA.OpenRead('c:\testfile.txt');

    I tried this then to retrieve from ftp (Changing FileA.OpenRead to FileA.OpenWrite - OK yes I am guessing here)

    ----
    FileStream := FileA.OpenWrite('c:\testfile.txt');
    FTPRequest.GetRequestStream.Write(FileA.ReadAllBytes('c:\testfile.txt'), 0, FileStream.Length);
    ----

    But it seems to trip over itself with the error:

    "A call to System.IO.File.ReadAllBytes failed with this message: The process cannot access the file 'c:\testfile.txt' because it is being used by another process."

    Bruce Anderson
Sign In or Register to comment.