Results 1 to 8 of 8

Thread: Managing a IE Download in the background

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2006
    Posts
    293

    Managing a IE Download in the background

    Hi All

    (me again!) lol

    Anyway, this is where I have got to, and now I'm stuck.

    I use a webbrowser to navigate to a site and log in. The site that I navigate to is actually the link to the download but you have got to login first and once you click submit on the login form it then starts the download.

    So where I get to is logging in, but I get the IE File download dialog "Open, Save or Cancel".

    What I want to do is to be able to download and save this file in the directory of the application all in the background, without the user having to interact with anything.

    The file is always a excel file (.xls)

    vb.net Code:
    1. Private Sub btnDownload_Click(sender As System.Object, e As System.EventArgs) Handles btnDownload.Click
    2.         Dim Tday, TMonth, TYear, TDate As String
    3.         Tday = Date.Now.Day
    4.         TMonth = Date.Now.Month
    5.         TYear = Date.Now.Year
    6.         Tday.Replace("0", "")
    7.         TMonth.Replace("0", "")
    8.         TDate = (Tday & "." & TMonth & "." & TYear)
    9.         Webaddress = (Website Address)
    10.         WebBrowser1.Navigate(Webaddress)
    11.         Timer1.Start()
    12.     End Sub
    13.  
    14.     Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    15.         Do Until WebBrowser1.ReadyState = WebBrowserReadyState.Complete
    16.             Application.DoEvents()
    17.         Loop
    18.         If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
    19.             btnlogin.PerformClick()
    20.             Timer1.Stop()
    21.         End If
    22.     End Sub
    23.  
    24.     Private Sub btnlogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlogin.Click
    25.         WebBrowser1.Document.All("ssousername").InnerText = "Username"
    26.         WebBrowser1.Document.All("password").InnerText = "Password"
    27.         Dim theElementCollection As HtmlElementCollection
    28.         theElementCollection = WebBrowser1.Document.GetElementsByTagName("input")
    29.         For Each curElement As HtmlElement In theElementCollection
    30.             If curElement.GetAttribute("value").Equals("Login") Then
    31.                 curElement.InvokeMember("click")
    32.             End If
    33.         Next
    34. End Sub

    If anyone could shed some light on this.

    Mega Thanks in Advance

    Alex

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Managing a IE Download in the background

    Don't download using the click. Use a WebClient, http://msdn.microsoft.com/en-us/library/ez801hhe.aspx

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2006
    Posts
    293

    Re: Managing a IE Download in the background

    Quote Originally Posted by dunfiddlin View Post
    Don't download using the click. Use a WebClient, http://msdn.microsoft.com/en-us/library/ez801hhe.aspx
    Ok so I used the code on there, but it has downloaded the sign in page. any help on this?

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Managing a IE Download in the background

    You still need to log in. Very often the log in page and the home page have the same address. You also need the complete filename for the download.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2006
    Posts
    293

    Re: Managing a IE Download in the background

    Quote Originally Posted by dunfiddlin View Post
    You still need to log in. Very often the log in page and the home page have the same address. You also need the complete filename for the download.
    how do I go about logging in.

    the current code downloads the sign in page and inserts the filename into the username box. The url I link to is the file but you have to sign in first. as soon as you hit the login button it generates and downloads the report.

    vb.net Code:
    1. Dim Tday, TMonth, TYear, TDate As String
    2.         Tday = Date.Now.Day
    3.         TMonth = Date.Now.Month
    4.         TYear = Date.Now.Year
    5.         Tday.Replace("0", "")
    6.         TMonth.Replace("0", "")
    7.         TDate = (Tday & "." & TMonth & "." & TYear)
    8.         Webaddress1 = ("THE URL")
    9.         Dim remoteUri As String = Webaddress1
    10.         Dim fileName As String = "00-3052.xls"
    11.         Dim myStringWebResource As String = Nothing
    12.         Dim myWebClient As New WebClient()
    13.         myStringWebResource = remoteUri + fileName
    14.         myWebClient.DownloadFile(myStringWebResource, fileName)

    The url (ive replaced sensitive info)
    "http://XXXX.com:PORT/reports/00-3052.jsp?SOME_OTHER_INFO_HERE&P_FROM_DATE=" & TDate & "&P_TO_DATE=" & TDate)
    The Sign in page consists of
    Username textbox. Html name = ssousername
    Password textbox. html name = password
    submit button. html name = login

    it will ask for the username and password every time, even if you have signed in once and then go to it.
    Last edited by crampz; Sep 2nd, 2012 at 07:53 AM.

  6. #6
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Managing a IE Download in the background

    Ah, right. That could be a problem. I'm sure you used to be able to override the dialog in Internet Options but if that ever was the case it certainly isn't now. There's probably a registry entry that will do it but the truble with that, of course, is that you'd override the dialog for everything, which may not be desirable. The only thing left would be to capture the dialog and press 'save' automatically. I'll have a little play and see if that's possible.

    In the meantime do I take the removal of 'sensitive' information as an indication that you have a direct relationship with the server? In that case you might ask whether there is an API or at least an alternative log in method that would obviate the problem directly.
    Last edited by dunfiddlin; Sep 2nd, 2012 at 10:07 AM.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2006
    Posts
    293

    Re: Managing a IE Download in the background

    Quote Originally Posted by dunfiddlin View Post
    Ah, right. That could be a problem. I'm sure you used to be able to override the dialog in Internet Options but if that ever was the case it certainly isn't now. There's probably a registry entry that will do it but the truble with that, of course, is that you'd override the dialog for everything, which may not be desirable. The only thing left would be to capture the dialog and press 'save' automatically. I'll have a little play and see if that's possible.

    In the meantime do I take the removal of 'sensitive' information as an indication that you have a direct relationship with the server? In that case you might ask whether there is an API or at least an alternative log in method that would obviate the problem directly.
    ahh ok I see. Just replying on mobile atm. Thank you for your help in advance. I just removed information as it just contained company name and some parameters that had info on it. I don't have direct access to it. Just access through our companies reporting tools.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2006
    Posts
    293

    Re: Managing a IE Download in the background

    Ok Question, Can I pass request headers to the webbrowser. I captured data of me downloading the file. so I have the capture information of the login and then of the download. I don't know if I can do anything with this

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width