Read data from a web page in VB.Net

  • Thread starter Thread starter John Bradley
  • Start date Start date
J

John Bradley

Toooo tired to look this one up.

I have data that I need to acces in a small program. The
data is accessable from an ASP webpage that I created
myself. If I need to see data from record #25, I just
open http://mydomain.com/datapage.asp?recnum=25. I would
like to read this data directly into a variable. I'm sure
this is quite easy (as easy as Fileopen), but I just
can't seem to find it.

Thanks
John
 
Hi Johnk
I have data that I need to acces in a small program. The
data is accessable from an ASP webpage that I created
myself. If I need to see data from record #25, I just
open http://mydomain.com/datapage.asp?recnum=25. I would
like to read this data directly into a variable. I'm sure
this is quite easy (as easy as Fileopen), but I just
can't seem to find it.

I won't know if it is difficult, but because of that you seems to use
classic ASP with a GET I think you do better asking this in a real ASP
newsgroup (not .NET)

I did for a while no classic ASP, therefore it would take me a lot of time
to help you, I don't know if there are others familiar with traditional ASP,
but I think from what I have seen from the regulars I think few, but I am
sure there are visitors who are still using it in this newsgroup, so maybe
you get your help here also, but take my advice and take a look in an real
ASP newsgroup.

Sorry I could not help you.

Cor
 
Maybe I wasn't too clear. I need to read some text from a webpage into my
vb.net program. I think that I may have found the solution though,
HttpWebRequest.

Thanks,
John
 
Figured it out myself.

Imports System.IO

Imports System.Net

Imports System.Text

Public Class Form1

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.IContainer

'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.

Friend WithEvents txtPageSource As System.Windows.Forms.TextBox

Friend WithEvents txtURL As System.Windows.Forms.TextBox

Friend WithEvents Label1 As System.Windows.Forms.Label

Friend WithEvents btnGetSource As System.Windows.Forms.Button

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.txtPageSource = New System.Windows.Forms.TextBox()

Me.btnGetSource = New System.Windows.Forms.Button()

Me.txtURL = New System.Windows.Forms.TextBox()

Me.Label1 = New System.Windows.Forms.Label()

Me.SuspendLayout()

'

'txtPageSource

'

Me.txtPageSource.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or
System.Windows.Forms.AnchorStyles.Bottom) _

Or System.Windows.Forms.AnchorStyles.Left) _

Or System.Windows.Forms.AnchorStyles.Right)

Me.txtPageSource.Location = New System.Drawing.Point(8, 32)

Me.txtPageSource.Multiline = True

Me.txtPageSource.Name = "txtPageSource"

Me.txtPageSource.ScrollBars = System.Windows.Forms.ScrollBars.Both

Me.txtPageSource.Size = New System.Drawing.Size(672, 272)

Me.txtPageSource.TabIndex = 0

Me.txtPageSource.Text = ""

'

'btnGetSource

'

Me.btnGetSource.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or
System.Windows.Forms.AnchorStyles.Right)

Me.btnGetSource.Location = New System.Drawing.Point(552, 320)

Me.btnGetSource.Name = "btnGetSource"

Me.btnGetSource.Size = New System.Drawing.Size(112, 24)

Me.btnGetSource.TabIndex = 1

Me.btnGetSource.Text = "Get Source Code"

'

'txtURL

'

Me.txtURL.Anchor = ((System.Windows.Forms.AnchorStyles.Top Or
System.Windows.Forms.AnchorStyles.Left) _

Or System.Windows.Forms.AnchorStyles.Right)

Me.txtURL.Location = New System.Drawing.Point(80, 8)

Me.txtURL.Name = "txtURL"

Me.txtURL.Size = New System.Drawing.Size(600, 20)

Me.txtURL.TabIndex = 2

Me.txtURL.Text = ""

'

'Label1

'

Me.Label1.Location = New System.Drawing.Point(8, 8)

Me.Label1.Name = "Label1"

Me.Label1.Size = New System.Drawing.Size(48, 23)

Me.Label1.TabIndex = 3

Me.Label1.Text = "URL"

Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(704, 366)

Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label1,
Me.txtURL, Me.btnGetSource, Me.txtPageSource})

Me.Name = "Form1"

Me.Text = "Form1"

Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

txtURL.Text = "http://www.jabtech.com"

End Sub

Private Sub btnGetSource_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnGetSource.Click

Dim inputURL, WebPageSourceCode As String

Dim WebPage As HttpWebRequest

Dim WebPageText As WebResponse

Dim WebPageSource As Stream

Dim WebPageSourceText As StreamReader

On Error GoTo ErrorHandler

txtPageSource.Text = ""

inputURL = txtURL.Text

WebPage = WebRequest.Create(inputURL)

WebPageText = WebPage.GetResponse()

WebPageSource = WebPageText.GetResponseStream

WebPageSourceText = New StreamReader(WebPageSource,
System.Text.Encoding.ASCII)

WebPageSourceCode = WebPageSourceText.ReadToEnd()

WebPageSourceText.Close()

WebPageSource.Close()

txtPageSource.Text = WebPageSourceCode

Exit Sub



ErrorHandler:

MsgBox("Invalid URL or page not available")

Exit Sub



End Sub



End Class
 
John Bradley said:
Figured it out myself.

Code:
[/QUOTE]

Only a hint: To keep the identation and to avoid empty lines, you can paste
the code to notepad first, then from there into your posting. It makes it
easier for us to read (and use).
 
Back
Top