Computing.Net > Forums > Programming > Help with downloading a file. VB.NET

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Help with downloading a file. VB.NET

Reply to Message Icon

Name: TheEvilRoot
Date: May 9, 2009 at 16:14:29 Pacific
OS: Windows XP
Subcategory: General
Comment:

I want to download a file from website with authentification system.
Ok, maybe ill do some explanation. First of all, i open the site using webbrowser control. Then i logon, using following code:

WebBrowser1.Document.DomDocument.All("uid").Value = "User"
WebBrowser1.Document.DomDocument.All("pwd").Value = "Password"
ButtonClick(("login"), WebBrowser1) //ButtonClick is predefined function

Everything is ok, webbrowser loads the page and i see that i am logged in. Then i open another URL in same site. Still loged in. Get its source in textbox with:

Src.Text = WebBrowser1.DocumentText.ToString

Then with some InStr's i get URL to file which looks like http://site/download.php?id=ee29045... filename]. id is allways a different string. Then i try to download it with many functions from web like:

Public Sub DownFile(ByVal sFile As String, ByVal sRemoteUrl As String)
Dim wr As HttpWebRequest
Dim ws As HttpWebResponse
Dim str As Stream
Dim inBuf(1000000) As Byte
Dim bytesToRead As Integer = CInt(inBuf.Length)
Dim bytesRead As Integer = 0
wr = CType(WebRequest.Create(sRemoteUrl), HttpWebRequest)
ws = CType(wr.GetResponse(), HttpWebResponse)
str = ws.GetResponseStream()
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(sFile, FileMode.OpenOrCreate, FileAccess.Write)
fstr.Write(inBuf, 0, bytesRead)
str.Close()
fstr.Close()
End Sub

or

Public Sub GetPackage(ByVal sFile As String, ByVal destination As String, ByVal sRemoteUrl As String)
Dim wc As New Net.WebClient()
Try
wc.DownloadFile(sRemoteUrl + sFile, destination + "\\" + sFile)
Catch
MsgBox("Unable to download file " + sRemoteUrl + sFile)
End Try
End Sub

I get a file, but it actually is HTML page that says that i cant download, because i am not logged in (same as i would get if i try to open that link in any browser when not logged in). Looks like it has something to do with cookies, but i'm not sure about that. Strange, but in vb6 any function successfully downloads a file, i never saw such a problem there.



Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Help with downloading a file. VB.NET

VB.Net Compiler www.computing.net/answers/programming/vbnet-compiler/12705.html

help with autorun.ini www.computing.net/answers/programming/help-with-autorunini/18667.html

VB.Net How to alter files www.computing.net/answers/programming/vbnet-how-to-alter-files/8691.html