![](/img/trans.png)
[英]Error : File partially Uploading to linux FTP using C# FTPwebRequest
[英]Error trying to use C# ftpwebrequest to ftp xml file to Zebra printer
我已經使用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:\\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相同,與別名放置相同。 我正在使用的代碼如下所示:
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發送的內容,還是我需要第三方庫?
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服務器。 不幸的是,我不知道如何阻止發送這些命令。 我只有2種解決方法:
Process.Start("ftp", "-A -s:commands.txt " + printer.m_IPAddress);
在創建的ftpRequest
(FtpWebRequest)中,我們有一個屬性EnableSsl。 如果在FTP站點中禁用了SSL,則此標志需要設置為false,否則該標志需要設置為true。
ftpRequest.EnableSsl = true;
這必須在打開StreamReader
之前完成。
我知道這個問題已有一年多的歷史了,但其他人可能會覺得有用。 如果您的目標是將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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.