Winsock connects but gets no response from server (stays on waiting status)
Hi,
I'm new to winsock and VB, What I want to do is simple. Just to get a webpage data from a site by using winsock (using an example source code which works in other PCs very well), the program connects to website and prints "waiting for response" but never gets the response (DataArrival never gets fired), I have attached the program, So what may cause this error?
Last edited by the_one2003a; Jul 10th, 2009 at 01:14 PM.
Re: Winsock connects but gets no response from server (stays on waiting status)
One reason might be you're sending HTTP/1.0 instead of 1.1, most servers are using the 1.1 protocol but they should still work with 1.0.
You're also not supplying much information in the HTTP header, and the Connection: Keep -Alive should be: Connection: Keep-Alive (no space before the dash).
Your code is also messy... some variables not declared, others declared as variants, no indenting, etc. Makes debugging a lot harder. I cleaned up the code a little and made a few changes, see if this works... If it doesn't, post the URL of the site that you tried.
Code:
Option Explicit
Private HTTPData As String
Private Sub Status(ByRef strStatus As String)
With txtStatus
.SelStart = Len(.Text)
.SelText = strStatus & vbCrLf
End With
End Sub
Private Sub Command1_Click()
With Win
HTTPData = vbNullString 'Reset/erase data
.Close
.Connect Text2.Text, 80
End With
Text1.Alignment = vbLeftJustify
Text1.Text = Text2.Text & " Informations:" & vbCrLf & vbCrLf
Status "Connecting..."
End Sub
Private Sub Label3_Click()
Do While Me.Height > 255
Me.Height = Me.Height - 15
DoEvents
Loop
Do While Me.Width > 15
Me.Width = Me.Width - 15
DoEvents
Loop
Unload Me 'Don't use End
End Sub
Private Sub Label4_Click()
Me.WindowState = vbMinimized
End Sub
Private Sub txtStatus_Change()
txtStatus.SelStart = Len(txtStatus)
End Sub
Private Sub Win_Close()
Win.Close
Status "Connection Closed."
AnalyseData HTTPData
End Sub
Private Sub Win_Connect()
Dim Packet As String
Status "Connected."
Text1 = Text1 & "Host Name: " & Win.RemoteHost & vbCrLf
Text1 = Text1 & "IP: " & Win.RemoteHostIP & vbCrLf
Packet = "GET / HTTP/1.1" & vbCrLf
Packet = Packet & "User-Agent: HTTPScanner" & vbCrLf
Packet = Packet & "Accept: *.*" & vbCrLf
Packet = Packet & "Connection: Close" & vbCrLf & vbCrLf
Win.SendData Packet
End Sub
Function AnalyseData(strData)
Dim s As Long, l As String, info As String, inf() As String
Dim i As Long
s = InStr(strData, "Content-Length:") + 16
l = Mid(strData, s, InStr(s, strData, vbCrLf) - s)
info = Left(strData, InStr(strData, vbCrLf & vbCrLf)) 'Left(strData, Len(strData) - l)
If InStr(info, "200 OK") <> 0 Then Status "Sent packet was accepted."
inf = Split(info, vbCrLf)
Text1 = Text1 & "Home Page Size: " & l & " bytes" & vbCrLf
For i = 0 To UBound(inf)
If InStr(inf(i), "HTTP/") = 0 And InStr(inf(i), "Content-Length: ") = 0 And InStr(inf(i), "Connection: ") = 0 And inf(i) <> "" Then
Text1 = Text1 & inf(i) & vbCrLf
End If
Next
hell:
Text1 = Left(Text1, Len(Text1) - 2)
Status "Completed."
End Function
Private Sub Win_DataArrival(ByVal bytesTotal As Long)
Dim Data As String
Status "Receiving data..."
Win.GetData Data, vbString, bytesTotal
HTTPData = HTTPData & Data
End Sub
Private Sub Win_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
Status "Error"
Win.Close
End Sub
Private Sub Win_SendComplete()
Status "Packet sent. Waiting for response."
End Sub
Private Sub Win_SendProgress(ByVal bytesSent As Long, ByVal bytesRemaining As Long)
Status "Sending Packet..."
End Sub
Re: Winsock connects but gets no response from server (stays on waiting status)
Actually I didn't written the code just got from a website to test. Changed the code as above and tested but when I click to connect and get the webpage it prints the following (testing www.google.com):
Code:
Sending Packet...
Packet sent. Waiting for response.
But no response and then prints 'Error'. I think it is something related to my PC configuration since I have tested some other VB programs which use Winsock and they have the same problem and just wait for response which never retrieve.
So any suggestions on how to find the problem and solving it?