Récuperer le type d'un serveur http via winsock


Description

Un simple code pour recuperer le type d'un serveur http et le temps de reponse , et qui donc par la meme occasion montre comment jouer avec la reception de donnée spécifique par winsock.
C'est simple, mais ca peut servir.
Pour info, touts les serveurs ne renvoient pas toujours dans leurs headers leurs type, par exemple yahoo.fr , mais ce type d'exemple est rare (sur les quelques tests que j'ai fait) .
Et sur votre form, il faut 3 textbox nommées "Host" , "srvtype" et "anstime" , un controle winsock nommé ws et un command button "command1" .

Source / Exemple :


Private Declare Function GetTickCount Lib "kernel32" () As Long
Dim answert As Long
Dim hblisten As Boolean
Dim buffer As String

Private Sub Command1_Click()
buffer = ""
hblisten = True
If Host.Text <> "" Then
ws.Close
ws.Connect "" & Host.Text & "", 80
answert = GetTickCount
Else
MsgBox "Vous n'avez pas entré de serveur", vbCritical
End If
End Sub

Private Sub ws_connect()
If hblisten = True Then
ws.SendData "GET http://" & Host.Text & "/ HTTP/1.0" & vbCrLf & _
            "Host: " & Host.Text & "" & vbCrLf & vbCrLf
End If
End Sub

Private Sub ws_DataArrival(ByVal bytesTotal As Long)
Dim data As String
ws.GetData data
buffer = buffer & data
If hblisten = True Then
 If hb_str_in_str(buffer, "Server: ", vbCrLf) <> "" Then
  srvtype.Text = hb_str_in_str(buffer, "Server: ", vbCrLf)
  anstime.Text = "réponse du serveur en " & CDec((GetTickCount - answert) / 1000) & " secondes"
    ws.Close
  hblisten = false
  Else
  srvtype.Text = "Can't get server type"
  anstime.Text = "réponse du serveur en " & CDec((GetTickCount - answert) / 1000) & " secondes"
    ws.Close
  hblisten = false
 End If

End If
 End Sub
 
 Public Function hb_str_in_str(source As String, stravant As String, strapres As String)
If InStr(1, source, stravant) > 0 Then
If InStr((InStr(1, source, stravant)), source, strapres) > 0 Then
If Mid(source, (InStr(1, source, stravant) + Len(stravant)), InStr((InStr(1, source, stravant) + Len(stravant)), source, strapres) - (InStr(1, source, stravant) + Len(stravant))) <> "" Then
hb_str_in_str = Mid(source, (InStr(1, source, stravant) + Len(stravant)), InStr((InStr(1, source, stravant) + Len(stravant)), source, strapres) - (InStr(1, source, stravant) + Len(stravant)))
End If
End If
End If
End Function

Conclusion :


en esperant que ca puisse servir

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.