Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Downloading a file from a secure site

Posted on 2004-10-29
Medium Priority
Last Modified: 2008-08-07
I have to automate download of files. The files are stored on a secured web site.

I normally used this method I wrote a year ago and don't have any problems:
    Public Function DownloadFileSecureURL(ByVal pstrURL As String, _
                                     ByVal pstrFilename As String, _
                                     ByVal pstrOutputPath As String, _
                                     ByVal pstrUID As String, _
                                     ByVal pstrPWD As String, _
                                     ByRef pexcError As Exception) As Boolean
        Dim wcWebClient As WebClient = New WebClient()

        'Provide username/password to credentials and assign credentials to our WebClient object
        Dim ncCred As NetworkCredential = New NetworkCredential(pstrUID, pstrPWD)

        wcWebClient.Credentials = ncCred

            wcWebClient.DownloadFile(pstrURL + pstrFilename, pstrOutputPath + pstrFilename)
            Return True
        Catch ex As Exception
            pexcError = ex
            DisplayStatus("DownloadFileSecureURL : " + ex.ToString, 1)
            Return False
        End Try
    End Function

This method is using Credentials to pass UID and Password.

I now have to download from a new site which requires me to select a check box in addition to providing UID and PWD. I have tried with my previous method, and it is not working.

I am able to use a webbrowser in an application and pass through this screen.

How do I provide required credentials in this case?
How can I use the WebBrowser to download a file without any user intervention?

This site is: https://bearprime.com/portal/home.do
Question by:Éric Moreau
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
LVL 18

Expert Comment

ID: 12460903
Use the webBrowser control to POST data to the page which is the Form Target, after filling the data in username and password. This way you will reach next page. Leaving the login page
see this link for posting data from WebBrowser control without filling it by the user

Now the cookie is set and you can download any file

If the File path is dynmaically changing, then you have to find the path of File, otherwise just hardcode the file path and it will be downloaded.

You can save the cookies and send them again and again as well leaving the authentication page as well and directly downloading the files

see this link for sending the cookies by adding it to header.
string sCookies = "MyCookie=MyCookieValue";

   object o         = null;
   object oHeader    = "Cookie: " + sCookies + "\r\nMyItem: YES\r\n";

   browser.Navigate(sURL, ref o, ref o, ref o, ref m_oHeader);

LVL 70

Author Comment

by:Éric Moreau
ID: 12462519
armoghan, I don't have problem being authenticated using the WebBrowser control. My problem with the WebBrowser control is to download the file without user intervention. Is it possible?
LVL 70

Author Comment

by:Éric Moreau
ID: 12674388
I finally found it.

I had to pass credentials into the http header. Here is some code to do that:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim req As WebRequest = WebRequest.Create("YOurURLHere")
            Dim cre As String = String.Format("{0}:{1}", "YourUIDHere", "YourPWDHere")
            Dim bytes() As Byte = Encoding.ASCII.GetBytes(cre)
            Dim base64 As String = Convert.ToBase64String(bytes)

            req.Headers.Add("Authorization", "basic " + base64)

            Dim resp As WebResponse = req.GetResponse()

            ' Call method 'GetResponseStream' to obtain stream associated with the response object
            Dim ReceiveStream As Stream = resp.GetResponseStream()

            Dim encode As Encoding = System.Text.Encoding.GetEncoding("utf-8")

            ' Pipe the stream to a higher level stream reader with the required encoding format.
            Dim readStream As New StreamReader(ReceiveStream, encode)
            Dim read(256) As [Char]

            ' Read 256 charcters at a time    .
            Dim count As Integer = readStream.Read(read, 0, 256)
            While count > 0

                ' Dump the 256 characters on a string and display the string onto the console.
                Dim str As New [String](read, 0, count)
                count = readStream.Read(read, 0, 256)

            End While
            ' Release the resources of stream object.


        Catch ex As Exception
            TextBox1.Text = ex.ToString
        End Try

    End Sub
LVL 70

Author Comment

by:Éric Moreau
ID: 12674395
I will ask for a PAQ/Refund.

Accepted Solution

CetusMOD earned 0 total points
ID: 12694815
Question PAQ'd
500 points refunded.

Community Support Moderator

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question