Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

System.Net.WebException

Status
Not open for further replies.

tuzojazz

Programmer
Dec 26, 2005
58
MX
Hi:

I'm writing an application which downolads a file from a remote server by FTP

this is my code

______________________________________________________________________________________

Dim ftpURI As String = "ftp://205.134.48.217/"
Try
Dim filename As String = ftpURI & "Digital/sounds/SOUND0608191550.WAV"
Dim client As New WebClient()
client.Credentials = New NetworkCredential("user1", "password1")
My.Computer.FileSystem.WriteAllBytes("C:\hello\SOUND0608191550.WAV", client.DownloadData(filename), False)
MsgBox("File downloaded!")
Catch ex As Exception
MsgBox(ex.ToString)
End Try

______________________________________________________________________________________

I get an "System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive" most of times at client.DownloadData function but other times it works well.

The size of SOUND0608191550.WAV is 5 MB.

When I try to download a smaller file (1 KB) from the remote server the application works successfully.

When I try to download SOUND0608191550.WAV from a local server the application works successfully too.

What could be wrong?

I suppose the problem is the bandwidth and/or the time out and this is the reason why the application sometimes works and other does not.

do I need to setup something on the server or my PC?

Thanks!!
 
tuzojazz,

This is due to a known .Net issue where the connection is closed before the request is complete. Essentially requests are made by opening a connection, making a number of them then closing it. In this occurrence, the connection times out but is not recognized as closed and thus a new connection is not established for the next request and an error is returned. Try chaning your code to
Code:
Dim ftpURI As String = "ftp://205.134.48.217/"
 Try
     Dim filename As String = ftpURI & "Digital/sounds/SOUND0608191550.WAV"
     Dim client As New WebClient()
     client.Credentials = New NetworkCredential("user1", "password1")
     client.KeepAlive = false
     client.ProtocolVersion=HttpVersion.Version10
      My.Computer.FileSystem.WriteAllBytes("C:\hello\SOUND0608191550.WAV",  client.DownloadData(filename), False)
     MsgBox("File downloaded!")
 Catch ex As Exception
     MsgBox(ex.ToString)
 End Try
and see if that solves your issue.

Senior Qik III, ASP.Net, VB.Net ,SQL Programmer

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SELECT * FROM Users WHERE clue > 0
0 Rows Returned

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Thanks PsychoCoder but I get 2 errors

'KeepAlive' is not a member of 'System.Net.WebClient'
'ProtocolVersion' is not a member of 'System.Net.WebClient'

What could be wrong?
 
tuzojazz,

What version of .Net are you using because those 2 are members of 2.0

Senior Qik III, ASP.Net, VB.Net ,SQL Programmer

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SELECT * FROM Users WHERE clue > 0
0 Rows Returned

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top