怎样用winsock通过http协议下载文件,比如要下载“http://www.*****.com/a.exe”
要用winsock,不要inet,谢谢

解决方案 »

  1.   

    看这篇文章:How to download a file from the Web server,使用了Winsock控件,包含代码http://www.vbip.com/winsock/winsock_http_01.asp
      

  2.   

    Option Explicit
    Dim strCommand As String
    Dim strWebPage As StringPrivate Sub Command1_Click()Winsock1.RemoteHost = "202.103.176.81" '返回或设置远程计算机,控件向它发送数据或从它那里接收数据。既可提供主机名,比如 "FTP://ftp.microsoft.com",也可提供点格式下的 IP 地址字符串,比如 "100.0.1.1"。
    Winsock1.RemotePort = 80 '返回或设置要连接的远程端口号
    Winsock1.Connect '返回与远程计算机的连接。
    End SubPrivate Sub Winsock1_Connect() '当一个 Connect 操作完成时发生。
    On Error Resume Next
    strWebPage = "http://202.103.176.81/crun/yingzi007/code_1.asp"
    strCommand = "GET " + strWebPage + " HTTP/1.0" + vbCrLf 'GET 为FTP命令
    strCommand = strCommand + "Accept: */*" + vbCrLf      '这句可以不要
    strCommand = strCommand + "Accept: text/html" + vbCrLf '这句可以不要
    strCommand = strCommand + vbCrLf      '记住一定要加上vbCrLfDebug.Print strCommandWinsock1.SendData strCommand  ''给远程计算机发送数据End SubPrivate Sub Winsock1_DataArrival(ByVal bytesTotal As Long) '当新数据到达时产生该事件
    On Error Resume Next   '在错误处理程序结束后,恢复原有的运行
    Dim webData As String
    Winsock1.GetData webData, vbString '检取当前的数据块
    Text1.Text = Text1.Text + webData
    End Sub
      

  3.   


    Private Sub cmdconnect_Click()
    On Error Resume Next  Winsock1.RemoteHost = txtwebserver.Text
      Winsock1.RemotePort = 80
      Winsock1.Connect
      
    End SubPrivate Sub Form_Load()End SubPrivate Sub Winsock1_Connect()
    On Error Resume Next
      Dim strCommand As String
      Dim strWebPage As String
      
      
      strWebPage = txtlocation.Text
      strCommand = "GET " + strWebPage + " HTTP/1.0" + vbCrLf
      strCommand = strCommand + "Accept: */*" + vbCrLf
      strCommand = strCommand + "Accept: text/html" + vbCrLf
      strCommand = strCommand + vbCrLf
      Winsock1.SendData strCommand
    End SubPrivate Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
      On Error Resume Next
      Dim webData As String
      
      Winsock1.GetData webData, vbString
      txtWebPage.Text = txtWebPage.Text + webData
    End Sub
      

  4.   

    我在数据接收完毕后,要继续下载同一个服务器的另外一个文件,我有添加了发HTTP头的请求代码,但程序报错,不知道是什么原因, 要连续下载多个文件该怎么做,就是怎么发送请求头,和接收返回信息
    Private Sub cmdconnect_Click()
    On Error Resume Next  Winsock1.RemoteHost = txtwebserver.Text
      Winsock1.RemotePort = 80
      Winsock1.Connect
      
    End SubPrivate Sub Form_Load()End SubPrivate Sub Winsock1_Connect()
    On Error Resume Next
      Dim strCommand As String
      Dim strWebPage As String
      
      
      strWebPage = txtlocation.Text
      strCommand = "GET " + strWebPage + " HTTP/1.0" + vbCrLf
      strCommand = strCommand + "Accept: */*" + vbCrLf            
      strCommand = strCommand + "Accept: text/html" + vbCrLf       
      strCommand = strCommand + vbCrLf
      Winsock1.SendData strCommand
    End SubPrivate Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
      On Error Resume Next
      Dim webData As String
      
      Winsock1.GetData webData, vbString
      txtWebPage.Text = txtWebPage.Text + webData
    End Sub
      

  5.   

    我看了: TechnoFantasy(冰儿马甲www.applevb.com) 看这篇文章:How to download a file from the Web server,使用了Winsock控件,包含代码
    http://www.vbip.com/winsock/winsock_http_01.asp我照着做,但它是将exe文件显视为文本了,请问高手要怎么将下载的文件保存为exe文件?  Private m_strRemoteHost As String    'the web server to connect to
    Private m_strFilePath As String      'relative path to the file to retrieve
    Private m_strHttpResponse As String  'the server response
    Private m_bResponseReceived As Boolean
    '
    Private Sub cmdReadURL_Click()
        '
        Dim strURL As String    'temporary buffer
        '
        On Error GoTo ERROR_HANDLER
        '
        'check the textbox
        If Len(txtURL) = 0 Then
            MsgBox "Please, enter the URL to retrieve.", vbInformation
            Exit Sub
        End If
        '
        'if the user has entered "http://", remove this substring
        '
        If Left(txtURL, 7) = "http://" Then
            strURL = Mid(txtURL, 8)
        Else
            strURL = txtURL
        End If
        '
        'get remote host name
        '
        m_strRemoteHost = Left$(strURL, InStr(1, strURL, "/") - 1)
        '
        'get relative path to the file to retrieve
        '
        m_strFilePath = Mid$(strURL, InStr(1, strURL, "/"))
        '
        'clear the RichTextBox
        '
        rtbDocument.Text = ""
        '
        'clear the buffer
        '
        m_strHttpResponse = ""
        '
        'turn off the m_bResponseReceived flag
        '
        m_bResponseReceived = False
        '
        'establish the connection
        '
        With wscHttp
            .Close
            .LocalPort = 0
            .Connect m_strRemoteHost, 80
        End With
        '
    EXIT_LABEL:
        Exit Sub
        
    ERROR_HANDLER:
        '
        If Err.Number = 5 Then
            strURL = strURL & "/"
            Resume 0
        Else
            MsgBox "Error was occurred." & vbCrLf & _
                    "Error #: " & Err.Number & vbCrLf & _
                    "Description: " & Err.Description, vbExclamation
            GoTo EXIT_LABEL
        End If
        '
    End SubPrivate Sub wscHttp_Close()
        '
        Dim strHttpResponseHeader As String
        '
        'to cut of the header info, we must find?
        'a blank line (vbCrLf & vbCrLf)
        'that separates the message body from the header
        '
        If Not m_bResponseReceived Then
            strHttpResponseHeader = Left$(m_strHttpResponse, _
                                    InStr(1, m_strHttpResponse, _
                                    vbCrLf & vbCrLf) - 1)
            Debug.Print strHttpResponseHeader
            m_strHttpResponse = Mid(m_strHttpResponse, _
                                InStr(1, m_strHttpResponse, _
                                vbCrLf & vbCrLf) + 4)
            '
            'pass the document data to the RichTextBox control
            '
            rtbDocument.Text = m_strHttpResponse
            '
            'turn on the m_bResponseReceived flag
            '
            m_bResponseReceived = True
            '
        End If
        '
    End SubPrivate Sub wscHttp_Connect()
        '
        Dim strHttpRequest As String
        '
        'create the HTTP Request
        '
        'build request line that contains the HTTP method,?
        'path to the file to retrieve,
        'and HTTP version info. Each line of the request?
        'must be completed by the vbCrLf
        strHttpRequest = "GET " & m_strFilePath & " HTTP/1.1" & vbCrLf
        '
        'add HTTP headers to the request
        '
        'add required header - "Host", that燾ontains the remote host name
        '
        strHttpRequest = strHttpRequest & "Host: " & m_strRemoteHost & vbCrLf
        '
        'add the "Connection" header to force the server to close the connection
        '
        strHttpRequest = strHttpRequest & "Connection: close" & vbCrLf
        '
        'add optional header "Accept"
        '
        strHttpRequest = strHttpRequest & "Accept: */*" & vbCrLf
        '
        'add other optional headers
        '
        'strHttpRequest = strHttpRequest & <Header Name> & _
                          <Header Value> & vbCrLf
        '. . .
        '
        'add a blank line that indicates the end of the request
        strHttpRequest = strHttpRequest & vbCrLf
        '
        'send the request
        wscHttp.SendData strHttpRequest
        '
        Debug.Print strHttpRequest
        '
    End SubPrivate Sub wscHttp_DataArrival(ByVal bytesTotal As Long)
        '
        On Error Resume Next
        '
        Dim strData As String
        '
        'get arrived data from winsock buffer
        '
        wscHttp.GetData strData
        '
        'store the data in the m_strHttpResponse variable
        m_strHttpResponse = m_strHttpResponse & strData
        '
    End Sub