⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.vb

📁 编程之道VB.NETt程序设计入门-589M.zip
💻 VB
字号:
Imports System.Net
Imports System.Net.Sockets
Imports System.Text

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows 窗体设计器生成的代码 "

    Public Sub New()
        MyBase.New()

        '该调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        '在 InitializeComponent() 调用之后添加任何初始化

    End Sub

    '窗体重写处置以清理组件列表。
    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

    'Windows 窗体设计器所必需的
    Private components As System.ComponentModel.IContainer

    '注意:以下过程是 Windows 窗体设计器所必需的
    '可以使用 Windows 窗体设计器修改此过程。
    '不要使用代码编辑器修改它。
    Friend WithEvents txtUrl As System.Windows.Forms.TextBox
    Friend WithEvents btnOpen As System.Windows.Forms.Button
    Friend WithEvents txtPageSource As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.txtUrl = New System.Windows.Forms.TextBox()
        Me.btnOpen = New System.Windows.Forms.Button()
        Me.txtPageSource = New System.Windows.Forms.TextBox()
        Me.SuspendLayout()
        '
        'txtUrl
        '
        Me.txtUrl.Location = New System.Drawing.Point(72, 8)
        Me.txtUrl.Name = "txtUrl"
        Me.txtUrl.Size = New System.Drawing.Size(472, 21)
        Me.txtUrl.TabIndex = 0
        Me.txtUrl.Text = "www.google.com"
        '
        'btnOpen
        '
        Me.btnOpen.Location = New System.Drawing.Point(8, 8)
        Me.btnOpen.Name = "btnOpen"
        Me.btnOpen.Size = New System.Drawing.Size(56, 24)
        Me.btnOpen.TabIndex = 1
        Me.btnOpen.Text = "打开"
        '
        'txtPageSource
        '
        Me.txtPageSource.Location = New System.Drawing.Point(8, 40)
        Me.txtPageSource.Multiline = True
        Me.txtPageSource.Name = "txtPageSource"
        Me.txtPageSource.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.txtPageSource.Size = New System.Drawing.Size(536, 304)
        Me.txtPageSource.TabIndex = 2
        Me.txtPageSource.Text = ""
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(552, 350)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtPageSource, Me.btnOpen, Me.txtUrl})
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
        '获取的页面源代码
        Dim sPageSource As String

        '缓冲从服务器获取的字节流
        Dim abyteReceive(1024) As Byte

        '构造HTTP GET请求
        Dim sGet As String = "GET / HTP/1.0" & vbCrLf & _
                             "Host: " & txtUrl.Text & vbCrLf & _
                             "Connection: Close" & vbCrLf & vbCrLf

        Dim asciiGet As Encoding = Encoding.ASCII

        '将GET请求转换为ASCII编码
        Dim abyteGet() As Byte = asciiGet.GetBytes(sGet)

        Try
            '从DNS获取服务器IP地址
            Dim hostentry As IPHostEntry = Dns.GetHostByName(txtUrl.Text)
            Dim hostadd As IPAddress = hostentry.AddressList(0)

            '构造服务器的网络端点对象(IP地址和端口号)
            Dim EPhost As IPEndPoint = New IPEndPoint(hostadd, 80)

            '建立一个新的套接字
            Dim sockHTTP As Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)

            '尝试连接到服务器
            sockHTTP.Connect(EPhost)

            If sockHTTP.Connected = False Then
                txtPageSource.Text = "无法连接到服务器"
                Exit Sub
            End If

            '发送HTTP GET请求
            sockHTTP.Send(abyteGet, abyteGet.Length, 0)

            '从Socket中读取数据,并将字节流转换为字符串
            Dim iBytes As Integer = 1
            Do While iBytes > 0
                iBytes = sockHTTP.Receive(abyteReceive, abyteReceive.Length, 0)
                sPageSource &= asciiGet.GetString(abyteReceive, 0, iBytes)
            Loop

            sockHTTP.Close()
            sockHTTP = Nothing

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

        txtPageSource.Text = sPageSource

    End Sub
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -