Solved

Downloading a file from a secure site

Posted on 2004-10-29
2,313 Views
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

        Try
            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?
-OR-
How can I use the WebBrowser to download a file without any user intervention?

This site is: https://bearprime.com/portal/home.do
0
Question by:Éric Moreau
    5 Comments
     
    LVL 18

    Expert Comment

    by:armoghan
    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
    http://support.microsoft.com/default.aspx?scid=kb;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);

    ref
    http://www.devdex.com/vb/message.asp?p=1120&r=4555642
    0
     
    LVL 69

    Author Comment

    by:Éric Moreau
    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?
    0
     
    LVL 69

    Author Comment

    by:Éric Moreau
    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
            Try
                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]

                TextBox1.Clear()
                ' 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)
                    TextBox1.AppendText(str)
                    count = readStream.Read(read, 0, 256)

                End While
     
                ' Release the resources of stream object.
                readStream.Close()

                resp.Close()

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

        End Sub
    0
     
    LVL 69

    Author Comment

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

    Accepted Solution

    by:
    Question PAQ'd
    500 points refunded.

    CetusMOD
    Community Support Moderator
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Course: From Zero to Hero with Nodejs & MongoDB

    Interested in Node.js, but don't know where to start or how to learn it properly? Confused about how the MEAN stack pieces of MongoDB, Expressjs, Angularjs, and Nodejs fit together? Or how it's even possible to run JavaScript outside of the browser?

    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This video discusses moving either the default database or any database to a new volume.

    875 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now