Want to use C # (FtpWebResponse) to read a list of files from FTP, but it returns HTML

I use the codes below to retrieve files from an FTP site. It works on my computer, but it only returns HTML codes when I run it on another computer (I see that HTML is the web page codes when accessing FTP via a 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('\n') >= 0) result.Remove(result.ToString().LastIndexOf('\n'), 1); reader.Close(); response.Close(); return result.ToString(); } catch (Exception ex) { } return null; } 

enter image description here

+4
source share
4 answers

Can this interfere with web proxy? Try to bypass the proxy server using the following:

 reqFTP.Proxy = GlobalProxySelection.GetEmptyWebProxy(); 
+5
source

This is the result of using FtpWebRequest through an HTTP proxy. The list of files is printed with HTML tags that have <A> hyperlinks to individual files in the list.

If you cannot bypass the proxy server, in our case it was possible to clear the section with the contents of the file that does not contain the <PRE> element, load it into XmlDocument and pull the list of files through .SelectNodes("//A/text()")

+1
source

I found a solution: the default proxy was turned on unexpectedly

Now I need to disable it using the configuration file:

 <?xml version="1.0" encoding="utf-8" ?> <configuration> <system.net> <defaultProxy enabled="false" useDefaultCredentials="true"/> </system.net> </configuration> 

Actually this is really a .NET problem!

+1
source

FTP PassiveMode required to upload, upload ...

Instead, try using:

 reqFTP.UsePassive = true; 
0
source

Source: https://habr.com/ru/post/1390613/


All Articles