我已經使用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.
所以看起來OPTS和PWD都是問題。有沒有辦法控制FtpWebRequest發送的內容,還是我需要第三方庫?
好的,我按照你的建議跑了TRACE,這就是記錄的內容: – chuck 2012-04-18 20:39:50
記錄了什麼?我什麼也沒看到。 – 2012-04-18 20:49:05
這是輸出: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