Highlighted
Absent Member.
Absent Member.
4654 views

FTP - send a file

Jump to solution

Hi,

Now I can log on to the FTP Server whats the COBOL code to send a small text file.

Thanks

Neil.

0 Likes
1 Solution

Accepted Solutions
Highlighted
Micro Focus Expert
Micro Focus Expert

The following works with .NET Framework 3.5 if you must use it:

     $set ilusing"System.Net"
     $set ilusing"System.IO"
      program-id. Program1 as "testftp.Program1".
      data division.
      working-storage section.
      01 cFtpWebRequest type System.Net.FtpWebRequest.
      01 cFtpWebResponse type System.Net.FtpWebResponse.
      01 cFileInfo type FileInfo.
      01 fileContents type Byte occurs any.
      01 cFileStream type FileStream.
      01 writer type Stream.
      procedure division.
          try
            set cftpWebRequest to type FtpWebRequest::Create(new type Uri("ftp.microfocus.com/testfile.txt")) as type FtpWebRequest
            set cFtpWebRequest::Method to type "System.Net.WebRequestMethods+Ftp"::"UploadFile"
            set cftpWebRequest::Proxy to null
            set cftpWebRequest::UseBinary to true
            set cftpWebRequest::Credentials to new NetworkCredential("user", "password")
            set cFileInfo to new FileInfo("c:\\temp\\testfile.txt") *>e.g.: c:\\Test.txt
            set size of fileContents to cFileInfo::Length
            set cFileStream to cFileInfo::OpenRead
            invoke cFileStream::Read(fileContents, 0, type Convert::ToInt32(cFileInfo::Length))
            set writer to cftpWebRequest::GetRequestStream
            invoke writer::Write(fileContents, 0, fileContents::Length)
            invoke writer::Close
            set cftpWebResponse to cFtpWebRequest::GetResponse as type FtpWebResponse
            display cftpWebResponse::StatusDescription
         catch ex as type WebException
            display ex::Message
         end-try
         goback.

View solution in original post

0 Likes
10 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

I haven't used the .NET Framework's FtpWebRequest class, which is what you're using, according to your previous question. But from the Microsoft documentation, it looks like the general procedure for any FTP operation using that class is:

  1. Open the FTP connection, specifying the name of the target file in the URL. In your previous note, you used "ftp://ftp.TestSite.com" as the URL. You'll want to use something like ftp://ftp.TestSite.com/somefile.txt.
  2. Set the Method property to indicate what operation you'd like to perform. In this case that's UploadFile.

    set cFtpWebRequest::Method to type WebRequestMethods::Ftp::UploadFile
  3. Get the RequestStream property. This will send a command to the FTP server and create the data connection. (The FTP protocol uses two network connections between the client and server, one for control and one for data.) It will return a network stream for the data connection.

    set requestStream to cFtpWebRequest::RequestStream
  4. Write the file contents to the request stream.

    invoke fileStream::CopyTo(requestStream)
  5. Close the request stream.

    invoke requestStream::Close
  6. Invoke the GetResponse method. This gets the response from the FTP server, which you can check to see if the operation succeeded.

    set webResponse to cFtpWebRequest::GetResponse

 

0 Likes
Highlighted
Absent Member.
Absent Member.

i have taken your code above and converted into cobol and it all converts (i think) apart from one line can you pls advise.

point 4 Write the file contents to the request stream.

invoke fileStream::CopyTo(requestStream)

      01  cFtpWebRequest    type "System.Net.FtpWebRequest".

      01  cStream   type       "System.IO.Stream".  

      01  cFileStream type "System.IO.FileStream".    

      01  cWebResponse  type "System.Net.WebResponse".

     *        set cFtpWebRequest::Method to type WebRequestMethods::Ftp::UploadFile

              set cFtpWebRequest::"Method" to type "System.Net.WebRequestMethods+Ftp"::"UploadFile"

     *        set requestStream to cFtpWebRequest::RequestStream

              set cStream to cFtpWebRequest::"GetRequestStream"()

     *        invoke fileStream::CopyTo(requestStream)

              invoke cFileStream::

     *        invoke requestStream::Close

              invoke cFileStream::"Close"()

     *        set webResponse to cFtpWebRequest::GetResponse

              set cWebResponse to cFtpWebRequest::"GetResponse" as cFtpWebResponse

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

It would use cStream as this is the return value from GetRequestStream method.

    set cStream to cFtpWebRequest::"GetRequestStream"()

    invoke cFileStream::CopyTo(cStream)

0 Likes
Highlighted
Absent Member.
Absent Member.

CopyTo

does not come up as an option (in visual cobol IDE) after i have type in

invoke cFileStream::

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

It does show CopyTo as an available method for me.

What does show in intellisense for available methods?

Are you using .Net Framework 4?

Try typing the statement anyway without intellisense and see if it shows a syntax error or not.

0 Likes
Highlighted
Absent Member.
Absent Member.

im using .net framework 3.5 which i guess is the problem.

but there must be ftp code that supports this version of .net, or is there a winapi way of send a small text file via ftp?

or some other way ..... its so close 😞

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

The following works with .NET Framework 3.5 if you must use it:

     $set ilusing"System.Net"
     $set ilusing"System.IO"
      program-id. Program1 as "testftp.Program1".
      data division.
      working-storage section.
      01 cFtpWebRequest type System.Net.FtpWebRequest.
      01 cFtpWebResponse type System.Net.FtpWebResponse.
      01 cFileInfo type FileInfo.
      01 fileContents type Byte occurs any.
      01 cFileStream type FileStream.
      01 writer type Stream.
      procedure division.
          try
            set cftpWebRequest to type FtpWebRequest::Create(new type Uri("ftp.microfocus.com/testfile.txt")) as type FtpWebRequest
            set cFtpWebRequest::Method to type "System.Net.WebRequestMethods+Ftp"::"UploadFile"
            set cftpWebRequest::Proxy to null
            set cftpWebRequest::UseBinary to true
            set cftpWebRequest::Credentials to new NetworkCredential("user", "password")
            set cFileInfo to new FileInfo("c:\\temp\\testfile.txt") *>e.g.: c:\\Test.txt
            set size of fileContents to cFileInfo::Length
            set cFileStream to cFileInfo::OpenRead
            invoke cFileStream::Read(fileContents, 0, type Convert::ToInt32(cFileInfo::Length))
            set writer to cftpWebRequest::GetRequestStream
            invoke writer::Write(fileContents, 0, fileContents::Length)
            invoke writer::Close
            set cftpWebResponse to cFtpWebRequest::GetResponse as type FtpWebResponse
            display cftpWebResponse::StatusDescription
         catch ex as type WebException
            display ex::Message
         end-try
         goback.

View solution in original post

0 Likes
Highlighted
Absent Member.
Absent Member.

Hi Chris,

Thanks for your help with this much appreciated, one slight problem, it fails on the following line (jumps to the catch):-

set writer to cftpWebRequest::GetRequestStream

With the following message:-

The requested URI is invalid for this FTP command

It also leaves the "writer" field set to NULL

Any idea whats wrong.

Thanks

Neil.

0 Likes
Highlighted
Absent Member.
Absent Member.

Hi Chris,

Sorry forget above I missed a line out ..... DOH.

Testing now.

Thanks

Neil.

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

I see that with assistance from Chris you have this working now. I just wanted to point out that my sample code was COBOL - I was using the syntax supported by the current Visual COBOL, including unquoted method names (though some of your changes might be necessary there too; I hadn't compiled the sample code). And, as noted elsewhere in the thread, the Stream::CopyTo method is available in .NET Framework 4. Mentioning what version of Visual COBOL and .NET Framework you're using when you post a question can help avoid some confusion.

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.