Showing posts with label files in ax 2009. Show all posts
Showing posts with label files in ax 2009. Show all posts

Monday, 23 November 2015

FtpWebRequest

Upload File

   System.Object ftpo;
   System.Object ftpResponse;
   System.Net.FtpWebRequest request;
   System.IO.StreamReader reader;
   System.IO.Stream requestStream;
   System.Byte[] bytes;
   System.Net.NetworkCredential credential;
   System.String xmlContent;
   System.Text.Encoding utf8;
   System.Net.FtpWebResponse response;
   ;
   // Read file
   reader = new System.IO.StreamReader("C:/test.xml");
   utf8 = System.Text.Encoding::get_UTF8();
   bytes = utf8.GetBytes( reader.ReadToEnd() );
   reader.Close();
   // little workaround to get around the casting in .NET
   ftpo = System.Net.WebRequest::Create("ftp://ftp.company.com/dir/test.xml");
   request = ftpo;
  
   credential = new System.Net.NetworkCredential("user","password");
   request.set_Credentials(credential);
   request.set_ContentLength(bytes.get_Length());
   request.set_Method("STOR");
   // "Bypass" a HTTP Proxy (FTP transfer through a proxy causes an exception)
   // request.set_Proxy( System.Net.GlobalProxySelection::GetEmptyWebProxy() );
   requestStream = request.GetRequestStream();
   requestStream.Write(bytes,0,bytes.get_Length());
   requestStream.Close();
  
   ftpResponse = request.GetResponse();
   response = ftpResponse;
   info(response.get_StatusDescription());
 
 

Read from FTP

   System.Object ftpo;
   System.Net.FtpWebRequest request;
   System.IO.StreamReader reader;
   System.Net.NetworkCredential credential;
   System.Net.FtpWebResponse response;
   System.String text;
   ;
   ftpo = System.Net.WebRequest::Create("ftp://ftpserver.com/dir/myfile.txt");
   request = ftpo;
   credential = new System.Net.NetworkCredential("user","password");
   request.set_Credentials(credential);
   response = request.GetResponse();
   reader = new System.IO.StreamReader(response.GetResponseStream());
   text = reader.ReadToEnd();
   info(text);