CDCOP
asked on
Load variable with html from webbrowser object
I need to load strVar1 with the data from a webpage using webbrowser object web1
ASKER
Cqn you give me an example?
What does the HTML look like?
Bob
Bob
ASKER
Lets say its a basic hello world line in the html.
I want to load that text, or the whole html from a webpage into a variable.
I want to load that text, or the whole html from a webpage into a variable.
ASKER
Is there anyway to get the response from a webpage? I was able to use:
Dim myHttpWebRequest2 As HttpWebRequest = CType(WebRequest.Create("http://www.cnn.com/asdasd" ), HttpWebRequest)
Dim myHttpWebResponse2 As HttpWebResponse = CType(myHttpWebRequest2.Ge tResponse( ), HttpWebResponse)
txt_1.Text = myHttpWebResponse2.StatusC ode
But it only works if its a code 200. 404 will crash at runtime.
Dim myHttpWebRequest2 As HttpWebRequest = CType(WebRequest.Create("http://www.cnn.com/asdasd"
Dim myHttpWebResponse2 As HttpWebResponse = CType(myHttpWebRequest2.Ge
txt_1.Text = myHttpWebResponse2.StatusC
But it only works if its a code 200. 404 will crash at runtime.
What .NET version?
Bob
Bob
Here is a 2003 example:
' Add a reference to MSHTML to your project.
Public Class formBrowser
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.ICon tainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Private WithEvents browserGoogle As AxSHDocVw.AxWebBrowser
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub InitializeComponent()
Dim resources As System.Resources.ResourceM anager = New System.Resources.ResourceM anager(Get Type(formB rowser))
Me.browserGoogle = New AxSHDocVw.AxWebBrowser
CType(Me.browserGoogle, System.ComponentModel.ISup portInitia lize).Begi nInit()
Me.SuspendLayout()
'
'browserGoogle
'
Me.browserGoogle.Dock = System.Windows.Forms.DockS tyle.Fill
Me.browserGoogle.Enabled = True
Me.browserGoogle.Location = New System.Drawing.Point(0, 0)
Me.browserGoogle.Size = New System.Drawing.Size(508, 423)
Me.browserGoogle.TabIndex = 0
'
'formBrowser
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(508, 423)
Me.Controls.Add(Me.browser Google)
Me.Name = "formBrowser"
Me.Text = "Google Search"
Me.WindowState = System.Windows.Forms.FormW indowState .Maximized
CType(Me.browserGoogle, System.ComponentModel.ISup portInitia lize).EndI nit()
Me.ResumeLayout(False)
End Sub
#End Region
Private m_document As mshtml.HTMLDocument
Private m_url As String = "http://www.google.com/"
Private Sub formBrowser_Activated(ByVa l sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
If Me.browserGoogle.Document Is Nothing Then
Me.browserGoogle.Navigate2 (m_url)
End If
End Sub
Private Sub browserEE_DocumentComplete (ByVal sender As System.Object, ByVal e As AxSHDocVw.DWebBrowserEvent s2_Documen tCompleteE vent) Handles browserGoogle.DocumentComp lete
If e.uRL = m_url Then
m_document = Me.browserGoogle.Document
Me.Google()
End If
End Sub
Private Sub Google()
Try
For Each input As mshtml.HTMLInputElement In m_document.getElementsByTa gName("inp ut")
If input.title = "Google Search" Then
input.value = "define: learned"
ElseIf input.name = "btnG" Then
input.click()
Exit For
End If
Next input
Catch ex As Exception
MessageBox.Show(ex.ToStrin g)
End Try
End Sub
End Class
Bob
' Add a reference to MSHTML to your project.
Public Class formBrowser
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.ICon
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Private WithEvents browserGoogle As AxSHDocVw.AxWebBrowser
<System.Diagnostics.Debugg
Dim resources As System.Resources.ResourceM
Me.browserGoogle = New AxSHDocVw.AxWebBrowser
CType(Me.browserGoogle, System.ComponentModel.ISup
Me.SuspendLayout()
'
'browserGoogle
'
Me.browserGoogle.Dock = System.Windows.Forms.DockS
Me.browserGoogle.Enabled = True
Me.browserGoogle.Location = New System.Drawing.Point(0, 0)
Me.browserGoogle.Size = New System.Drawing.Size(508, 423)
Me.browserGoogle.TabIndex = 0
'
'formBrowser
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(508, 423)
Me.Controls.Add(Me.browser
Me.Name = "formBrowser"
Me.Text = "Google Search"
Me.WindowState = System.Windows.Forms.FormW
CType(Me.browserGoogle, System.ComponentModel.ISup
Me.ResumeLayout(False)
End Sub
#End Region
Private m_document As mshtml.HTMLDocument
Private m_url As String = "http://www.google.com/"
Private Sub formBrowser_Activated(ByVa
If Me.browserGoogle.Document Is Nothing Then
Me.browserGoogle.Navigate2
End If
End Sub
Private Sub browserEE_DocumentComplete
If e.uRL = m_url Then
m_document = Me.browserGoogle.Document
Me.Google()
End If
End Sub
Private Sub Google()
Try
For Each input As mshtml.HTMLInputElement In m_document.getElementsByTa
If input.title = "Google Search" Then
input.value = "define: learned"
ElseIf input.name = "btnG" Then
input.click()
Exit For
End If
Next input
Catch ex As Exception
MessageBox.Show(ex.ToStrin
End Try
End Sub
End Class
Bob
ASKER
What does your code do?
Can I load the whole webpage's html into a variable?
Can I load the whole webpage's html into a variable?
1) This is a form with a WebBrowser COM control on it, which navigates to Google, and does a lookup, and gets the results in the DocumentComplete event handler. The Google method looks for certain <input> elements to set values and perform button clicks.
2) Yes, you can load the HTML into a variable, but what are you going to do with that text?
Bob
2) Yes, you can load the HTML into a variable, but what are you going to do with that text?
Bob
ASKER
I would like to save the text into a text file.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
2) Call document.getElementById or document.getElementByTagNa
Bob