|
Tipp 0019
|
HTML-Seite auslesen
|
|
|
Autor/Einsender: Datum: |
|
Alex Wenig 15.09.2003 |
|
Entwicklungsumgebung: |
|
VB.Net 2002 |
Framework: |
|
1.0 |
|
|
Die Klasse HttpWebRequest, Namespace System.Net, stellt eine
HTTP-spezifische Implementierung der WebRequest-Klasse bereit. WebRequest und
WebResponse sind abstrakte Basisklassee (MustInherit in Visual Basic) für den
Zugriff auf Daten im Internet. Mit Hilfe bestimmter Methoden beider Klassen kann ein Stream ausgelesen
(System.IO.StreamReader) und wie in diesem Beispiel, in einer Multiline-Textbox
angezeigt werden.
|
|
|
Imports System.Net
Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form Vom Windows Form Designer generierter Code
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
pbrPercent.Value = (0)
txtHtml.Text = ""
If txtAddress.Text <> "" Then
If Not txtAddress.Text.TrimStart.Substring(0, 7) = _
"http://" Then
txtAddress.Text = "http://" & txtAddress.Text
End If
txtHtml.Text = GetHTMLPage(txtAddress.Text)
Else
MsgBox("Geben Sie eine Internetadresse ein.", _
MsgBoxStyle.Exclamation)
End If
End Sub
Public Function GetHTMLPage(ByVal URL As String) As String
Dim sResult As String
Dim oHttp As HttpWebRequest
Dim objResponse As WebResponse
Dim objRequest As WebRequest
Try
objRequest = System.Net.HttpWebRequest.Create(URL)
objRequest.Method = "GET"
objRequest.Timeout = 120000
objResponse = objRequest.GetResponse
ProgressBarinPercent(URL, pbrPercent)
Dim sr As System.IO.StreamReader = _
New System.IO.StreamReader( _
objResponse.GetResponseStream(), _
System.Text.Encoding.UTF7)
sResult = sr.ReadToEnd()
sr.Close()
objResponse.Close()
Return sResult
Catch ex As System.Exception
MsgBox(ex.Message)
pbrPercent.Value = (0)
txtHtml.Text = ""
oHttp = Nothing
objResponse = Nothing
objRequest = Nothing
End Try
End Function
Function ProgressBarinPercent(ByVal sURL As String, _
ByVal pProgress As ProgressBar)
Const APPROXFILESIZE As Integer = 80000
Dim wRemote As System.Net.WebRequest
Dim bBuffer(999) As Byte
Dim sResponseData As String
Dim iBytesRead As Integer
wRemote = WebRequest.Create(sURL)
pProgress.Maximum = APPROXFILESIZE
Dim sChunks As Stream = wRemote.GetResponse.GetResponseStream
Do
iBytesRead = sChunks.Read(bBuffer, 0, 1000)
If pProgress.Value + iBytesRead <= pProgress.Maximum Then
pProgress.Value += iBytesRead
Else
pProgress.Value = pProgress.Maximum
End If
Loop Until iBytesRead = 0
pProgress.Value = pProgress.Maximum
sChunks.Close()
End Function
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
End Class
|
|
|
|
|
Windows-Version |
98/SE |
|
|
ME |
|
|
NT |
|
|
2000 |
|
|
XP |
|
|
Vista |
|
|
Win
7 |
|
|
|
|
Download (6,7 kB)
|
Downloads bisher: [ 2666 ]
|
|
|