Le protocole HTTP: Client HTTP en visual basic


Ce programme est le premier en visual basic ( un petit retour en adolescence !! ), il permet tres simplement de faire une requete http, et d'obtenir le resultat dans une fenêtre. Grace a la dllde intenet explorer vous pouvez aussi visualiser la page en wysiwyg.

voici a quoi cela ressemble :

( sauvegarder le source dans un fichier vbclient.frm avant de l'inclure dans un nouveau projet visual basic)
VERSION 5.00
Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX"
Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "Mscomctl.ocx"
Object = "{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}#1.1#0"; "shdocvw.dll"
Begin VB.Form vbclientHttp
   BorderStyle     =   3  'Fixed Dialog
   Caption         =   "salemioche.com : http vbClient"
   ClientHeight    =   11685
   ClientLeft      =   5100
   ClientTop       =   1905
   ClientWidth     =   9450
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   11685
   ScaleWidth      =   9450
   ShowInTaskbar   =   0   'False
   Begin VB.CommandButton clear
      Caption         =   "Clear"
      Height          =   495
      Left            =   8520
      TabIndex        =   10
      Top             =   240
      Width           =   855
   End
   Begin VB.TextBox url
      Height          =   285
      Left            =   3720
      TabIndex        =   8
      Text            =   "index.htm"
      Top             =   330
      Width           =   3735
   End
   Begin VB.TextBox port
      Height          =   285
      Left            =   3000
      TabIndex        =   6
      Text            =   "80"
      Top             =   330
      Width           =   390
   End
   Begin MSWinsockLib.Winsock socket
      Left            =   0
      Top             =   0
      _ExtentX        =   741
      _ExtentY        =   741
      _Version        =   393216
      RemoteHost      =   "www.salemioche.com"
      RemotePort      =   80
   End
   Begin VB.CommandButton Go
      Caption         =   "Go"
      Height          =   495
      Left            =   7800
      TabIndex        =   1
      Top             =   240
      Width           =   615
   End
   Begin VB.TextBox Host
      Height          =   285
      Left            =   960
      TabIndex        =   0
      Text            =   "www.salemioche.com"
      Top             =   330
      Width           =   1815
   End
   Begin VB.TextBox body
      Height          =   8175
      Left            =   480
      MultiLine       =   -1  'True
      ScrollBars      =   3  'Both
      TabIndex        =   2
      Top             =   3240
      Width           =   8655
   End
   Begin VB.TextBox header
      Height          =   1815
      Left            =   480
      MultiLine       =   -1  'True
      ScrollBars      =   2  'Vertical
      TabIndex        =   3
      Top             =   1320
      Width           =   8655
   End
   Begin SHDocVwCtl.WebBrowser explorer
      Height          =   10095
      Left            =   480
      TabIndex        =   9
      Top             =   1320
      Visible         =   0   'False
      Width           =   8655
      ExtentX         =   15266
      ExtentY         =   17806
      ViewMode        =   0
      Offline         =   0
      Silent          =   0
      RegisterAsBrowser=   0
      RegisterAsDropTarget=   1
      AutoArrange     =   0   'False
      NoClientEdge    =   0   'False
      AlignLeft       =   0   'False
      NoWebView       =   0   'False
      HideFileNames   =   0   'False
      SingleClick     =   0   'False
      SingleSelection =   0   'False
      NoFolders       =   0   'False
      Transparent     =   0   'False
      ViewID          =   "{0057D0E0-3573-11CF-AE69-08002B2E1262}"
      Location        =   "http:///"
   End
   Begin VB.CheckBox isproxy
      Caption         =   "Proxy"
      Height          =   285
      Left            =   840
      TabIndex        =   12
      ToolTipText     =   "if you use a proxy"
      Top             =   1440
      Visible         =   0   'False
      Width           =   975
   End
   Begin VB.TextBox proxyserver
      Height          =   285
      Left            =   1920
      TabIndex        =   13
      ToolTipText     =   "proxy server name"
      Top             =   1440
      Visible         =   0   'False
      Width           =   2535
   End
   Begin VB.TextBox proxyport
      Height          =   285
      Left            =   4560
      TabIndex        =   14
      ToolTipText     =   "proxy server port number"
      Top             =   1440
      Visible         =   0   'False
      Width           =   855
   End
   Begin VB.TextBox getHeader
      Height          =   3975
      Left            =   840
      MultiLine       =   -1  'True
      TabIndex        =   15
      Text            =   "Form1.frx":0000
      Top             =   2160
      Visible         =   0   'False
      Width           =   8295
   End
   Begin MSComctlLib.TabStrip View
      Height          =   10815
      Left            =   240
      TabIndex        =   11
      Top             =   840
      Width           =   9135
      _ExtentX        =   16113
      _ExtentY        =   19076
      MultiRow        =   -1  'True
      _Version        =   393216
      BeginProperty Tabs {1EFB6598-857C-11D1-B16A-00C0F0283628}
         NumTabs         =   3
         BeginProperty Tab1 {1EFB659A-857C-11D1-B16A-00C0F0283628}
            Caption         =   "Text"
            ImageVarType    =   2
         EndProperty
         BeginProperty Tab2 {1EFB659A-857C-11D1-B16A-00C0F0283628}
            Caption         =   "IE"
            ImageVarType    =   2
         EndProperty
         BeginProperty Tab3 {1EFB659A-857C-11D1-B16A-00C0F0283628}
            Caption         =   "Setup"
            ImageVarType    =   2
         EndProperty
      EndProperty
   End
   Begin VB.Label slash
      Caption         =   "/"
      Height          =   285
      Left            =   3480
      TabIndex        =   7
      Top             =   360
      Width           =   255
   End
   Begin VB.Label portcolumn
      Caption         =   ":"
      Height          =   285
      Left            =   2880
      TabIndex        =   5
      Top             =   360
      Width           =   255
   End
   Begin VB.Label http
      Caption         =   "http://"
      Height          =   285
      Left            =   360
      TabIndex        =   4
      Top             =   360
      Width           =   615
   End
End
Attribute VB_Name = "vbclientHttp"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim isHeader As Boolean

Private Sub clear_Click()
    body.Text = ""
    header.Text = ""
End Sub


Private Sub socket_Connect()
    socket.SendData "GET /" & url.Text & " HTTP\\1.0" & vbCrLf & _
                    "Host: " & Host.Text & ":" & port.Text & vbCrLf & _
                    getHeader.Text & vbCrLf & vbCrLf
    isHeader = True

End Sub


Private Sub Go_Click()

    If socket.State <> 0 Then
        socket.Close
        clear_Click
    End If

    socket.RemotePort = port.Text

    If isproxy.Value = 0 Then
        socket.RemoteHost = Host.Text
    ElseIf proxyserver.Text <> "" Then
        socket.RemoteHost = proxyserver.Text
        socket.RemotePort = proxyport.Text
    Else
        socket.RemoteHost = "localhost"
    End If

    explorer.Navigate2 "http://" & Host.Text & ":" & port.Text & "/" & url.Text

    socket.Connect

End Sub

Private Sub socket_dataArrival(ByVal bytesTotal As Long)

    Dim str, str1 As String
    Dim lg As Long

    socket.GetData str, vbString

    If isHeader = True Then
        lg = InStr(str, vbCrLf & vbCrLf)
        If lg <> 0 Then
            isHeader = False
            header.Text = header.Text & Left(str, lg - 1)
            str1 = Replace(Right(str, Len(str) - lg - 3), vbCr & vbLf, "" & vbCrLf)
             body.Text = body.Text & str1
        Else
            header.Text = header.Text & str1
        End If
    Else
        str1 = Replace(str, vbCr & vbLf, "" & vbCrLf)
        body.Text = body.Text & str1

    End If

End Sub



Private Sub url_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = 13 Then
        Go_Click
    End If
End Sub

Private Sub View_Click()
    Select Case View.SelectedItem.Index
        Case 1
            header.Visible = True
            body.Visible = True
            explorer.Visible = False
            proxyport.Visible = False
            proxyserver.Visible = False
            isproxy.Visible = False
            getHeader.Visible = False

        Case 2
            header.Visible = False
            body.Visible = False
            explorer.Visible = True
            If explorer.LocationURL <> "" Then
                explorer.Refresh2
            End If
            proxyport.Visible = False
            proxyserver.Visible = False
            isproxy.Visible = False
            getHeader.Visible = False

        Case Else
            header.Visible = False
            body.Visible = False
            explorer.Visible = False
            proxyport.Visible = True
            proxyserver.Visible = True
            isproxy.Visible = True
            getHeader.Visible = True

    End Select

End Sub