2012-04-18 85 views
2

我已經使用ZebraDesigner for xml ver2.0爲Zebra 170PAX4打印機創建了一些標籤模板。我已經成功地將zpl導出到Zebra,並且我可以在Windows命令提示符中使用ftp編輯和發送xml文件到Zebra,例如:c:\ftp 10.161.41.212 Connected to 10.161.41.212. 220 ZBR-46688 Version 1.01.7 ready. User (10.161.41.212:(none)): 230 User logged in. ftp> put c:SPver1_1.xml 200 PORT command successful. 150 Opening ASCII mode data connection for SPver1_1.xml. 226 Transfer complete. ftp: 324 bytes sent in 0.00Seconds 324000.00Kbytes/sec錯誤嘗試使用C#ftpwebrequest到ftp xml文件到Zebra打印機

現在,我正在嘗試使用MS VS2010和C#從WinForms應用程序自動執行此操作。但由於某些原因沒有什麼,我似乎使用的FtpWebRequest類的嘗試,我還收到一個錯誤

The remote server returned an error: (502) Command not implemented.

據我瞭解,ftp方法UploadFile相同STOR這是相同的別名放。我使用的代碼如下所示:

public bool PutXmlFile(CZebraPrinter printer, CDeviceContainer devices) 
    { 
     bool OK = true; 
     CDevice currDevice = new CDevice(); 
     currDevice = devices.GetDevice(this.m_devName); 
     string ftpHost = "ftp://" + printer.m_IPAddress + "/"; 
     string ftpPath = "E:"; 
     Uri printerUri = new Uri(ftpHost + ftpPath); 

     try 
     { 
      FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(printerUri); 
      ftpRequest.Method = WebRequestMethods.Ftp.UploadFile; 
      ftpRequest.KeepAlive = false; 
      ftpRequest.UseBinary = false; 
      ftpRequest.UsePassive = false; 
      ftpRequest.Timeout = 5000; //milliseconds 

      // The Zebra printer uses anonymous login 
      ftpRequest.Credentials = new NetworkCredential("anonymous", ""); 

      // Copy the contents of the file to the request stream. 
      StreamReader sourceStream = new StreamReader(currDevice.m_defaultDataFile); 
      byte[] fileContents = Encoding.ASCII.GetBytes(sourceStream.ReadToEnd()); 
      sourceStream.Close(); 

      Stream requestStream = ftpRequest.GetRequestStream(); 
      requestStream.Write(fileContents, 0, fileContents.Length); 
      requestStream.Close(); 

      FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse(); 
      //Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription); 
      //Check response 
      response.Close(); 
     } 
     catch (Exception ex) 
     { 
      string errMessage = string.Format("An error occured trying to ftp the device xml file to the Zebra printer. \n\nReason:\n{0}", ex.Message); 
      MessageBox.Show(errMessage, m_mfgInfoErr, MessageBoxButtons.OK, MessageBoxIcon.Error); 
      return !OK; 
     } 
     return OK; 
    } 

有沒有人看到我可能做錯了什麼。任何想法,將不勝感激。 謝謝。

我跑的痕跡,這裏是輸出:

System.Net Information: 0 : [4780] FtpWebRequest#14993092::.ctor(ftp://10.161.41.212/E:) 
System.Net Verbose: 0 : [4780] Exiting WebRequest::Create()  -> FtpWebRequest#14993092 
System.Net Verbose: 0 : [4780] FtpWebRequest#14993092::GetRequestStream() 
System.Net Information: 0 : [4780] FtpWebRequest#14993092::GetRequestStream(Method=STOR.) 
System.Net Information: 0 : [4780] Associating FtpWebRequest#14993092 with FtpControlStream#720107 
System.Net Information: 0 : [4780] FtpControlStream#720107 - Received response [220 ZBR-46688 Version 1.01.7 ready.] 
System.Net Information: 0 : [4780] FtpControlStream#720107 - Sending command [USER anonymous] 
System.Net Information: 0 : [4780] FtpControlStream#720107 - Received response [230 User logged in.] 
System.Net Information: 0 : [4780] FtpControlStream#720107 - Sending command [OPTS utf8 on] 
System.Net Information: 0 : [4780] FtpControlStream#720107 - Received response [502 Command 'OPTS' not implemented.] 
System.Net Information: 0 : [4780] FtpControlStream#720107 - Sending command [PWD] 
System.Net Information: 0 : [4780] FtpControlStream#720107 - Received response [502 Command 'PWD' not implemented.] 
System.Net Information: 0 : [4780] FtpWebRequest#14993092::(Releasing FTP connection#720107.) 
System.Net Error: 0 : [4780] Exception in the FtpWebRequest#14993092::GetRequestStream - The remote server returned an error: (502) Command not implemented. 

所以看起來OPTSPWD都是問題。有沒有辦法控制FtpWebRequest發送的內容,還是我需要第三方庫?

回答

1

UploadFile使用STOR命令,但在發送此命令之前,.NET有時會發送其他命令,例如「OPTS utf8 on」或「TYPE I」等。如果您爲您的應用程序創建了以下內容的配置文件:

<?xml version="1.0"?> 
<configuration> 
    <system.diagnostics> 
    <sharedListeners> 
     <add name="console" type="System.Diagnostics.TextWriterTraceListener" initializeData="c:\logfile.txt" /> 
    </sharedListeners> 
    <sources> 
     <source name="System.Net" switchName="sourceSwitch"> 
     <listeners> 
      <add name="console" /> 
     </listeners> 
     </source> 
    </sources> 
    <switches> 
     <add name="sourceSwitch" value="Verbose"/> 
    </switches> 
    </system.diagnostics> 
</configuration> 

所有的命令和響應都會記錄在文件中,所以你可以看看,哪一個會導致問題。

UPDATE: 所以問題是,發出STOR命令之前,FTP客戶端發送OPTS和PWD命令,您的打印機不明白,可能是因爲它是非常輕量級的FTP服務器。不幸的是,我不知道如何阻止這些命令的發送。我在我的腦海裏只有兩種解決方法:

  • 找到一些第三方FTP庫,它不會發送那些有問題的命令。
  • 因爲在命令行上載文件的作品,創建相應的文件(commands.txt中)「把」命令,然後開始從你的程序是這樣的FTP傳輸:

Process.Start("ftp", "-A -s:commands.txt " + printer.m_IPAddress);

+0

好的,我按照你的建議跑了TRACE,這就是記錄的內容: – chuck 2012-04-18 20:39:50

+0

記錄了什麼?我什麼也沒看到。 – 2012-04-18 20:49:05

+0

這是輸出:System.Net詳細:0:[4780] FtpWebRequest#14993092 :: GetRequestStream() 系統。Net Information:0:[4780] FtpWebRequest#14993092 :: GetRequestStream(Method = STOR。) System.Net信息:0:[4780]將FtpWebRequest#14993092與FtpControlStream關聯#720107System.Net信息:0:[4780] FtpControlStream# 720107 - 收到響應[220 ZBR-46688 1.01.7版準備就緒。] System.Net信息:0:[4780] FtpControlStream#720107 - 發送命令[用戶匿名] System.Net信息:0:[4780] FtpControlStream# 720107 - 收到的回覆[230用戶登錄] – chuck 2012-04-18 21:13:15

0

ftpRequest( FtpWebRequest)創建,我們有一個屬性EnableSsl。如果FTP站點中的SSL被禁用,則該標誌需要設置爲false,否則該標誌需要設置爲true。

ftpRequest.EnableSsl = true; 

這必須在打開StreamReader之前完成。