Downloading a file from a secure site

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:
LVL 71
Éric MoreauSenior .Net ConsultantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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;EN-US;313068

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);

Éric MoreauSenior .Net ConsultantAuthor Commented:
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?
Éric MoreauSenior .Net ConsultantAuthor Commented:
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
Éric MoreauSenior .Net ConsultantAuthor Commented:
I will ask for a PAQ/Refund.
Question PAQ'd
500 points refunded.

Community Support Moderator

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.