基于HTTP协议用WinSock实现任意文件下载_.NET教程_编程技术_ASP.NET_青云站长教程网
欢迎来到站长教程网!
  • 秒到短信接口 免费试用
  • 微信支付宝接口 秒结算
  • 中文网站排名|申请加入
  • 搜索引擎全站查询
  • 查询移动网站权重数据
  • 海外主机 台湾公司直销
  • ASP.NET

    当前位置:主页 > 网络编程 > ASP.NET >

    基于HTTP协议用WinSock实现任意文件下载_.NET教程_编程技术

    时间:2019-08-13|栏目:ASP.NET|点击:
  • 下载
    HTTP协议是文本格式通讯,下载文件是二进制数据,怎样处理好两种格式,而不受VB独断专行的Unicode转换影响,本代码提供了一个示例。

    Option Explicit
    Private strURL As String
    Private mstrFileName As String, mlngFileNum As Long
    Private mlngFileLen As Long, mlngCurByte As Long
    Private mblnOnlyLen As Boolean, mblnPutStart As Boolean
    Private Sub Form_Load()
        strURL = Text1.Text '准备下载的文件URL
        mstrFileName = Text2.Text   '下载文件在本存放的位置与文件名
        Label1.Caption = "文件总字节:0"
        Label2.Caption = "已下载字节:0"
        Command1.Caption = "开始下载"
        Command2.Caption = "取得长度"
    End Sub
    Private Sub Command1_Click()
        mblnOnlyLen = False
        DownFile
    End Sub
    Private Sub Command2_Click()
        mblnOnlyLen = True
        Label1.Caption = "文件总字节:0"
        DownFile
    End Sub
    Private Sub DownFile()
        mblnPutStart = False
        Label2.Caption = "已下载字节:0"
        Command1.Enabled = False
        Command2.Enabled = False
        With Winsock1
            If .State <> sckClosed Then .Close
            .Protocol = sckTCPProtocol
            .RemoteHost = "article.tianyaclub.com"
            .RemotePort = 80
            .Connect
        End With
    End Sub

    Private Sub Winsock1_Connect()
        Dim s As String
        s = "GET " + strURL + " HTTP/1.0" + vbCrLf
        s = s + "Accept: */*" + vbCrLf
        s = s & "Pragma: no-cache" & vbCrLf
        s = s & "Cache-Control: no-cache" & vbCrLf
        s = s & "Connection: close" & vbCrLf & vbCrLf
        s = s + vbCrLf
        Winsock1.SendData s
    End Sub
    Private Sub CloseAll()
        If Winsock1.State <> sckClosed Then Winsock1.Close
        Close #mlngFileNum
        Command1.Enabled = True
        Command2.Enabled = True
    End Sub
    Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
        Dim RevData() As Byte
        Dim a() As Byte, b() As String, c() As String
        Dim s As String, i As Long, k As Long
        On Error GoTo fail
        If mblnPutStart = False Then
            Winsock1.PeekData RevData, vbArray Or vbByte
            k = InStrB(1, RevData, ChrB(13) & ChrB(10) & ChrB(13) & ChrB(10))
            If k > 0 Then
                Winsock1.GetData RevData, vbArray Or vbByte
                a = LeftB(RevData, k - 1)
                RevData = MidB(RevData, k + 4)
                s = StrConv(a, vbUnicode)
                b = Split(s, vbCrLf)
                If InStr(1, b(0), "200 OK", vbTextCompare) = 0 Then GoTo fail
                For i = 1 To UBound(b)
                    c = Split(b(i), ": ")
                    Select Case c(0)
                        Case "Content-Length"
                            mlngFileLen = CLng(c(1))
                            Label1.Caption = "文件总字节:" & mlngFileLen
                            If mblnOnlyLen Then
                                CloseAll
                                Exit Sub
                            End If
                    End Select
                Next
                mblnPutStart = True
                mlngCurByte = UBound(RevData) + 1
                mlngFileNum = FreeFile
                Open mstrFileName For Binary As #mlngFileNum
            Else
                Exit Sub
            End If
        Else
            Winsock1.GetData RevData, vbArray Or vbByte
            mlngCurByte = mlngCurByte + bytesTotal
        End If
        Put #mlngFileNum, , RevData
        Label2.Caption = "已下载字节:" & mlngCurByte
        If mlngCurByte = mlngFileLen Then
            CloseAll
            MsgBox "下载成功!"
        End If
        Exit Sub
    fail:
        CloseAll
        MsgBox "网络传输错误,文件下载失败!"
    End Sub



    上一篇:《Essential .Net》读书笔记 - Chapter 2_.NET教程_编程技术

    栏    目:ASP.NET

    下一篇:[EnterpriseServices]利用assembly定义我们的组件在COM+中的注册方式_.NET教程_编程技术

    本文标题:基于HTTP协议用WinSock实现任意文件下载_.NET教程_编程技术

    本文地址:http://www.jh-floor.com/wangluobiancheng/ASP_NET/20218.html

    您可能感兴趣的文章

    广告投放 | 联系我们 | 版权申明

    重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

    如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

    联系QQ:888888 | 邮箱:888888#qq.com(#换成@)

    Copyright © 2002-2017 青云站长教程网 版权所有 琼ICP备xxxxxxxx号