本文介紹了想使用 C# (FtpWebResponse) 從 FTP 讀取文件列表,但它返回 HTML的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
限時(shí)送ChatGPT賬號..
我使用以下代碼從 FTP 站點(diǎn)獲取文件.它可以在我的計(jì)算機(jī)上運(yùn)行,??但是當(dāng)我在另一臺計(jì)算機(jī)上運(yùn)行它時(shí)它只會(huì)返回 HTML 代碼(當(dāng)我通過瀏覽器訪問 FTP 時(shí),我可以看到 HTML 是網(wǎng)頁的代碼).怎么了?
I use below codes to get files from a FTP site. It works in my computer, but it only return HTML codes when I run it on another computer (I can see that the HTML are codes of web page when I access FTP via browser). What's wrong?
public String GetFilesAsString(string folder,string fileExtension)
{
StringBuilder result = new StringBuilder();
FtpWebRequest reqFTP;
try
{
String ftpserver = ftp + folder+"/";
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpserver));
reqFTP.UsePassive = false;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(username, password);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);
string line = "";
while (reader.Peek()>-1)
{
line = reader.ReadLine();
Console.WriteLine(line);//**********HTML was wrote out here*************
}
if (result.ToString().LastIndexOf('
') >= 0)
result.Remove(result.ToString().LastIndexOf('
'), 1);
reader.Close();
response.Close();
return result.ToString();
}
catch (Exception ex)
{
}
return null;
}
推薦答案
會(huì)不會(huì)是網(wǎng)絡(luò)代理干擾?嘗試使用以下方法繞過代理:
Could it be a web proxy interfering? Try to get around the proxy by using the following:
reqFTP.Proxy = GlobalProxySelection.GetEmptyWebProxy();
這篇關(guān)于想使用 C# (FtpWebResponse) 從 FTP 讀取文件列表,但它返回 HTML的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!