关于FtpWebRequest有关问题
关于FtpWebRequest问题
我写了两个方法a和b,先后调用Download方法,a方法先执行,b方法后执行,每次b方法调用Download方法时抛出“文件不可用”的异常。两个方法传的par_downloadUrl参数都是对的。谁能帮忙解决一下,先谢谢了。
Public Sub Download(ByVal par_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(par_downloadUrl)
downloadRequest.Credentials = New NetworkCredential("", "")
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)
Else
fileStream = File.Create("C://" & fileName)
Dim buffer(GetFileSize(par_downloadUrl)) 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
Catch ex As UriFormatException
MessageBox.Show(ex.Message)
Catch ex As WebException
MessageBox.Show("1" & ex.Message)
Catch ex As IOException
MessageBox.Show(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
------解决方案--------------------
试试加上downloadRequest.keepalive = false这句。
我写了两个方法a和b,先后调用Download方法,a方法先执行,b方法后执行,每次b方法调用Download方法时抛出“文件不可用”的异常。两个方法传的par_downloadUrl参数都是对的。谁能帮忙解决一下,先谢谢了。
Public Sub Download(ByVal par_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(par_downloadUrl)
downloadRequest.Credentials = New NetworkCredential("", "")
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)
Else
fileStream = File.Create("C://" & fileName)
Dim buffer(GetFileSize(par_downloadUrl)) 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
Catch ex As UriFormatException
MessageBox.Show(ex.Message)
Catch ex As WebException
MessageBox.Show("1" & ex.Message)
Catch ex As IOException
MessageBox.Show(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
------解决方案--------------------
试试加上downloadRequest.keepalive = false这句。