Member Avatar for Member #13457

Hi guys,

as per title above,
im at my wits end after a week of thoughts....
i want to download a file from a SSL web to my local(C:\)

e.g

Scenario:
from the web browser(Internet Explorer), i required to login to the site in order to download the file.

in the vb program i pass in this url to download the file (include my password and username):
https://www.abc.com?Login&password=pass&username=usr&redirectto=File/myfile.txt
but what has been downloaded to my C:\ is the login page(in myfile.txt)

but when i use the url on the web browser(https://www.abc.com?Login&password=pass&username=usr&redirectto=File/myfile.txt
)
i managed to get the file (the actual file).

Question:
is there anyway that i can download the file from a login page?
do you get what i mean?

Thanks

Dani AI

Generated

Good catch by @kohkohkoh — the missing piece is the cookie session. was correct to point at the WebRequest stack, but you must capture the server session cookie on the login response and reuse it when requesting the file (attach a CookieContainer to your requests). (learn.microsoft.com)

A minimal VB.NET flow (login POST, then GET the file reusing cookies):

Dim cookies As New CookieContainer()

' 1) Login (POST credentials, capture cookies)
Dim loginReq = CType(WebRequest.Create("https://www.abc.com/Login"), HttpWebRequest)
loginReq.Method = "POST"
loginReq.ContentType = "application/x-www-form-urlencoded"
loginReq.CookieContainer = cookies
Dim post = System.Text.Encoding.UTF8.GetBytes("username=usr&password=pass&redirectto=File/myfile.txt")
Using rs = loginReq.GetRequestStream()
    rs.Write(post, 0, post.Length)
End Using
Using loginResp = CType(loginReq.GetResponse(), HttpWebResponse)
    ' check loginResp.StatusCode or loginResp.Cookies if needed
End Using

' 2) Request the protected file with the same CookieContainer
Dim fileReq = CType(WebRequest.Create("https://www.abc.com/File/myfile.txt"), HttpWebRequest)
fileReq.CookieContainer = cookies
Using fileResp = CType(fileReq.GetResponse(), HttpWebResponse)
    Using fs = IO.File.Create("C:\myfile.txt")
        fileResp.GetResponseStream().CopyTo(fs)
    End Using
End Using

If the GET still returns the login HTML, inspect the login response for Set-Cookie headers or missing fields (some sites require a CSRF token or extra form fields; check the login POST exactly as the browser sends it). You may also need to allow automatic redirects when the server issues a 3xx to the file URL. (learn.microsoft.com)

For modern servers be mindful of TLS defaults; prefer letting the OS choose the protocol or follow Microsoft guidance rather than hardcoding SecurityProtocol. For new development consider using HttpClient with a SocketsHttpHandler and a CookieContainer for cleaner async code. (learn.microsoft.com)

Security note: avoid hardcoding credentials and store secrets securely.

Recommended Answers

All 4 Replies

Member Avatar for Member #13457

>i want to download a file from a SSL web to my local(C:\)
Think about WebRequest class. Read sknake's post - http://www.daniweb.com/forums/thread203253.html

Thanks for your reply :D
but im using webrequest class as welll....
but it doesnt seems working....
i compared the code as described and my code..
about the same.
Is there something need to be done on the login session?

anyway, here is my code,
thanks :D

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
        Dim request

        request = WebRequest.Create(sRemoteUrl)
        wr = CType(request, 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()

>Is there something need to be done on the login session?

Read MSDN FAQ.

Member Avatar for Member #13457

Thanks for your guys' url/article.
The only missing is the cookie session.
Really appreciate your guy's assistance :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.