viewpoint-particle

Author Topic: CLR - FileDownload errors  (Read 9472 times)

stanl

  • Pundit
  • *****
  • Posts: 782
CLR - FileDownload errors
« 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.
Code: [Select]
;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

  • Pundit
  • *****
  • Posts: 782
Re: CLR - FileDownload errors
« Reply #1 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.

stanl

  • Pundit
  • *****
  • Posts: 782
Re: CLR - FileDownload errors
« Reply #2 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.
Code: [Select]
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

  • Pundit
  • *****
  • Posts: 782
Re: CLR - FileDownload errors
« Reply #3 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()

stanl

  • Pundit
  • *****
  • Posts: 782
Re: CLR - FileDownload errors
« Reply #4 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.
Code: [Select]
$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

  • Pundit
  • *****
  • Posts: 782
Re: CLR - FileDownload errors
« Reply #5 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'??
Code: [Select]
;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

  • Tech Support
  • *****
  • Posts: 2566
    • WinBatch
Re: CLR - FileDownload errors
« Reply #6 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"
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates


stanl

  • Pundit
  • *****
  • Posts: 782
Re: CLR - FileDownload errors
« Reply #7 on: June 24, 2013, 08:53:16 am »
I did in my 'real' script, just didn't want to post actual user/pw.

Deana

  • Wilson WindowWare Tech Support
  • Pundit
  • *****
  • Posts: 1183
  • WinBatch® can do it.
    • WinBatch Tech Support Database
Re: CLR - FileDownload errors
« Reply #8 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
Deana F.
Technical Support
Wilson WindowWare Inc.

td

  • Tech Support
  • *****
  • Posts: 2566
    • WinBatch
Re: CLR - FileDownload errors
« Reply #9 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.
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates


stanl

  • Pundit
  • *****
  • Posts: 782
Re: CLR - FileDownload errors
« Reply #10 on: June 24, 2013, 10:11:14 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

  • Pundit
  • *****
  • Posts: 782
Re: CLR - FileDownload errors
« Reply #11 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.

Deana

  • Wilson WindowWare Tech Support
  • Pundit
  • *****
  • Posts: 1183
  • WinBatch® can do it.
    • WinBatch Tech Support Database
Re: CLR - FileDownload errors
« Reply #12 on: June 24, 2013, 11:17:19 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
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

  • Pundit
  • *****
  • Posts: 782
Re: CLR - FileDownload errors
« Reply #13 on: June 24, 2013, 12:17:41 pm »
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

  • Tech Support
  • *****
  • Posts: 2566
    • WinBatch
Re: CLR - FileDownload errors
« Reply #14 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
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates


stanl

  • Pundit
  • *****
  • Posts: 782
Re: CLR - FileDownload errors
« Reply #15 on: June 24, 2013, 05:17:48 pm »
Probably a stray neutrino interacting with some speck of matter in your computer's CPU

More likely a cache-22.