English 中文(简体)
需使用C#(FtpWebResponse)阅读从FTP收到的档案清单,但回报为超文本
原标题:Want to use C# (FtpWebResponse) to read file list from FTP, but it returns HTML

我使用以下代码从FTP网站获取档案。 它使用我的计算机,但在我用另一个电脑操作时,它只使用回音码(我可以看到,当我通过浏览器查阅FTP时,超文本是网页的代码)。 什么错误?

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;
}

“enter

最佳回答

它是网络代理干预吗? 采用以下方法处理代理人:

reqFTP.Proxy = GlobalProxySelection.GetEmptyWebProxy();
问题回答

这是使用<代码>FtpWebRequest通过HTTP Proxy的结果。 文档清单用超文本标签印制,上面有<代码><A>超文本链接。

如果你可以绕过代理,就我们而言,可以将该部分与文件内容放在一个封条的封面上:<PRE>,将其装入一个<>XmlDocument,并将文件清单从>.SelectNodes("/A/text()”/code>。

我找到了解决办法:意外地使缺席代理成为可能。

我现在不得不特别用一个书面文件来解脱:

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

事实上,这是一个真正的问题!

FTP 要求PassiveMode 页: 1

相反,试图利用:

reqFTP.UsePassive = true;




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...