loli
loli

Reputation: 1068

WebBrowser control in ASP doesn't work when published

I have an ASP page with a WebBrowser control that goes on a website, logs into that website and gets redirected to one of my pages that updates the database. When I run it on my machine everything works fine, but when I publish my website, code is executing but the database is not updated (I can see the Thread doing the sleep). I'm not sure if a redirection is done though. Here is the code :

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim thread As New Thread(Sub()
                                 Using browser As New WebBrowser()
                                     browser.AllowNavigation = True
                                     browser.Navigate("https://www.website.com?redirect_uri=http://mywebsite.com/myPage.aspx")
                                     AddHandler browser.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf DocumentCompleted)
                                     While browser.ReadyState <> WebBrowserReadyState.Complete
                                         System.Windows.Forms.Application.DoEvents()
                                     End While
                                 End Using
                             End Sub)
    thread.SetApartmentState(ApartmentState.STA)
    thread.Start()
    thread.Join()
End Sub
Private Sub DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs)
        Dim browser As WebBrowser = TryCast(sender, WebBrowser)
    browser.Document.GetElementById("username").SetAttribute("value", "[email protected]")
    browser.Document.GetElementById("password").SetAttribute("value", "password")
    browser.Document.All.GetElementsByName("authorize").Item(0).InvokeMember("click")
    Thread.Sleep(6000)
End Sub

End Class

Upvotes: 0

Views: 311

Answers (1)

Joel Coehoorn
Joel Coehoorn

Reputation: 415600

When the http request for the page is finished, your thread is killed.

Don't use a WebBrowser control for this. Use the System.Net.WebClient object, or the System.Net.HttpWebRequest/HttpWebResponse objects instead.

Upvotes: 1

Related Questions