E
Edison.CCC
I'm facing a prblem with FTPWebRequest of .NET v2.0
1. The FTP server I have to access is a small device(not a PC)
which provides 2 folders named c: & d:. While attempting to
access/get/put files I always get the following exception
System.Net.WebException: The remote server returned an error: (550)
File unavail
able (e.g., file not found, no access).
at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
at System.Net.FtpWebRequest.RequestCallback(Object obj)
at System.Net.CommandStream.Abort(Exception e)
at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
at System.Net.FtpWebRequest.GetResponse()
at ConsoleApplication1.Program.Main(String[] args)
Can anyone provide some pointers/ideas? Other FTP client softwares
like WS-FTP are able to connect
and down/upload without any issues to that server.
Thanks in advance,
Ed.
I have provided my code snippet below for your reference -
Private Sub Download(ByVal downloadUrl As String)
Dim responseStream As Stream = Nothing
Dim fileStream As FileStream = Nothing
Dim reader As StreamReader = Nothing
Try
Dim downloadRequest As FtpWebRequest = _
WebRequest.Create(downloadUrl)
downloadRequest.Proxy = Nothing
downloadRequest.UsePassive = True
downloadRequest.UseBinary = True
downloadRequest.Credentials = New _
System.Net.NetworkCredential("xx", "xx")
Dim downloadResponse As FtpWebResponse = _
downloadRequest.GetResponse()
responseStream = downloadResponse.GetResponseStream()
Dim fileName As String = _
Path.GetFileName(downloadRequest.RequestUri.AbsolutePath)
If fileName.Length = 0 Then
reader = New StreamReader(responseStream)
Console.WriteLine(reader.ReadToEnd())
Else
fileStream = File.Create(fileName)
Dim buffer(1024) As Byte
Dim bytesRead As Integer
While True
bytesRead = responseStream.Read(buffer, 0,
buffer.Length)
If bytesRead = 0 Then
Exit While
End If
fileStream.Write(buffer, 0, bytesRead)
End While
End If
Console.WriteLine(downloadResponse.StatusCode & " " &
downloadResponse.StatusDescription)
Console.WriteLine("Download complete.")
Catch ex As UriFormatException
Console.WriteLine(ex.Message)
Catch ex As WebException
Console.WriteLine(ex.Message)
Catch ex As IOException
Console.WriteLine(ex.Message)
Finally
If reader IsNot Nothing Then
reader.Close()
ElseIf responseStream IsNot Nothing Then
responseStream.Close()
End If
If fileStream IsNot Nothing Then
fileStream.Close()
End If
End Try
End Sub
1. The FTP server I have to access is a small device(not a PC)
which provides 2 folders named c: & d:. While attempting to
access/get/put files I always get the following exception
System.Net.WebException: The remote server returned an error: (550)
File unavail
able (e.g., file not found, no access).
at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
at System.Net.FtpWebRequest.RequestCallback(Object obj)
at System.Net.CommandStream.Abort(Exception e)
at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
at System.Net.FtpWebRequest.GetResponse()
at ConsoleApplication1.Program.Main(String[] args)
Can anyone provide some pointers/ideas? Other FTP client softwares
like WS-FTP are able to connect
and down/upload without any issues to that server.
Thanks in advance,
Ed.
I have provided my code snippet below for your reference -
Private Sub Download(ByVal downloadUrl As String)
Dim responseStream As Stream = Nothing
Dim fileStream As FileStream = Nothing
Dim reader As StreamReader = Nothing
Try
Dim downloadRequest As FtpWebRequest = _
WebRequest.Create(downloadUrl)
downloadRequest.Proxy = Nothing
downloadRequest.UsePassive = True
downloadRequest.UseBinary = True
downloadRequest.Credentials = New _
System.Net.NetworkCredential("xx", "xx")
Dim downloadResponse As FtpWebResponse = _
downloadRequest.GetResponse()
responseStream = downloadResponse.GetResponseStream()
Dim fileName As String = _
Path.GetFileName(downloadRequest.RequestUri.AbsolutePath)
If fileName.Length = 0 Then
reader = New StreamReader(responseStream)
Console.WriteLine(reader.ReadToEnd())
Else
fileStream = File.Create(fileName)
Dim buffer(1024) As Byte
Dim bytesRead As Integer
While True
bytesRead = responseStream.Read(buffer, 0,
buffer.Length)
If bytesRead = 0 Then
Exit While
End If
fileStream.Write(buffer, 0, bytesRead)
End While
End If
Console.WriteLine(downloadResponse.StatusCode & " " &
downloadResponse.StatusDescription)
Console.WriteLine("Download complete.")
Catch ex As UriFormatException
Console.WriteLine(ex.Message)
Catch ex As WebException
Console.WriteLine(ex.Message)
Catch ex As IOException
Console.WriteLine(ex.Message)
Finally
If reader IsNot Nothing Then
reader.Close()
ElseIf responseStream IsNot Nothing Then
responseStream.Close()
End If
If fileStream IsNot Nothing Then
fileStream.Close()
End If
End Try
End Sub