使用socket向HTTP服务器发送数据

发布于 2024-12-03 09:45:14 字数 1543 浏览 1 评论 0原文

我正在用 vb 6 编写一个程序。我想将数据发送到我的服务器并获取返回的数据
但我遇到了一个问题:当我发送数据(没有任何错误)时,我的服务器没有答复。
注意:我使用XAMPP作为服务器,浏览器没问题!!!

Private blnConnected As Boolean


Private Sub cmdSend_Click()
    Dim str As String
    str = "GET /newpass/ HTTP/1.1" & vbNewLine & "Host: localhost" & _
        vbNewLine & "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.21) Gecko/20110830 Firefox/3.6.21" & _
        vbNewLine & "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" & _
        vbNewLine & "Accept-Language: en-us,en;q=0.5" & _
        vbNewLine & "Accept-Encoding: gzip,deflate" & _
        vbNewLine & "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7" & _
        vbNewLine & "Keep-Alive: 115" & _
        vbNewLine & "Connection: keep-alive"
    winsock.Protocol = sckTCPProtocol
    winsock.RemoteHost = "localhost"
    winsock.RemotePort = 80
    winsock.Connect
    While Not blnConnected
        DoEvents
    Wend


    winsock.SendData str

End Sub


Private Sub winsock_Connect()
 blnConnected = True
End Sub

Private Sub winsock_DataArrival(ByVal bytesTotal As Long)
    Dim strResponse As String
    winsock.GetData strResponse, vbString, bytesTotal
    info.Text = strResponse
End Sub

Private Sub winsock_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)
winsock.Close
End Sub

I'm writing a program in vb 6. I want to send data to my server and get returned data
but I faced a problem: when I send data( without any error) there is no answer from my server.
Note: I'm using XAMPP as server and it's ok with browser!!!

Private blnConnected As Boolean


Private Sub cmdSend_Click()
    Dim str As String
    str = "GET /newpass/ HTTP/1.1" & vbNewLine & "Host: localhost" & _
        vbNewLine & "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.21) Gecko/20110830 Firefox/3.6.21" & _
        vbNewLine & "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" & _
        vbNewLine & "Accept-Language: en-us,en;q=0.5" & _
        vbNewLine & "Accept-Encoding: gzip,deflate" & _
        vbNewLine & "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7" & _
        vbNewLine & "Keep-Alive: 115" & _
        vbNewLine & "Connection: keep-alive"
    winsock.Protocol = sckTCPProtocol
    winsock.RemoteHost = "localhost"
    winsock.RemotePort = 80
    winsock.Connect
    While Not blnConnected
        DoEvents
    Wend


    winsock.SendData str

End Sub


Private Sub winsock_Connect()
 blnConnected = True
End Sub

Private Sub winsock_DataArrival(ByVal bytesTotal As Long)
    Dim strResponse As String
    winsock.GetData strResponse, vbString, bytesTotal
    info.Text = strResponse
End Sub

Private Sub winsock_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)
winsock.Close
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷了相思 2024-12-10 09:45:14

尝试在最后一个标题后添加 2 个换行符。这将向服务器表明标头已完成。

str = "GET /newpass/ HTTP/1.1" & vbNewLine & "Host: localhost" & _
        vbNewLine & "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.21) Gecko/20110830 Firefox/3.6.21" & _
        vbNewLine & "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" & _
        vbNewLine & "Accept-Language: en-us,en;q=0.5" & _
        vbNewLine & "Accept-Encoding: gzip,deflate" & _
        vbNewLine & "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7" & _
        vbNewLine & "Keep-Alive: 115" & _
        vbNewLine & "Connection: keep-alive" & vbNewLine & vbNewLine

Try adding 2 newlines after your last header. That will indicate to the server that the headers are complete.

str = "GET /newpass/ HTTP/1.1" & vbNewLine & "Host: localhost" & _
        vbNewLine & "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.21) Gecko/20110830 Firefox/3.6.21" & _
        vbNewLine & "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" & _
        vbNewLine & "Accept-Language: en-us,en;q=0.5" & _
        vbNewLine & "Accept-Encoding: gzip,deflate" & _
        vbNewLine & "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7" & _
        vbNewLine & "Keep-Alive: 115" & _
        vbNewLine & "Connection: keep-alive" & vbNewLine & vbNewLine
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文