WebRequest Class Not Working Properly!!!!

gorbit

Member
Joined
Sep 2, 2005
Messages
7
Location
Australia
Im writing a sub in one of my VB programs for downloading .exe files from an HTTP server to the local machine. But when ever the file is downloaded it appears to be corrupted because no icon shows on the file and it wont run. The same code works fine for ordinary HTML pages but not exes it seems. Can anyone help with this problem? - heres the sub:

Code:
        Dim v_base As String = "http://www.talbotech.com/"

        Public Sub GetFile(ByVal uri As String, ByVal toFile As String)
            Dim wreq As Net.HttpWebRequest = Net.WebRequest.CreateDefault(New Uri(v_base & uri))
            wreq.AllowAutoRedirect = True
            wreq.Proxy = Net.WebProxy.GetDefaultProxy
            Dim resI As Net.HttpWebResponse = wreq.GetResponse
            Dim wres As IO.StreamReader = New IO.StreamReader(resI.GetResponseStream, System.Text.Encoding.Default)
            Dim buff(resI.ContentLength - 1) As Char
            wres.Read(buff, 0, buff.Length)
            Dim fileIO As IO.StreamWriter = New IO.StreamWriter(IO.File.Open(toFile, IO.FileMode.Create), System.Text.Encoding.Default)
            fileIO.Write(buff, 0, buff.Length)
            fileIO.Close()
            wres.Close()
            resI.Close()
        End Sub
 
Executables are not text

The problem is almost certainly to do with the fact that youre treating the response as text. I suggest you try reading directly from the Stream rather than using a StreamReader:

Code:
Dim stm As Stream
Dim buff As Byte()

Read from the stream
stm = resI.GetResponseStream()
ReDim buff(stm.Length - 1)
stm.Read(buff, 0, buff.Length)
stm.Close()

Write to the file
stm = New FileStream(toFile, FileMode.Create)
stm.Write(buff, 0, buff.Length)
stm.Close()

Good luck :)
 
It didnt work, mate. I got rid of "fileIO" and just used "stm" as an io.stream an i still got the same result. cant run the file. i know the problems not with the server either - itll work if i use internet explorer to download it. the file on the server and the one downloaded using the sub are exactly the same size and even when i open the two in notepad, i see the same gibberish in each. i have no idea what is going on.

heres the new sub

Code:
        Public Sub GetFile(ByVal uri As String, ByVal toFile As String)
            Dim wreq As Net.HttpWebRequest = Net.WebRequest.CreateDefault(New Uri(v_base & uri))
            wreq.AllowAutoRedirect = True
            wreq.Proxy = Net.WebProxy.GetDefaultProxy
            Dim resI As Net.HttpWebResponse = wreq.GetResponse
            Dim stm As IO.Stream = resI.GetResponseStream
            Dim buff(resI.ContentLength - 1) As Byte
            stm.Read(buff, 0, buff.Length)
            stm.Close()
            stm = IO.File.Open(toFile, IO.FileMode.Create)
            stm.Write(buff, 0, buff.Length)
            stm.Close()
            resI.Close()
        End Sub
 
there are a couple of examples on downloading files in the codebank, my example was used to download an image, ive simpley replaced the image name with an exe ( in this case windows messenger live on microsofts site )
with it i downloaded the complete exe which you can launch.
Code:
    Private Sub download()
        Try
            Dim exepath As String = "[URL]http://download.microsoft.com/download/1/A/4/1A4FEB1A-18E0-423A-B898-F697402E4F7F/Install_Messenger_nous.exe[/URL]" /// "[URL]http://g.msn.com/8reen_us/EN_NOUS/INSTALL_MSN_MESSENGER_DL.EXE[/URL]"
            Dim wRequest As HttpWebRequest = DirectCast(HttpWebRequest.Create(exepath), HttpWebRequest)
            Dim wResponse As WebResponse = DirectCast(wRequest.GetResponse(), WebResponse)
            Dim sWriter As FileStream = New FileStream("c:\msgr.exe", FileMode.OpenOrCreate)
            Dim ContentLength As Integer = wResponse.ContentLength
            Dim sizeInKB As String = (ContentLength / 1024).ToString()
            Me.Text = "The Size of the file is: " & sizeInKB.Substring(0, sizeInKB.IndexOf(".") + 3) & "KB"
            Dim buffer(ContentLength) As Byte
            Dim length As Integer = ContentLength
            Dim position As Integer = 0
            Dim complete As Integer = 1
            Dim returned As Integer = 0
            ProgressBar1.Value = 0 /// clear the progressbar.
            ProgressBar1.Maximum = ContentLength /// set its length.
            While Not complete = 0
                position = wResponse.GetResponseStream().Read(buffer, returned, length)
                sWriter.Write(buffer, returned, position)
                complete = position
                returned += position
                length -= position
                ProgressBar1.Step = returned
                ProgressBar1.PerformStep()
            End While
            Me.Text = "Completed download"
            sWriter.Close()
            wRequest = Nothing
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        Catch webex As WebException
            Console.WriteLine(webex.Message)
        End Try
    End Sub
 
Back
Top