FtpWebRequest over SSL
- From: "William_melvin" <William_melvin@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 13 Jan 2006 06:02:03 -0800
I have been working for the last several days on a small progam to retrieve
the directory from an FTP server. I have one site that is not using SSL, and
the code works fine, but another site that uses SSL times out on the
GetResponse method. The code that works looks like this:
#region Using directives
using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Collections;
using System.Security.Cryptography.X509Certificates;
#endregion
namespace TestFTPService
{
class Program
{
static void Main(string[] args)
{
// The serverUri parameter should start with the ftp:// scheme.
UriBuilder uriBuilder = new UriBuilder();
uriBuilder.Scheme = Uri.UriSchemeFtp;
uriBuilder.Host = "222.222.222.222";
uriBuilder.Path = "";
Uri serverUri = new Uri(uriBuilder.ToString());
// Get the object used to communicate with the server.
FtpWebRequest request =
(FtpWebRequest)WebRequest.Create(serverUri);
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.Credentials = new NetworkCredential("abced", "zzzzz99",
"abc.com");
request.EnableSsl = false;
request.UsePassive = false;
request.UseBinary = false;
// Get the ServicePoint object used for this request, and limit
it to one connection.
// In a real-world application you might use the default number
of connections (2),
// or select a value that works best for your application.
ServicePoint sp = request.ServicePoint;
Console.WriteLine("ServicePoint connections = {0}.",
sp.ConnectionLimit);
sp.ConnectionLimit = 1;
FtpWebResponse response = null;
try
{
response = (FtpWebResponse)request.GetResponse();
}
catch (WebException ex1)
{
Console.WriteLine(ex1.ToString());
}
Console.WriteLine("The content length is {0}",
response.ContentLength);
// The following streams are used to read the data returned from
the server.
Stream responseStream = null;
StreamReader readStream = null;
try
{
responseStream = response.GetResponseStream();
readStream = new StreamReader(responseStream,
System.Text.Encoding.UTF8);
if (readStream != null)
{
// Display the data received from the server.
Console.WriteLine(readStream.ReadToEnd());
}
Console.WriteLine("List status: {0}",
response.StatusDescription);
}
finally
{
if (readStream != null)
{
readStream.Close();
}
if (response != null)
{
response.Close();
}
}
Console.WriteLine("Banner message: {0}",
response.BannerMessage);
Console.WriteLine("Welcome message: {0}",
response.WelcomeMessage);
Console.WriteLine("Exit message: {0}",
response.ExitMessage);
// return true;
Console.ReadLine();
}
}
}
when I change the credentials code to match the user information of the
secure site, change the host address, change the enableSSL flag to true and
change UsePassive to true (which is what my SSL site requires), the
GetResponse times out.
This is output from an FTP client program that is connecting to my SSL ftp
server:
WinSock 2.0 -- OpenSSL 0.9.7g 11 Apr 2005
[07:50:30] [R] Connecting to abc.123.com -> DNS=abc.123.com
IP=123.123.123.123 PORT=21
[07:50:30] [R] Connected to abc.123.com
[07:50:30] [R] 220-bla bla bla
[07:50:30] [R] AUTH TLS
[07:50:30] [R] 234 AUTH Command OK. Initializing SSL connection.
[07:50:30] [R] Connected. Negotiating TLSv1 session..
[07:50:30] [R] TLSv1 negotiation successful...
[07:50:30] [R] TLSv1 encrypted session using cipher AES256-SHA (256 bits)
[07:50:30] [R] PBSZ 0
[07:50:30] [R] 200 PBSZ Command OK. Protection buffer size set to 0.
[07:50:30] [R] USER xxxxx
[07:50:30] [R] 331 Password required for xxxxx.
[07:50:30] [R] PASS (hidden)
[07:50:30] [R] 230-If you encounter difficulties using this FTP server,
please contact the
[07:50:30] [R] 230 IT HelpDesk at 1-900-900-9999.
[07:50:30] [R] SYST
[07:50:30] [R] 215 UNIX Type: L8
[07:50:30] [R] FEAT
[07:50:30] [R] 211-Features supported:
[07:50:30] [R] COMB target;source_list
[07:50:30] [R] REST STREAM
[07:50:30] [R] SIZE
[07:50:30] [R] MDTM
[07:50:30] [R] XCRC filename;start;end
[07:50:30] [R] SSCN
[07:50:30] [R] MLST
Size*;Modify*;Create;Type*;Unique;Perm*;Lang;Media-Type;CharSet;
[07:50:30] [R] MODE Z
[07:50:30] [R] 211 END
[07:50:30] [R] PWD
[07:50:30] [R] 257 "/" is current folder.
[07:50:30] [R] List (cached)
[07:50:30] [R] List Complete: 2 KB in 0.06 seconds (33.7 KB/s)
[07:52:35] [R] QUIT
[07:52:35] [R] 221 Service closing control connection.
[07:52:35] [R] Logged off: abc.123.com
Any help that you could provide would be greatly appreciated.
Thanks,
William
.
- Prev by Date: Re: System.Security.SecurityException: Request failed.
- Next by Date: Re: Using caspol to import a strong name codegroup
- Previous by thread: Issuing X.509 Certificates
- Next by thread: Request.IsAuthenticated returning false when it shouldn't
- Index(es):
Relevant Pages
|