以下のコード (.Net 2.0 である必要があります) を使用して、UAT サーバーで顧客の FTP サーバーに接続し、ファイルをアップロード/ダウンロードします。彼らが提供する自己署名証明書を使用して、ポート 990 経由で接続する必要があります。UAT サーバーからポート 990 の URI への接続を許可するように、ファイアウォール ルールを修正しました。
ただし (! :)) 回線でタイムアウトが発生します
Stream requestStream = request.GetRequestStream();
タイムアウト時間を増やしても違いはありません。
私はウェブを見回しましたが、コードに欠けている明らかなものは何も見つかりませんでした。
CuteFTP を使用して UAT サーバーに接続すると、当然、正常に接続され、手動でファイル転送を行うことができます。WireShark を使用してネットワーク トラフィックを確認すると、FTP サーバーから応答が返されますが、ユーザー ID と pwd (コード用) のハンドシェイクは行われませんが、CuteFTP を介してすべてのネットワーク トラフィックは問題ありません。
証明書をチェックする場所で True を強制的に返します。
private void button4_Click(object sender, EventArgs e)
{
try
{
string completeFTPPath = ConfigurationManager.AppSettings["FTPPath"];
// get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(completeFTPPath);
request.EnableSsl = true;
request.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["FtpUserName"], ConfigurationManager.AppSettings["FtpPassword"]);
request.Method = WebRequestMethods.Ftp.UploadFile;
ServicePointManager.ServerCertificateValidationCallback = AcceptAllCertifications;
// read file into byte array
StreamReader sourceStream = new StreamReader(ConfigurationManager.AppSettings["LocalFilePath"]);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
// send bytes to server
MessageBox.Show("GetRequestStream() start");
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
MessageBox.Show("GetRequestStream() end");
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
MessageBox.Show("Response status: " + response.StatusDescription);
}
catch (WebException we)
{
MessageBox.Show(we.Message);
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
public bool AcceptAllCertifications(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{ return true; }
例: FTPPath - ftp://111.222.333.444:990/UAT/testFile.zip ; FtpUserName - ユーザー ID; FtpPassword = userPwd; LocalFilePath - c:\temp\testFile.zip
誰でもアイデアはありますか?一部の人々は上記のコードが機能しているようです。ティア。