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

现在,我正在尝试使用 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.

所以看起来OPTSPWD是问题所在。有什么方法可以控制 FtpWebRequest 发送的内容还是我需要第 3 方库?

4

3 回答 3

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>

所有命令和响应都将记录在文件中,因此您可以查看是哪一个导致了问题。

更新: 所以问题是,在发出 STOR 命令之前,FTP 客户端发送 OPTS 和 PWD 命令,您的打印机不理解这些命令,可能是因为它是非常轻量级的 FTP 服务器。不幸的是,我不知道如何防止发送这些命令。我只有两个解决方法:

  • 找到一些第三方 FTP 库,它不会发送那些有问题的命令。
  • 由于从命令行上传文件有效,请使用适当的“put”命令创建文件(commands.txt),然后从您的程序开始 FTP 传输,如下所示:

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

于 2012-04-18T18:39:35.823 回答
0

我知道这个问题已经有一年多了,但其他人可能会觉得这很有用。如果您的目标是将 ZPL 代码或 XML 文件发送到您的打印机,则您不受 ftp 协议的约束。我通过 TCP/IP 套接字发送我的代码。这是我使用的:

public void SendCode(string ipAddress, string zplCode)
    {
        Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        clientSocket.NoDelay = true;

        IPAddress ip = IPAddress.Parse(ipAdress);
        IPEndPoint ipep = new IPEndPoint(ip, 9100);
        clientSocket.Connect(ipep);

        byte[] stringBytes = Encoding.UTF8.GetBytes(zplCode);

        clientSocket.Send(stringBytes);
        clientSocket.Close();
    }
于 2019-07-18T05:07:41.127 回答
0

ftpRequest(FtpWebRequest)创建的时候,我们有一个属性EnableSsl。如果在 FTP 站点中禁用 SSL,则需要将此标志设置为 false,否则需要将此标志设置为 true。

ftpRequest.EnableSsl = true;

这必须在打开StreamReader.

于 2018-02-07T12:33:24.573 回答