Problem:
The .Net framework v2.0 introduced some new classes for ftp such as System.Net.FtpWebRequest. How can you use these classes from COBOL to perform an FTP request.
Resolution:
The exmaple project is attached.
Some sample code to perrform ftp in .Net is:-
$set sourceformat"variable"
***********************************************************
*
* Author - Micro Focus SupportLine
*
* Date - Aug 2006
*
* Demo of .Net Framework 2.0 class for FTP File Transfer
*
***********************************************************
Program-id. FTPSample as "FTPSample.FTPSample".
environment division.
configuration section.
repository.
class cFtpWebRequest as "System.Net.FtpWebRequest"
class cFtpWebResponse as "System.Net.FtpWebResponse"
class cWebRequestMethods as "System.Net.WebRequestMethods"
class cFtp as "System.Net.WebRequestMethods.Ftp"
class cStreamReader as "System.IO.StreamReader"
class cPath as "System.IO.Path"
class cStream as "System.IO.Stream"
class cByteArray as "System.Byte[]"
class cString as "System.String"
interface ICreadentials as "System.Net.ICredentials"
class cNetworkCredentials as "System.Net.NetworkCredential"
.
data division.
working-storage section.
78 wc-Buffer-Len value 4096.
01 myFtpRequest cFtpWebRequest.
01 myFtpResponse cFtpWebResponse.
01 myReader cStreamReader.
01 ws-filename pic x(50).
01 ws-access-mode pic x comp-x.
01 ws-deny-mode pic x comp-x.
01 ws-device pic x comp-x.
01 ws-file-handle pic x(4).
01 ws-file-offset pic x(8) comp-x.
01 ws-picx-buffer pic x(4096).
01 ws-buffer binary-char unsigned occurs wc-Buffer-Len. *> A Byte[]
01 ws-bytes binary-long.
01 ws-byte-count pic x(4) comp-x.
01 ws-flags pic x comp-x.
01 ws-stream cStream.
01 ws-creds ICreadentials.
01 ws-netcreds cNetworkCredentials.
procedure division.
***** Just display a directory list of the FTP Site top level.
set myFtprequest to cFtpWebRequest::"Create"("ftp://ftp.microfocus.com") as cFtpWebRequest
*> set myFtprequest::"Method" to cFtp::"ListDirectoryDetails"
set myFtprequest::"Method" to "LIST"
set myFtpResponse to myFtpRequest::"GetResponse" as cFtpWebResponse
set myReader to cStreamReader::"NEW"(myFTPResponse::"GetResponseStream"())
***** We should have a valid response here
***** Display Banner Details and the directory Listing
display myFtpResponse::"BannerMessage"
display myReader::"ReadToEnd"()
*****
***** Download a file from the Microfocus FTP Server
*****
display "Now we will attempt to download a file from the Micro Focus FTP Server....."
display "downloading now....."
set myFtprequest to cFtpWebRequest::"Create"("ftp://ftp.microfocus.com/samples/demos/webinformer/xmlparse.zip") as cFtpWebRequest
*> Deal with User Id
set ws-netcreds to myFtprequest::"Credentials" as cNetworkCredentials
display "Default UserName = " ws-netcreds::"UserName"
display "Default Password = " ws-netcreds::"Password"
*> Now show how to set the user/pass
set ws-netcreds::"UserName" to "anonymous"
set ws-netcreds::"Password" to "validemail@validdomain.com"
*> On with the download
set myFtpResponse to myFtpRequest::"GetResponse" as cFtpWebResponse
set ws-filename to cPath::"GetFileName"(myFtpRequest::"RequestUri"::"AbsolutePath")
perform read-ftp-stream
display "Download has finished"
goback.
****************************************************
* Lets get the file copied to the local machine.
****************************************************
read-ftp-stream section.
***** Use COBOL Byte Stream to save the binary data
***** Create the file in the current directory
call "CBL_DELETE_FILE" using ws-filename
move 2 to ws-access-mode
move 0 to ws-deny-mode ws-device
call "CBL_CREATE_FILE" using ws-filename
ws-access-mode
ws-deny-mode
ws-device
ws-file-handle
perform check-status
set ws-stream to myFTPResponse::"GetResponseStream"()
****** Set up a Buffer
set ws-bytes to ws-stream::"Read"(ws-buffer,0,wc-Buffer-Len)
move 0 to ws-file-offset
move 0 to ws-flags
move ws-bytes to ws-byte-count
perform until ws-bytes = 0
set ws-picx-buffer to ws-buffer
call "CBL_WRITE_FILE" using ws-file-handle
ws-file-offset
ws-byte-count
ws-flags
ws-picx-buffer
end-call
perform check-status
add ws-bytes to ws-file-offset
set ws-bytes to ws-stream::"Read"(ws-buffer,0,wc-Buffer-Len)
end-perform
call "CBL_CLOSE_FILE" using ws-file-handle
perform check-status
.
check-status section.
if return-code not = 0 then
display "CBL_ File error : " return-code
stop run
end-if
.
end program FTPSample.