我使用 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
现在,我正在尝试使用 MS VS2010 和 C# 从 WinForms 应用程序自动执行此操作。但是由于某种原因,我似乎没有尝试使用 FtpWebRequest 类,我也收到一个错误
远程服务器返回错误:(502) 命令未执行。
据我了解,ftp 方法 UploadFile 与 STOR 相同,与别名 put 相同。我正在使用的代码如下所示:
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;
}
有谁看到我可能做错了什么。任何想法,将不胜感激。谢谢。
我运行了 TRACE,这是输出:
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 发送的内容还是我需要第 3 方库?