WinBatch® Technical Support Forum

Archived Boards => COM Automation and dotNet => Topic started by: stanl on June 23, 2013, 08:31:39 AM

Title: CLR - FileDownload errors
Post by: stanl on June 23, 2013, 08:31:39 AM
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
Title: Re: CLR - FileDownload errors
Post by: stanl on June 23, 2013, 09:43:22 AM
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.
Title: Re: CLR - FileDownload errors
Post by: stanl on June 23, 2013, 11:10:13 AM
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)
Title: Re: CLR - FileDownload errors
Post by: stanl on June 23, 2013, 12:16:44 PM
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()
Title: Re: CLR - FileDownload errors
Post by: stanl on June 23, 2013, 12:25:38 PM
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.
Title: Re: CLR - FileDownload errors
Post by: stanl on June 24, 2013, 05:40:28 AM
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
Title: Re: CLR - FileDownload errors
Post by: td on June 24, 2013, 07:04:55 AM
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"
Title: Re: CLR - FileDownload errors
Post by: stanl on June 24, 2013, 08:53:16 AM
I did in my 'real' script, just didn't want to post actual user/pw.
Title: Re: CLR - FileDownload errors
Post by: Deana on June 24, 2013, 09:12:27 AM
Posted a FileDownload code sample in the tech database (based off your code) that seems to work: http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/tsleft.web+WinBatch/dotNet/System_Net+DownloadFile.txt
Title: Re: CLR - FileDownload errors
Post by: 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.
Title: Re: CLR - FileDownload errors
Post by: stanl on June 24, 2013, 10:11:14 AM
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.
Title: Re: CLR - FileDownload errors
Post by: 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.
Title: Re: CLR - FileDownload errors
Post by: Deana on June 24, 2013, 11:17:19 AM
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
Title: Re: CLR - FileDownload errors
Post by: stanl on June 24, 2013, 12:17:41 PM
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.
Title: Re: CLR - FileDownload errors
Post by: td on June 24, 2013, 01:22:11 PM
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
Title: Re: CLR - FileDownload errors
Post by: stanl on June 24, 2013, 05:17:48 PM
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.