• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2320
  • Last Modified:

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

        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
Éric Moreau
Asked:
Éric Moreau
  • 3
1 Solution
 
armoghanCommented:
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
 
É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?
0
 
É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
        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
 
Éric MoreauSenior .Net ConsultantAuthor Commented:
I will ask for a PAQ/Refund.
0
 
CetusMODCommented:
Question PAQ'd
500 points refunded.

CetusMOD
Community Support Moderator
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now