Я могу подключаться к серверу по FTP и загружать файлы во время работы VS2013 в отладке, но когда я компилирую как Release | x86 Я получаю сообщение об ошибке «Не удается подключиться к удаленному серверу» в строке, вызывающей System.Net.FtpWebRequest.GetRequestStream (). Я могу получить доступ к FTP и загрузить файлы с помощью WinSCP. Я запустил WireShark и вообще не видел пакетов из версии Release моего кода, но видел все соответствующие пакеты, когда выполнял из режима отладки в VS2013. У меня нет брандмауэра, и он действительно поставил меня в тупик. Любая помощь будет принята с благодарностью. - Чак
public bool UploadToFtp(string sFtpServer, string sFtpUserName, string sFtpPassword, string sSourceLocationPath, string sFtpDestinationPath, StringBuilder sbLogMessage)
{
bool bUpload;
try
{
var oFile = new FileInfo(sSourceLocationPath);
var oUri = new Uri(sFtpServer + sFtpDestinationPath + oFile.Name);
var oFtp = (FtpWebRequest)WebRequest.Create(oUri);
oFtp.Credentials = new NetworkCredential(sFtpUserName, sFtpPassword);
oFtp.KeepAlive = false;
oFtp.UseBinary = true;
oFtp.UsePassive = true;
oFtp.Timeout = 10000000;
oFtp.ReadWriteTimeout = 10000000;
oFtp.Method = WebRequestMethods.Ftp.UploadFile;
oFtp.ContentLength = oFile.Length;
var arrContent = File.ReadAllBytes(oFile.FullName);
using (var oFileStream = oFile.OpenRead())
{
using (var oStream = oFtp.GetRequestStream())
{
int iReadBytes;
do
{
iReadBytes = oFileStream.Read(arrContent, 0, int.Parse(oFile.Length.ToString(CultureInfo.InvariantCulture)));
oStream.Write(arrContent, 0, iReadBytes);
} while (iReadBytes != 0);
bUpload = true;
oStream.Flush();
oStream.Close();
oFileStream.Close();
}
}
File.Delete(sSourceLocationPath + oFile.Name);
}
catch (Exception ex)
{
LogMessage(ex.Message);
bUpload = false;
}
return bUpload;
}
***** РЕДАКТИРОВАТЬ ***** Я создал журнал сетевой трассировки, как было предложено, и получил следующий результат (ПРИМЕЧАНИЕ: я изменил фактический IP-адрес и имя файла в этом листинге):
System.Net Verbose: 0 : [27252] WebRequest::Create(ftp://1.2.3.4//file.zip)
System.Net Information: 0 : [27252] FtpWebRequest#28316044::.ctor(ftp://1.2.3.4//file.zip)
System.Net Verbose: 0 : [27252] Exiting WebRequest::Create() -> FtpWebRequest#28316044
System.Net Verbose: 0 : [27252] FtpWebRequest#28316044::GetRequestStream()
System.Net Information: 0 : [27252] FtpWebRequest#28316044::GetRequestStream(Method=STOR.)
System.Net Information: 0 : [27252] Current OS installation type is 'Client'.
System.Net Verbose: 0 : [27252] ServicePoint#39974954::ServicePoint(1.2.3.4:21)
System.Net.Sockets Verbose: 0 : [27252] Socket#24230272::Socket(AddressFamily#2)
System.Net.Sockets Verbose: 0 : [27252] Exiting Socket#24230272::Socket()
System.Net.Sockets Verbose: 0 : [27252] Socket#16745860::Socket(AddressFamily#23)
System.Net.Sockets Verbose: 0 : [27252] Exiting Socket#16745860::Socket()
System.Net.Sockets Verbose: 0 : [27252] DNS::TryInternalResolve(1.2.3.4)
System.Net.Sockets Verbose: 0 : [27252] Socket#24230272::Connect(1.2.3.4:21#1294145406)
System.Net.Sockets Error: 0 : [27252] Socket#24230272::UpdateStatusAfterSocketError() - TimedOut
System.Net.Sockets Error: 0 : [27252] Exception in Socket#24230272::Connect - A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 1.2.3.4:21.
System.Net Error: 0 : [27252] Exception in FtpWebRequest#28316044::GetRequestStream - Unable to connect to the remote server.
at System.Net.FtpWebRequest.GetRequestStream()
System.Net Verbose: 0 : [27252] Exiting FtpWebRequest#28316044::GetRequestStream()