通过ax2012将xml文件上传到ftp服务器

时间:2014-12-12 10:22:09

标签: ftp axapta microsoft-dynamics dynamics-ax-2012 ftpwebrequest

我正在寻找通过动态ax 2012连接到ftp并在ftp服务器上传文件xml,我已经做了测试工作,但是我无法连接到ftp我得到这个错误Impossible de créer l'objet 'CLRObject'

static void uploadTestFile(Args _args) {
   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("ftp://net.sites.com/user/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://net.sites.com/user/test.xml");
   request = ftpo;

   credential = new System.Net.NetworkCredential("login","pass");
   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());
} 

2 个答案:

答案 0 :(得分:0)

来自this blog

  

再一次axaptapedia.com可以节省一天的时间。这一次将我们指向Dynamics AX内.NET框架中的FtpWebRequest class。 Axaptapedia页面是完整的,除了一个薄薄的遗漏...异常处理。

将您的代码与博客上的代码进行比较我是否说它不适用于InteropPermission?此外,博客还提到在服务器上执行,这可能会避免其他潜在的错误,例如系统上没有安装ftp.exe ...你已经检查过机器上是否存在FTP客户端,对吗?

答案 1 :(得分:0)

问题在于StreamReader初始化:

reader = new System.IO.StreamReader("ftp://net.sites.com/user/test.xml");

如果源文件来自FTP,您可以按如下方式重写此部分:

System.IO.Stream responseStream;

ftpo = System.Net.WebRequest::Create("ftp://net.sites.com/user/testFrom.xml");
request = ftpo;

credential = new System.Net.NetworkCredential("login","pass");
request.set_Credentials(credential);
request.set_Method("RETR");

response = request.GetResponse();
responseStream = response.GetResponseStream();
reader = new System.IO.StreamReader(responseStream);