FtpWebRequest Response returns Html for ListDirectory

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

This is just plain odd. Here is my code:
Console.WriteLine("Connecting to server to list directory...");
Uri uri = new Uri(Settings.FtpServer);
FtpWebRequest listRequest = (FtpWebRequest)WebRequest.Create(uri);
listRequest.Credentials = new
NetworkCredential(Settings.FtpUser, Settings.FtpPassword);
listRequest.Method = WebRequestMethods.Ftp.ListDirectory;
listRequest.KeepAlive = false;
listRequest.UseBinary = false;
listRequest.UsePassive = false;
FtpWebResponse listResponse =
(FtpWebResponse)listRequest.GetResponse();
StreamReader reader = new
StreamReader(listResponse.GetResponseStream());
Console.WriteLine("List Response:");
Console.WriteLine(reader.ReadToEnd());

I get the following output:
Connecting to server to list directory...
List Response:
A.zip
B.zip
C.zip

(which are the files on the ftp server)

Running the EXACT same code on a DIFFERENT client machine, hitting the EXACT
same ftp server. I get this:
Connecting to server to list directory...
List Response:
<HTML>
<meta http-equiv="Content-Type" content="text-html; charset=UTF-8">
<HEAD>
<TITLE>FTP root at 172.17.2.20. </TITLE>
</HEAD>
<BODY>
<H1>FTP root at 172.17.2.20. </H1>
<HR>
<PRE>
01/18/06 06:41PM <DIR> <A HREF="/./">.</A>
01/18/06 06:41PM <DIR> <A HREF="/../">..</A>
01/07/03 12:00AM 199 <A HREF="/A.zip">A.zip</A>
01/18/06 01:01AM 225 <A HREF="/B.zip">B.zip</A>
07/26/06 12:32PM 5,902 <A HREF="/C.zip">C.zip</A>
</PRE>
</HR>
</BODY>
</HTML>

It is returning the listing of directory names as HTML. Has anyone seen
this before? I haven't been able to figure out what is different about the
two client machines or how to fix it.

Thanks,
Brian Orrell
Pariveda Solutions
 
Hi Brian,

Based on my test on multiple machines including windows xp, windows 2003.
The code below will return the HTML like string.

static void Main(string[] args)
{
Console.WriteLine("Connecting to server to list directory...");
Uri uri = new Uri("ftp://ftp.gnu.org/");
FtpWebRequest listRequest =
(FtpWebRequest)WebRequest.Create(uri);
//listRequest.Credentials = CredentialCache.DefaultCredentials;
listRequest.Method = WebRequestMethods.Ftp.ListDirectory;
listRequest.KeepAlive = true;
listRequest.UseBinary = true;
listRequest.UsePassive = true;
FtpWebResponse listResponse =
(FtpWebResponse)listRequest.GetResponse();
//Console.WriteLine(listResponse.BannerMessage.ToString());
StreamReader reader = new
StreamReader(listResponse.GetResponseStream());
string st = reader.ReadToEnd();
Console.WriteLine("List Response:");
Console.WriteLine(st);
}

So I suggest you try other machines to see what is the result.
Also can you provide more information about the scenario that will return
the non-HTML string?

If you still have any concern, please feel free to post here.
Thanks for your understanding!

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
The response on my machine when I run the code is:
Connecting to server to list directory...
List Response:
CRYPTO.README
MISSING-FILES
MISSING-FILES.README
MISSING-FILES~
README
README.~1~
before-2003-08-01.md5sums.asc
gnu
gnu+linux-distros
iso
lpf.README
ls-lrRt.txt.gz
mirrors
non-gnu
old-gnu
pub
savannah
third-party
welcome.msg
welcome.msg~

That is what is expected to happen, Not the Html response. All the
examples that are provided in MSDN do not show an Html response, they show a
line by line listing of files.

Here is an example that was posted on how to retrieve a list of files
available on an ftp server.

Dim reqUri As String
reqUri = String.Format("ftp://{0}:{1}/", server, portNumber)
Dim req As FtpWebRequest = CType(WebRequest.Create(reqUri), FtpWebRequest)
req.Credentials = New NetworkCredential(userName, password)
req.Method = WebRequestMethods.Ftp.ListDirectory
Dim resp As FtpWebResponse = CType(req.GetResponse, FtpWebResponse)
Dim reader As New IO.StreamReader(resp.GetResponseStream)

Dim files as New List(Of String)
While Not reader.EndOfStream
files.Add(reader.ReadLine)
End While

This code would not work if it returns Html.

I can't identify any difference between the setup between my two machines,
but it is troubling that the response back from the same Ftp server with the
same code would be different dependent upon the client machine. It sounds
like the machines you tested on are also acting eratically-- HTML is not the
expected response for a ListDirectory method.

Thanks for looking into this,
Brian

--
Brian Orrell
Pariveda Solutions


"Peter Huang" said:
Hi Brian,

Based on my test on multiple machines including windows xp, windows 2003.
The code below will return the HTML like string.

static void Main(string[] args)
{
Console.WriteLine("Connecting to server to list directory...");
Uri uri = new Uri("ftp://ftp.gnu.org/");
FtpWebRequest listRequest =
(FtpWebRequest)WebRequest.Create(uri);
//listRequest.Credentials = CredentialCache.DefaultCredentials;
listRequest.Method = WebRequestMethods.Ftp.ListDirectory;
listRequest.KeepAlive = true;
listRequest.UseBinary = true;
listRequest.UsePassive = true;
FtpWebResponse listResponse =
(FtpWebResponse)listRequest.GetResponse();
//Console.WriteLine(listResponse.BannerMessage.ToString());
StreamReader reader = new
StreamReader(listResponse.GetResponseStream());
string st = reader.ReadToEnd();
Console.WriteLine("List Response:");
Console.WriteLine(st);
}

So I suggest you try other machines to see what is the result.
Also can you provide more information about the scenario that will return
the non-HTML string?

If you still have any concern, please feel free to post here.
Thanks for your understanding!

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi

Thanks for your quickly reply!

Currently I am researching the issue and we will reply here with more
information as soon as possible.
If you have any more concerns on it, please feel free to post here.

Thanks for your understanding!

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Brian,

From you description, I understand when you use the FtpWebRequest class to
list the content of an FTP site, you receive a result in HTML format. You
want to know how to make the FtpWebRequest class return a plain text result.
If I misunderstood, please feel free to let me know.

Based on my research, we can check whether we are making the connection via
an HTTP proxy. If HTTP proxy is used, you will get the result in the HTML
format.

By default, the FtpWebRequest class will use the HTTP proxy setting for IE.
We can check the IE proxy setting via the steps below.
1. Open an IE window
2. Click Tools
3. Click Internet Options
4. Click Connections Tab
5.1. If you are using a LAN to access the FTP server, click the LAN Setting
5.2. Uncheck all the checkboxes in the dialog box to make sure we are not
using a proxy.
6. Run the test code again.

If you still have any concern, please feel free to let me know.
I look forward to hearing from you.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Thanks for the info. Upon further research, if you want force your
FtpWebRequest not to use an HttpProxy if it is specified in IE, set the proxy
property to:

listRequest.Proxy = GlobalProxySelection.GetEmptyWebProxy();

Thanks for the help,
-Brian
 
Back
Top