473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

VB-APP downloading a file with webrequest HELP!!!

59
I am trying to download a file from the internet using webrequest... but most of the time i try to download a file it only downloads a small portion of the file...any help would be greatly appreciated. heres the code
The url is the url to download and the filename is the filename to save as
Expand|Select|Wrap|Line Numbers
  1. Private Sub downloadfile(ByVal url As String, ByVal filename As String)
  2.         Dim wr As Net.HttpWebRequest = Net.HttpWebRequest.Create(url)
  3.         Dim ws As Net.HttpWebResponse = CType(wr.GetResponse(), Net.HttpWebResponse)
  4.         Dim str As IO.Stream = ws.GetResponseStream()
  5.         Dim inBuf(100000000) As Byte
  6.         Dim bytesToRead As Integer = CInt(inBuf.Length)
  7.         Dim bytesRead As Integer = 0
  8.         ProgressBar1.Maximum = ws.ContentLength
  9.         While bytesToRead > 0
  10.             Application.DoEvents()
  11.             Dim n As Integer = str.Read(inBuf, bytesRead, bytesToRead)
  12.             If n = 0 Then
  13.                 Exit While
  14.             End If
  15.             bytesRead += n
  16.             bytesToRead -= n
  17.             ProgressBar1.Value = bytesRead
  18.             Label1.Text = bytesRead / 128 & "KB /" & ProgressBar1.Maximum / 128 & "KB"
  19.         End While
  20.         Dim fstr As New IO.FileStream("D:/" & filename, IO.FileMode.OpenOrCreate, IO.FileAccess.Write)
  21.         fstr.Write(inBuf, 0, bytesRead)
  22.         str.Close()
  23.         fstr.Close()
  24.     End Sub 
Please help!!!!
Nov 2 '08 #1
2 1709
joedeene
583 512MB
Why are you making it so complicated on yourself? Especially using VB.Net, you can use the My.Computer.Network.DownloadFile() Method. It's always worked for me. Also, if why not use the WebClient.DownloadFile Method, (although I think it's the same as the first method, not positive). But, if you want help using the WebRequest Class, then I'll search Google for some tutorials, and get back to you :)

joedeene
Nov 2 '08 #2
Plater
7,872 Expert 4TB
(Lost my post darn it)

You make a few logical leaps, while good ideas, are incorrect.

For instance if you read zero bytes, you exit your loop. For smaller files on a faster connection that could be correct, but for larger files or when the connection is slow, the data might not be there yet by the time you go to read the data, so you exit your loop prematurally.

Also, at the time that you set bytesToRead, not all of the data could have arrived on the buffer. the .ContentLength proeprty (that you use for your ProgressBar) should contain the correct size.
EDIT: Oops, ok you are just blanket setting your stream and bytesToRead to a large number. Well you should still be using the ContentLength

Now, Application.DoEvents() might be enough to allow processing but a Thread.Sleep(10) would also potentially be usefull to allow the background IO to catch up.
Nov 3 '08 #3

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

Similar topics

1
by: Christina Feichter | last post by:
Hello All How can i start a help-file like help.chm? Thanks
5
by: kevin | last post by:
Hi, Any help with this would be really appreciated! I'm trying to download a file from a remote server. The access permissions is okay but the problem I'm facing is that the file is getting...
3
by: Jocelyn Duhaylungsod | last post by:
I have been getting the following server errors sporadically while downloading excel, csv or xml file. Internet Explorer cannot download <file name> from <IP address> "The server returned an...
4
by: Richard L Rosenheim | last post by:
I know that I can download a file from a web server by using the WebClient.DownloadFile method. But, does anyone know of an example of downloading a file from a web server with the ability to...
7
by: Frank | last post by:
Hi, On one of my server, downloading file by clicking a button give empt file. On the other servers work perfectly. Is any idea ? What should be change in IIS Metabase setting? After click the...
2
by: Tomas Martinez | last post by:
Hi there! I'm trying to download a file in my asp.net web, but when downloading it from a Firefox browser, instead of downloading the example.exe file, it's downloading example.exe.htm. My code...
6
by: Bob | last post by:
Hi, I am new to using a web resource which is not a webservice and I am spinning my wheels with the webrequest class. I am trying to interact with a URI that has been given to me. It is say...
8
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hi all, given: HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create("myurl"); WebReq.Method = "GET"; HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();...
7
by: raids51 | last post by:
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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...

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.