CLR - FileDownload errors

Started by stanl, June 23, 2013, 08:31:39 AM

Previous topic - Next topic

stanl

Below is a sample outline of a simple procedure to download a file from an FTP site. You will have to set your own site/user/pw to test. Creating the CLR objects goes well but the actual FileDownload throws an error. I am able to do this in Powershell.

;Winbatch 2013 - CLR - simple FTP download errors out
;
;
;Stan Littlefield June 23, 2013
;NOTE: no error or version checking
;////////////////////////////////////////////////////////////////////////////////////////////////////////
           
cFile="C:\temp\myfile"  ;modify for your environment
If FileExist(cFile) Then FileDelete(cFile)
;using version 4.0
ObjectClrOption("use","System.Net, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
oWeb = ObjectClrNew('System.Net.WebClient')
oCred = ObjectClrNew('System.Net.NetworkCredential','ftpuser','ftppassword') ;modify
oURI =  ObjectClrNew('System.Uri',"ftp://ftp.mysite/myfile")
oWeb.Credentials = oCred
oWeb.DownloadFile(oURI,cFile)
oURI=0
oCred=0
oWeb=0
If FileExist(cFile) Then Message("Success","File Downloaded: ":cFile)
Exit

stanl

NOTE:  I deliberately decided against the System.Net.FtpWebRequest class, as I was only interested in a file download.  Will be playing around with Chilkat's free .NET ftp assembly in a bit.

stanl

I also looked into the CredentialCache class, but even the simplest code fails. The code below errors that there is no add method, but the attached jpeg indicates that MSDN thinks there is.

ObjectClrOption("use","System.Net, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
oCred = ObjectClrNew('System.Net.NetworkCredential','user','password')
oCache = ObjectClrNew('System.Net.CredentialCache')
oURI =  ObjectClrNew('System.Uri',"ftp://ftp.ftpsite.com/")
oCache.Add(oCred,"Basic",oURI)

stanl

ooooPPs....  my bad

should be

oCache.Add(oURI,"Basic",oCred)
and that appears to work.
[EDIT]
But the ftp script still fails with an exception for FileFownload()

stanl

In PS the easiest method is to use the URL post which unfortunately sends user/pw as clear text, which I wished to avoid, assuming credentials were secure.

$File = "path\file"
$u = "user"
$p = "pw"
$ftp = "ftp://" + $u + ":" + $p + "@ftp.mydomain.com/myfile"
"ftp url: $ftp"
$webclient = New-Object System.Net.WebClient
$uri = New-Object System.Uri($ftp)
"Downloading $File..."
$webclient.DownloadFile($uri, $File)

and before I get mucho flak for bringing in PS, I see it analogous to using VBA/VbScript to model WB COM scripts.

stanl

Here is my latest attempt (again change to real ftp site/file to test). Is it possible that the credential password has to be assigned as 'secure'??

;Winbatch 2013 - CLR - simple FTP download
;
;
;Stan Littlefield June 23, 2013
;NOTE: no error or version checking
;////////////////////////////////////////////////////////////////////////////////////////////////////////
           
cFile="path\file"
If FileExist(cFile) Then FileDelete(cFile)
;using version 4.0
ObjectClrOption("use","System.Net, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
oWeb = ObjectClrNew('System.Net.WebClient')
oCred = ObjectClrNew('System.Net.NetworkCredential','user','password')
oCache = ObjectClrNew('System.Net.CredentialCache')
oURI =  ObjectClrNew('System.Uri',"ftp://ftpuri")
oCache.Add(oURI,"Basic",oCred)
oWeb.Credentials = oCache
oWeb.DownloadFile("ftp://path/file",cFile)           
oURI=0
oCred=0
oWeb=0

If FileExist(cFile) Then Message("Success","File Downloaded: ":cFile)

Exit

td

You need to pass the full path plus file name as the constructor parameter to the URI object or drop the URI class completely and pass the full path as the first parameter to the Download method.

A full path would look something like "ftp://server.domain.com/subdir/subdir/subdir/test.txt"
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

I did in my 'real' script, just didn't want to post actual user/pw.

Deana

Deana F.
Technical Support
Wilson WindowWare Inc.

td

I have no problems getting the 'DownloadFile' method to work on an ftp site requiring credentials as long as the paths and credentials are correctly entered.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

Quote from: td on June 24, 2013, 09:45:06 AM
I have no problems getting the 'DownloadFile' method to work on an ftp site requiring credentials as long as the paths and credentials are correctly entered.

All I can say is I used the same uri, user, pw as in my PS script that worked. I'll adopt mine to the code Deana posted.

stanl

Deana and Tony... you probably won't believe this but.....
I downloaded Deana's post, substituted my creds and file/path. It worked. Her post and my original were about equal accept for outside placement and naming of some variables. With my original aligned with hers I tried it and it didn't work.  However, after substituting my ""  for '' it worked. Go figure.
Anyway, I feel more comfortable creating the basic classes and calling CLR methods.

Deana

Quote from: stanl on June 24, 2013, 10:30:06 AM
Deana and Tony... you probably won't believe this but.....
I downloaded Deana's post, substituted my creds and file/path. It worked. Her post and my original were about equal accept for outside placement and naming of some variables. With my original aligned with hers I tried it and it didn't work.  However, after substituting my ""  for '' it worked. Go figure.
Anyway, I feel more comfortable creating the basic classes and calling CLR methods.

I can't imagine how type of quotation marks would have any effect. Are you absolutely sure that is the only difference between the working and non working script?

Here is a simplified version of your code that uses double quotes. It was tested and works. Note it uses the FULL FTP SERVER FILE PATH in both the oURI and DownloadFile lines.:
Code (winbatch) Select
ObjectClrOption( "use", "System.Net, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" )
oWeb = ObjectClrNew( "System.Net.WebClient" )
oCred = ObjectClrNew( "System.Net.NetworkCredential", "itsme", "private" )
oCache = ObjectClrNew( "System.Net.CredentialCache")
oURI =  ObjectClrNew( "System.Uri", "ftp://sample.myserver.com/Cover.JPG" )
oCache.Add( oURI, "Basic", oCred )
oWeb.Credentials = oCache
oWeb.DownloadFile( "ftp://sample.myserver.com/Cover.JPG", "C:\temp\cover.JPG" )           
Exit
Deana F.
Technical Support
Wilson WindowWare Inc.

stanl

Quote from: Deana on June 24, 2013, 11:17:19 AM
I can't imagine how type of quotation marks would have any effect. Are you absolutely sure that is the only difference between the working and non working script?
As far as I know, unless there was some arcane subtlety I had whittled everything down to quotes. Perhaps there was some 'garbage' left over from my previous failures.  After a reboot, I'll change back to "" and re-run.

td

Probably a stray neutrino interacting with some speck of matter in your computer's CPU. Who knows? You may have just proven that neutrinos do have mass.  ;D
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

Quote from: td on June 24, 2013, 01:22:11 PM
Probably a stray neutrino interacting with some speck of matter in your computer's CPU

More likely a cache-22.