Issue with Downloading zip file using webclient

renjithmadhavan

New member
Joined
Nov 24, 2010
Messages
3
Programming Experience
Beginner
I am very new to VB.net . I am trying to use system.net to download a zip from a website . I am using the following code .
VB.NET:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
     
        Dim filename As String = TextBox1.Text
        Dim dest As String = "C:\test.zip"

        Dim name As String = "http://www.Source URL/test.zip"

        Dim wr As HttpWebRequest = CType(WebRequest.Create(name), HttpWebRequest)
        Dim ws As HttpWebResponse = CType(wr.GetResponse(), HttpWebResponse)
        Dim str As Stream = ws.GetResponseStream()
       
     [B]   Dim a As Integer = ws.ContentLength



        Dim inBuf(a) As Byte[/B]
        Dim bytesToRead As Integer = CInt(inBuf.Length)
        Dim bytesRead As Integer = 0
        While bytesToRead > 0
            Dim n As Integer = str.Read(inBuf, bytesRead, bytesToRead)
            If n = 0 Then
                Exit While
            End If
            bytesRead += n
            bytesToRead -= n
        End While

        Dim fstr As New FileStream(dest, FileMode.OpenOrCreate, FileAccess.Write)
        fstr.Write(inBuf, 0, bytesRead)
        str.Close()
        fstr.Close()
        MessageBox.Show("Downloaded Successfully")
        
    End Sub

I am doing something wrong here . The zip file is getting downloaded but the size is not correct . How to know the size of the file I download and assign to my buffer . Is there any other way to do this . Please help ..
 
Your thread title says that you're using a WebClient but your code says that you're using an HttpWebRequest. They are two quite different things. Why not just use a WebClient?
VB.NET:
Using client As New WebClient
    client.DownloadFile("http://www.Source URL/test.zip", "C:\test.zip")
End Using
 
Hi ,
Thank you for the response ...
I used webclient also ...
I am able to download .. the issue is ... say there are 3 zip files a,b,c . Zip file size a is 261 KB , ZIP file B is 1907 KB , Zip file C is 1909 KB . I do not have any issues in downloading zip files B and C , but when I download zip file A , it shows as 1908 KB . The same thing happens with both httpwebrequest and webclient .
 
Is there any particular reason that you decided to keep that information to yourself? If you want us to help you with a problem then it would help to provide all the relevant information. It's not fun for us trying to guess what you're doing or what you want or wasting our time chasing red herrings. Provide a full and clear description up front, including all relevant information.

Also, you provide an example of three ZIP files. Are these real files? Is it just that one specific file you're having issues with? If you download them in a different order, what happens? If you download different files, what happens? If you download the same files using a browser, what happens?
 
I apologize for miswording my question ...
The problem is with only one particular zip file .
Tried the download with other files like doc and other zip files all are downloading fine .
I am able to download that zip file through browser and the size is 261 KB .
But when I do that through code as mentioned earlier showing 1908 KB .
Tried different order also , but again problem with only the one single zip file .......
 
Back
Top