ASPNET and Webbrowser control

D

Daniel Frede

Hello,


I Created a AxSHDocVw.AxWebBrowser in a class and want to Post Values
to a WebPage. When I use this class in a Windows application,
everything is fine, but in an ASPX page it hangs after the .submit
(see code)

Even if i try to start the class in a new thread it terminates at the
..submit

Any ideas?

-----------------
Imports mshtml

Public Class cEbay

'Inherits System.Windows.Forms.Form

Private m_iStatus As Integer
Private m_sUserID As String
Private m_sPassword As String
Private m_sReturnString As String
Private m_bTransactionFinished As Boolean
Private m_bPassString As String
Private m_bTimerFinished As Boolean

Const sURL_SignIn As String =
"http://cgi.ebay.de/aw-cgi/eBayISAPI.dll?SignIn"
Const sURL_UserData As String =
"http://cgi4.ebay.de/aw-cgi/eBayISAP...SSL=0&countryid=0&pass=[pass]&userid=[userid]"
Const sURL_LogOut =
"http://signin.ebay.de/aw-cgi/eBayISAPI.dll?SignIn&ssPageName=h:h:sout:DE"
Const sURL_MyEbay =
"http://cgi1.ebay.de/aw-cgi/eBayISAP...ngOn&userid=[userid]&pass=[pass]&dayssince=20"

Private iTimeoutSec As Integer = 15
Private components As System.ComponentModel.IContainer
Friend WithEvents WebBrowser1 As AxSHDocVw.AxWebBrowser

Public Sub New()
MyBase.New()
m_iStatus = 0
'WebBrowser1.Show()

Me.components = New System.ComponentModel.Container
Me.WebBrowser1 = New AxSHDocVw.AxWebBrowser
Me.WebBrowser1.Enabled = True
Dim resources As System.Resources.ResourceManager = New
System.Resources.ResourceManager(GetType(cEbay))
Me.WebBrowser1.OcxState =
CType(resources.GetObject("AxWebBrowser1.OcxState"),
System.Windows.Forms.AxHost.State)
Dim frm As New System.Windows.Forms.Form
frm.Controls.Add(Me.WebBrowser1)
CType(Me.WebBrowser1,
System.ComponentModel.ISupportInitialize).EndInit()

WebBrowser1.Navigate("about:blank")

End Sub

#Region "Properties"

'TimeOutSec
Public Property TimeOutSec() As Integer
Get
TimeOutSec = iTimeoutSec
End Get
Set(ByVal Value As Integer)
iTimeoutSec = Value
End Set
End Property

'UserID
Public Property EBAY_UserID() As String
Get
EBAY_UserID = m_sUserID
End Get
Set(ByVal Value As String)
m_sUserID = Value
End Set
End Property

'Password
Public Property EBAY_Password() As String
Get
EBAY_Password = m_sPassword
End Get
Set(ByVal Value As String)
m_sPassword = Value
End Set
End Property


'Address
Public Property EBAY_Address() As String
Get
EBAY_Address = m_sAddress
End Get
Set(ByVal Value As String)
m_sAddress = Value
End Set
End Property
#End Region


Private Sub WebBrowser1_DocumentComplete(ByVal sender As Object,
ByVal e As AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent) Handles
WebBrowser1.DocumentComplete

'Print WebBrowser1.Document.body.innerhtml
Try
Select Case m_iStatus


'-------------------------------------------------------------GetEbayAddress
'-------------------------------------------------------------GetEbayAddress
'-------------------------------------------------------------GetEbayAddress
Case 10
WebBrowser1.Navigate(sURL_SignIn)
Case 11

WebBrowser1.Navigate(sURL_UserData)
Case 12


'Login
WebBrowser1.Document.Forms("SignInForm").UserID.value
= m_sUserID
WebBrowser1.Document.Forms("SignInForm").pass.value
= m_sPassword
WebBrowser1.Document.Forms("SignInForm").submit()
<<<------Here
it freezes, but not in a Window Application! What can I do??? Any
Suggestions?

'For Each frm As mshtml.HTMLFormElementClass In
WebBrowser1.Document.Forms
' If frm.Name = "SignInForm" Then

' frm.elements.UserID.Value = m_sUserID
' frm.elements.pass.Value = m_sPassword
' frm.elements.keepMeSignInOption.Value = 1
' frm.setActive()
' frm.Submit()

' End If
'Next

Case 13

End Select

m_iStatus += 1
Catch ex As Exception
End Try

End Sub



Function GetEbayAddress() As String
Try
m_iStatus = 10
m_sAddress = ""
m_bBidFailed = True
m_bTransactionFinished = False
m_sAddress = ""
WebBrowser1.Navigate(sURL_LogOut)

Dim myTimer As System.Timers.Timer
myTimer = New System.Timers.Timer
myTimer.AutoReset = True
myTimer.Interval = iTimeoutSec * 1000
AddHandler myTimer.Elapsed, New
System.Timers.ElapsedEventHandler(AddressOf ElapsedEventHandler)
m_bTimerFinished = False
myTimer.Start()

Do
If m_bTimerFinished Or m_bTransactionFinished Then
Exit Do
System.Windows.Forms.Application.DoEvents()
Loop

GetEbayAddress = m_sAddress

Catch ex As Exception
End Try
End Function

Sub ElapsedEventHandler(ByVal sender As Object, ByVal e As
System.Timers.ElapsedEventArgs)
m_bTimerFinished = True
End Sub


'----------------- The Starter
Public Function GetAddress(ByVal sPassword As String, ByVal
sUserID As String) As String
Dim sResult As String

Dim oEbayThread As New Thread(AddressOf thread_GetEbayAddress)
oEbayThread.IsBackground = False
oEbayThread.ApartmentState = Threading.ApartmentState.STA
oEbayThread.Start()
oEbayThread.Join()

GetEbayAddress = m_sEbayAddress
End Function

Private Sub thread_GetEbayAddress()
Dim ebay As New cEbay
ebay.EBAY_Password = "password"
ebay.EBAY_UserID = "user"
m_sEbayAddress = ebay.GetEbayAddress()
End Sub
 
K

Ken Cox [Microsoft MVP]

Hi Daniel,

In ASP.NET, I think you'd be much better of with the Webclient class to do
this kind of thing:

Provides common methods for sending data to and receiving data from a
resource identified by a URI.

http://msdn.microsoft.com/library/d...ef/html/frlrfSystemNetWebClientClassTopic.asp


HOWTO: Use WebClient Class To Make HTTP Requests

http://support.microsoft.com/default.aspx?scid=kb;en-us;328820

Ken
Microsoft MVP [ASP.NET]


Daniel Frede said:
Hello,


I Created a AxSHDocVw.AxWebBrowser in a class and want to Post Values
to a WebPage. When I use this class in a Windows application,
everything is fine, but in an ASPX page it hangs after the .submit
(see code)

Even if i try to start the class in a new thread it terminates at the
.submit

Any ideas?

-----------------
Imports mshtml

Public Class cEbay

'Inherits System.Windows.Forms.Form

Private m_iStatus As Integer
Private m_sUserID As String
Private m_sPassword As String
Private m_sReturnString As String
Private m_bTransactionFinished As Boolean
Private m_bPassString As String
Private m_bTimerFinished As Boolean

Const sURL_SignIn As String =
"http://cgi.ebay.de/aw-cgi/eBayISAPI.dll?SignIn"
Const sURL_UserData As String =
"http://cgi4.ebay.de/aw-cgi/eBayISAP...SSL=0&countryid=0&pass=[pass]&userid=[userid]"
Const sURL_LogOut =
"http://signin.ebay.de/aw-cgi/eBayISAPI.dll?SignIn&ssPageName=h:h:sout:DE"
Const sURL_MyEbay =
"http://cgi1.ebay.de/aw-cgi/eBayISAP...ngOn&userid=[userid]&pass=[pass]&dayssince=20"

Private iTimeoutSec As Integer = 15
Private components As System.ComponentModel.IContainer
Friend WithEvents WebBrowser1 As AxSHDocVw.AxWebBrowser

Public Sub New()
MyBase.New()
m_iStatus = 0
'WebBrowser1.Show()

Me.components = New System.ComponentModel.Container
Me.WebBrowser1 = New AxSHDocVw.AxWebBrowser
Me.WebBrowser1.Enabled = True
Dim resources As System.Resources.ResourceManager = New
System.Resources.ResourceManager(GetType(cEbay))
Me.WebBrowser1.OcxState =
CType(resources.GetObject("AxWebBrowser1.OcxState"),
System.Windows.Forms.AxHost.State)
Dim frm As New System.Windows.Forms.Form
frm.Controls.Add(Me.WebBrowser1)
CType(Me.WebBrowser1,
System.ComponentModel.ISupportInitialize).EndInit()

WebBrowser1.Navigate("about:blank")

End Sub

#Region "Properties"

'TimeOutSec
Public Property TimeOutSec() As Integer
Get
TimeOutSec = iTimeoutSec
End Get
Set(ByVal Value As Integer)
iTimeoutSec = Value
End Set
End Property

'UserID
Public Property EBAY_UserID() As String
Get
EBAY_UserID = m_sUserID
End Get
Set(ByVal Value As String)
m_sUserID = Value
End Set
End Property

'Password
Public Property EBAY_Password() As String
Get
EBAY_Password = m_sPassword
End Get
Set(ByVal Value As String)
m_sPassword = Value
End Set
End Property


'Address
Public Property EBAY_Address() As String
Get
EBAY_Address = m_sAddress
End Get
Set(ByVal Value As String)
m_sAddress = Value
End Set
End Property
#End Region


Private Sub WebBrowser1_DocumentComplete(ByVal sender As Object,
ByVal e As AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent) Handles
WebBrowser1.DocumentComplete

'Print WebBrowser1.Document.body.innerhtml
Try
Select Case m_iStatus



'-------------------------------------------------------------GetEbayAddress

'-------------------------------------------------------------GetEbayAddress

'-------------------------------------------------------------GetEbayAddress
Case 10
WebBrowser1.Navigate(sURL_SignIn)
Case 11

WebBrowser1.Navigate(sURL_UserData)
Case 12


'Login
WebBrowser1.Document.Forms("SignInForm").UserID.value
= m_sUserID
WebBrowser1.Document.Forms("SignInForm").pass.value
= m_sPassword
WebBrowser1.Document.Forms("SignInForm").submit()
<<<------Here
it freezes, but not in a Window Application! What can I do??? Any
Suggestions?

'For Each frm As mshtml.HTMLFormElementClass In
WebBrowser1.Document.Forms
' If frm.Name = "SignInForm" Then

' frm.elements.UserID.Value = m_sUserID
' frm.elements.pass.Value = m_sPassword
' frm.elements.keepMeSignInOption.Value = 1
' frm.setActive()
' frm.Submit()

' End If
'Next

Case 13

End Select

m_iStatus += 1
Catch ex As Exception
End Try

End Sub



Function GetEbayAddress() As String
Try
m_iStatus = 10
m_sAddress = ""
m_bBidFailed = True
m_bTransactionFinished = False
m_sAddress = ""
WebBrowser1.Navigate(sURL_LogOut)

Dim myTimer As System.Timers.Timer
myTimer = New System.Timers.Timer
myTimer.AutoReset = True
myTimer.Interval = iTimeoutSec * 1000
AddHandler myTimer.Elapsed, New
System.Timers.ElapsedEventHandler(AddressOf ElapsedEventHandler)
m_bTimerFinished = False
myTimer.Start()

Do
If m_bTimerFinished Or m_bTransactionFinished Then
Exit Do
System.Windows.Forms.Application.DoEvents()
Loop

GetEbayAddress = m_sAddress

Catch ex As Exception
End Try
End Function

Sub ElapsedEventHandler(ByVal sender As Object, ByVal e As
System.Timers.ElapsedEventArgs)
m_bTimerFinished = True
End Sub


'----------------- The Starter
Public Function GetAddress(ByVal sPassword As String, ByVal
sUserID As String) As String
Dim sResult As String

Dim oEbayThread As New Thread(AddressOf thread_GetEbayAddress)
oEbayThread.IsBackground = False
oEbayThread.ApartmentState = Threading.ApartmentState.STA
oEbayThread.Start()
oEbayThread.Join()

GetEbayAddress = m_sEbayAddress
End Function

Private Sub thread_GetEbayAddress()
Dim ebay As New cEbay
ebay.EBAY_Password = "password"
ebay.EBAY_UserID = "user"
m_sEbayAddress = ebay.GetEbayAddress()
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top