473,857 Members | 2,042 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

VB.NET-APP Downloading file using httpwebrequest/response

59 New Member
Hello, i have a program that downloads a file using the httpwebrequest/response, and it usually works, but sometimes it will freeze at a random part of the download without an error.
here is the code:
Expand|Select|Wrap|Line Numbers
  1.   'Creating the request and getting the response
  2.         Dim theResponse As HttpWebResponse
  3.         Dim theRequest As HttpWebRequest
  4.  
  5.         Try 'Checks if the file exist
  6.             theRequest = WebRequest.Create(Me.Filelocation)
  7.             theResponse = theRequest.GetResponse
  8.         Catch ex As Exception
  9.  
  10.             MessageBox.Show("An error occurred while downloading file. Possibe causes:" & ControlChars.CrLf & _
  11.                             "1) File doesn't exist" & ControlChars.CrLf & _
  12.                             "2) Remote server error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  13.  
  14.             Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)
  15.  
  16.             Me.Invoke(cancelDelegate, True)
  17.  
  18.             Exit Sub
  19.         End Try
  20.         theRequest.Timeout = 30000
  21.         theRequest.KeepAlive = True
  22.         Dim length As Long = theResponse.ContentLength 'Size of the response (in bytes)
  23.  
  24.         Dim safedelegate As New ChangeTextsSafe(AddressOf ChangeTexts)
  25.         Me.Invoke(safedelegate, length, 0, 0, 0) 'Invoke the TreadsafeDelegate
  26.  
  27.         Dim writeStream As New IO.FileStream(Me.whereToSave, IO.FileMode.Create)
  28.  
  29.         'Replacement for Stream.Position (webResponse stream doesn't support seek)
  30.         Dim nRead As Integer
  31.  
  32.         'To calculate the download speed
  33.         Dim speedtimer As New Stopwatch
  34.         Dim currentspeed As Double = -1
  35.         Dim readings As Integer = 0
  36.  
  37.         Do
  38.             If BackgroundWorker1.CancellationPending Then 'If user abort download
  39.                 Exit Do
  40.             End If
  41.  
  42.             speedtimer.Start()
  43.  
  44.             Dim readBytes(4095) As Byte
  45.             Dim bytesread As Integer = theResponse.GetResponseStream.Read(readBytes, 0, 4096)
  46.  
  47.             nRead += bytesread
  48.             Dim percent As Integer = (nRead / length) * 100
  49.  
  50.             Me.Invoke(safedelegate, length, nRead, percent, currentspeed)
  51.  
  52.             If bytesread = 0 Then Exit Do
  53.  
  54.             writeStream.Write(readBytes, 0, bytesread)
  55.  
  56.             speedtimer.Stop()
  57.  
  58.             readings += 1
  59.             If readings >= 5 Then 'For increase precision, the speed it's calculated only every five cicles
  60.                 currentspeed = 20480 / (speedtimer.ElapsedMilliseconds / 1000)
  61.                 speedtimer.Reset()
  62.                 readings = 0
  63.             End If
  64.         Loop
  65.  
  66.         'Close the streams
  67.         theResponse.GetResponseStream.Close()
  68.         writeStream.Close()
  69.  
  70.         If Me.BackgroundWorker1.CancellationPending Then
  71.  
  72.             IO.File.Delete(Me.whereToSave)
  73.  
  74.             Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)
  75.  
  76.             Me.Invoke(cancelDelegate, True)
  77.  
  78.             Exit Sub
  79.  
  80.         End If
  81.  
  82.         Dim completeDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)
  83.         Try
  84.             Me.Invoke(completeDelegate, False)
  85.         Catch ex As Exception
  86.             MsgBox(ex.Message)
  87.         End Try
But im pretty sure my problem is in this part of the code:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  Do
  3.  If BackgroundWorker1.CancellationPending Then 'If user abort download
  4.                 Exit Do
  5.             End If
  6.  
  7.             speedtimer.Start()
  8.  
  9.             Dim readBytes(4095) As Byte
  10.             Dim bytesread As Integer = theResponse.GetResponseStream.Read(readBytes, 0, 4096)
  11.  
  12.             nRead += bytesread
  13.             Dim percent As Integer = (nRead / length) * 100
  14.  
  15.             Me.Invoke(safedelegate, length, nRead, percent, currentspeed)
  16.  
  17.             If bytesread = 0 Then Exit Do
  18.  
  19.             writeStream.Write(readBytes, 0, bytesread)
  20.  
  21.             speedtimer.Stop()
  22.  
  23.             readings += 1
  24.             If readings >= 5 Then 'For increase precision, the speed it's calculated only every five cicles
  25.                 currentspeed = 20480 / (speedtimer.ElapsedMilliseconds / 1000)
  26.                 speedtimer.Reset()
  27.                 readings = 0
  28.             End If
  29.         Loop
Nov 9 '08 #1
7 7794
joedeene
583 Contributor
Put a Try...Catch Statement around the WHOLE code, and then in the 'Catch Ex as Exception' line, under that have a message box showing the whole error.

Example:

Expand|Select|Wrap|Line Numbers
  1.       Try
  2.         'All Code
  3.         Catch ex As Exception
  4.             MessageBox.Show(ex.ToString())
  5.         End Try
This should give you an error the error that's happening some point in the code, post back the error and line number so we can help you furthermore. :)

joedeene
Nov 9 '08 #2
raids51
59 New Member
i tried that already, but it doesnt show that anythings wrong, the whole program still works but the downloaded freezes at a random place... and it doesnt do it all the time it, its just random.
Nov 9 '08 #3
joedeene
583 Contributor
Hmm...This has occurred to me a while ago when I was working with downloading files, but since you're in VB.NET, why not use the My.Computer.Net work.DownloadFi le() Method, it's kind of the lazy way, but if it works?

joedeene
Nov 10 '08 #4
raids51
59 New Member
because i want to get the speed and progress of the download
Nov 13 '08 #5
Plater
7,872 Recognized Expert Expert
Hmm these lines:
theRequest.Time out = 30000
theRequest.Keep Alive = True
have no effect since you have already called for the response.

Your code does lot of work, is it possible it is getting stuck in a loop somewhere.
Have you set a breakpoint and stepped through your code?
Nov 13 '08 #6
raids51
59 New Member
its usually just a random point that it just decides to stop downloading, i did find a quick fix though... instead of using a backgroundworke r i just put it all in a different sub and called application.doe vents, and this seemed to fix it.
Dec 2 '08 #7
Plater
7,872 Recognized Expert Expert
That was as I thought.
You are in a busy loop somewhere trying to get the rest of the data, but the managed object doesn't know more data has come in, because it never had the chance to get its messages (the doevents() call)
Dec 2 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
4051
by: lawrence | last post by:
I haven't been able to reach www.php.net for days. Most of the rest of the web is working for me, though I've bad trouble reaching any English sites. Anyone else having trouble?
0
2003
by: Brian Loesgen | last post by:
The next San Diego .Net User Group meeting is Tuesday, November 25, 2003 at the Scripps Ranch Library. Scripps Ranch Library 10301 Scripps Lake Drive San Diego, CA 92131-1026 Please join us at 6:00 pm for pizza and networking. The meeting starts at 6:30 pm.
5
306
by: Eric Clapton | last post by:
When should I use vb.net and when I should use c#.net? What is pros and cons?
16
486
by: Terry | last post by:
Hi, I need some feedback. We are converting to .Net and we are trying to decide on whether to use VB.net or C#.net. As far as our current systems, they will probably be rewritten in ASP.Net. I have looked into both and I don't see anything that would scream out to use one or the other. So at this point, it is still a toss up. However, I want to look at the two languages from a different perspective. That is from a career point of view....
2
6479
by: pieter.breed | last post by:
Hi All, The company I work for has traditionally used COM/ActiveX for the solutions that it provides. We are in the process of moving to .NET and a few applications have been written in VB.NET up to this point. Some of the applications are class libraries which need to be callable via the COM interface from some of the older applications. Could someone please explain to me the issues involved in deploying such a class library?
3
20687
by: CMan | last post by:
Hi, We are currently trying to install .Net Framework v.1.1 on a server which already has v1.0. We are receiving the following error. Error 1704.An installation for Microsoft .NET Framework SDK (English) is currently suspended. You must undo the changes made by that installation to continue. Do you want to undo those changes?
44
4305
by: lester | last post by:
a pre-beginner's question: what is the pros and cons of .net, compared to ++ I am wondering what can I get if I continue to learn C# after I have learned C --> C++ --> C# ?? I think there must be many know the answer here. thanks
142
4328
by: Herr Lucifer | last post by:
As the founder of .NET framework, Microsoft claims that it invention will be the next best platform for programming in a near future. Now it is 2005, ..NET is 5 years old, and can talk and walk for himself with some help of his mum. However, we see the same native office applications are coming out again, and many other tools in SP2 of XP which could be in managed code....but are not. So, as the inventor of .NET , why doesn't Microsoft...
2
1472
by: Scott Lee via .NET 247 | last post by:
(Please pardon me if this is a duplicate --- not sure if messageposted) Hello, I have been working my tail off developing an ASP.NETapplication. Now that the project is nearing completion, ananti-microsoft type has complained that "much of .NET in a webserver environment is based on ASP (active server pages) whichwe forcibly disable, and which is one of our key selling pointsfrom a security standpoint as ASP has been an Achilles heel...
9
3128
by: gulu man | last post by:
Hi, What is the substitute for COM objects in .NET? How can I create something similar to com in .net? Is it still possible? Thank you
0
9923
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9767
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10394
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9549
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7939
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7104
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5774
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4190
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.