問題描述
我在將 Windows 服務(wù)連接到 FTP 站點(diǎn)時(shí)遇到問題.
I am having a problem connecting a Windows service to an FTP site.
我從另一個(gè)開發(fā)人員那里繼承了一項(xiàng) Windows 服務(wù).該服務(wù)連接到第 3 方服務(wù)器,下載 csv 文件,然后對其進(jìn)行處理.由于某種原因,該服務(wù)停止工作(一年多以前,在我獲得項(xiàng)目之前).
I inherited a Windows service from another developer. The service connects to a 3rd party server, downloads a csv file and then processes it. For some reason, the service stopped working (well over a year ago, before I was given the project).
所以我回到了基礎(chǔ),創(chuàng)建了一個(gè)控制臺應(yīng)用程序,并僅在該應(yīng)用程序中嘗試了連接/文件下載功能.我嘗試了許多不同的方法來連接到 FTP,但它們都向我的應(yīng)用程序返回相同的錯(cuò)誤:
So I went back to basics, created a console app and tried the connection/ file download function only in that app. I have tried many different methods to connect to the FTP, but all of them return the same error to my application:
遠(yuǎn)程服務(wù)器返回錯(cuò)誤:227 Entering Passive Mode ()
The remote server returned an error: 227 Entering Passive Mode ()
這是我嘗試過的眾多方法之一:
This is one of the many methods I've tried:
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftpaddress/filename.csv");
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential("username", "password");
request.UsePassive = true;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
Console.WriteLine(reader.ReadToEnd());
Console.WriteLine("Download Complete, status {0}", response.StatusDescription);
reader.Close();
response.Close();
但它落在這部分:
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
我在幾個(gè)論壇中看到將 UsePassive 屬性設(shè)置為 False 可以修復(fù)這些錯(cuò)誤,但發(fā)生在我身上的只是我得到了一個(gè)語法錯(cuò)誤,如下所示:
I read in several forums that setting the UsePassive property to False fixes these errors, but all that happened to me was that I got a syntax error instead, as below:
遠(yuǎn)程服務(wù)器返回錯(cuò)誤:(500) 語法錯(cuò)誤,命令無法識別.
The remote server returned an error: (500) Syntax error, command unrecognized.
該文件托管在我無法控制的第 3 方 FTP 服務(wù)器上.我可以將 URL 粘貼到瀏覽器中,系統(tǒng)會提示我輸入用戶名和密碼,然后我就可以通過并下載文件.
The file is hosted on a 3rd party FTP server I have no control over. I can paste the URL into a browser, and I am prompted for a username and password, which then allows me through and I can download the file.
為了消除我們的防火墻作為問題的原因,我在內(nèi)部網(wǎng)絡(luò)和 WiFi(不在防火墻后面)上運(yùn)行了該應(yīng)用程序,這沒有任何區(qū)別.我還在默認(rèn)、主動和被動模式下通過 FileZilla 進(jìn)行連接,并且每次都能正常工作.所以沒有問題.
To eliminate our firewall as the cause of the problem, I ran the app on both the internal network and the WiFi (which isn't behind the firewall), and it makes no difference. I also connected through FileZilla in Default, Active and Passive modes, and it worked every time. So no problem there.
然后我運(yùn)行了 Wireshark.這是在被動模式下使用 Filezilla(即成功的)捕獲線路的圖像:
So then I ran Wireshark. Here is an image of the wire capture using Filezilla (i.e. a successful one), in Passive mode:
這是使用應(yīng)用程序連接(和失敗)時(shí)的捕獲,被動設(shè)置為 true:
And here is the capture when connecting (and failing) using the app, with passive set to true:
所以正如您在上面的失敗連接中看到的那樣,我可以很好地登錄服務(wù)器.然后無論出于何種原因發(fā)送了一個(gè)額外的請求,即TYPE I",它提示切換到二進(jìn)制模式"的響應(yīng).在下面,我得到以下內(nèi)容:
So as you can see in the failed connection above, I can log in to the server just fine. Then for whatever reason an extra request is sent, namely "TYPE I", which prompts the response of "Switching to binary mode." The below that, I get the following:
500 oops:vsf_sysutil_recv_peek:無數(shù)據(jù)
500 oops: vsf_sysutil_recv_peek: no data
另外我也把Passive屬性設(shè)置為false后又跑了一次,這就是我當(dāng)時(shí)得到的:
In addition, I also ran it again after setting the Passive property to false, and this is what I got that time:
所以我的問題是雙重的;
So my question is twofold;
1,如果我以某種方式解決了 UsePassive 問題并將該屬性設(shè)置為 false,那會解決我的問題嗎?
1, if I somehow get past the UsePassive issue and set that property to false, will that solve my problem?
2,忽略 UsePassive 屬性,為什么我不能從應(yīng)用程序下載文件,但可以從其他地方下載文件?
2, ignoring the UsePassive property, why can't I download the file from the app, but can from everywhere else?
推薦答案
問題現(xiàn)已解決.原來是卡巴斯基的內(nèi)置防火墻阻止了連接.很煩人,當(dāng)我嘗試連接時(shí)它沒有向我顯示警告,但知道我的電腦是安全的.
The issue is now resolved. It turned out to be Kaspersky's built-in firewall that was blocking the connection. It's annoying that it didn't present me with a warning when I tried to connect, but reassuring to know my PC is safe.
線索在 227 返回的細(xì)節(jié)中:
The clue was in the detail of the 227 return:
10051 – 嘗試對無法訪問的網(wǎng)絡(luò)進(jìn)行套接字操作
10051 – A socket operation was attempted to an unreachable network
此外,對于通過 Google 等訪問此內(nèi)容的任何人,遠(yuǎn)程服務(wù)器被配置為僅允許被動連接,這就是我收到 500 語法錯(cuò)誤的原因.研究下載文件時(shí)的 Wire 捕獲發(fā)現(xiàn),如果選擇 Active 但失敗,F(xiàn)ilezilla 實(shí)際上會自動恢復(fù)到 Passive 模式.
Also, for anyone reaching this via Google etc, the remote server was configured to only allow Passive connections, which is why I was getting the 500 syntax error. Studying a Wire capture when downloading a file revealed that Filezilla actually reverts to Passive mode automatically if Active is selected but fails.
我原來帖子中的代碼現(xiàn)在可以正常工作了.
The code in my original post works fine now.
這篇關(guān)于遠(yuǎn)程服務(wù)器返回錯(cuò)誤:227 Entering Passive Mode (500 oops vs_utility_recv_peek: no data)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!