Highlighted
Absent Member.
Absent Member.
369 views

Code Conversion

Hi,

Can any advise how to convert below to Visual Cobol?

using System;

using System.IO;

using System.Net;

using System.Net.Security;

using System.Security.Cryptography.X509Certificates;

 

namespace ConnectToFtpDemo

{

    class Program

    {

        static void Main(string[] args)

        {

            try

            {

                FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp:///testFileUpload.txt"); 

                request.Method = WebRequestMethods.Ftp.UploadFile; 

                request.Credentials = new NetworkCredential("Username", "Password");

                request.EnableSsl = true;

                ServicePointManager.ServerCertificateValidationCallback = ServicePointManager_ServerCertificateValidationCallback;

 

                long bytesSent = 0;

                using (Stream requestStream = request.GetRequestStream())

                using (FileStream uploadFileStream = File.OpenRead("C:\\Test\\test.txt"))

                {

                    uploadFileStream.CopyTo(requestStream);

                    bytesSent = uploadFileStream.Position;

                }

 

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex);

            }

 

            Console.ReadLine();

        }

 

 

        private static bool ServicePointManager_ServerCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)

        {

            //TODO: Logic to determine if certificate sent by server on connection is valid if it is return to true otherwise return false

            return true;

 

            //Type type = null;

 

            //var publicKey = certificate.GetPublicKeyString();

            //// Check public key if matche

            //type = certificate.GetType();

            //var p = type.GetProperty("Thumbprint");

            //var thumbPrint = p.GetValue(certificate)?.ToString();

           

 

        }

    }

}

0 Likes
2 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Code Conversion

I am not sure if it works or not but something like this should be close:

 

 

      $set ilusing"System""
      $set ilusing"System.IO""
      $set ilusing"System.Net""
      $set ilusing"System.Net.Security""
      $set ilusing"System.Security.Cryptography.X509Certificates""

       class-id ConnectToFtpDemo.Program.
       working-storage section.
       method-id Main static.
       local-storage section.
       procedure division using args as string occurs any.
		   try
             declare request as type FtpWebRequest = type WebRequest::Create("ftp:///testFileUpload.txt") as type FtpWebRequest 
             set request::Method = type WebRequestMethods+Ftp::UploadFile
             set request::Credentials = new type NetworkCredential("Username", "Password")
             set request::EnableSsl = true
             set type ServicePointManager::ServerCertificateValidationCallback to method self::ServicePointManager_ServerCertificateValidationCallback
             declare bytesSent as binary-long = 0
             perform using requestStream as type Stream = request::GetRequestStream
				 perform using uploadFileStream as type FileStream = type File::OpenRead("C:\\Test\\test.txt")
                    invoke uploadFileStream::CopyTo(requestStream)
                    set bytesSent = uploadFileStream::Position
                end-perform
             end-perform
           catch ex as type Exception 
               invoke type Console::WriteLine(ex)
           end-try
		    
           invoke type Console::ReadLine
           goback.
       end method.

       method-id ServicePointManager_ServerCertificateValidationCallback private static.
       local-storage section.
       procedure division using by value sender as object, certificate as type X509Certificate, #chain as type X509Chain, sslPolicyErrors as type SslPolicyErrors
                      returning retbool as condition-value.    
         *>TODO: Logic to determine if certificate sent by server on connection is valid if it is return to true otherwise return false
           set retbool to true
 
           *>declare #type as type Type = null
 
           *>declare publicKey = certificate::GetPublicKeyString
           *> Check public key if matche
           *> set #type = certificate::GetType
           *> declare p = #type::GetProperty("Thumbprint")
           *> declare thumbPrint = p::GetValue(certificate)::ToString
           goback.
       end method.
       end class.

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: Code Conversion

Thanks for the quick reply Chris, Ill give it a go
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.