Jump to content

HTTP GET with User Agent and Percentage


Recommended Posts

Hello,

I want to download a file via HTTP GET with a custom User-Agent but also show a percentage.

I found this old link which downloads with custom User-Agent, but it doesn't show percentage:

http://www.autoitscript.com/forum/index.php?showtopic=76080&hl=winhttprequest

If I add on a Range to the header (to allow me to calculate how much I've got down and enable resuming) it works but it takes a LOT longer to download.

Here's what I tried, by requesting chunks of the file using the range header and then looping through writing specified length pieces of each chunk (otherwise it just jumps to 30%, 60%, 90%, 100%), which does work, but it's very slow.

; Choose iTunes 93MB or BullZip 5MB
;$URL = "http://appldnld.apple.com.edgesuite.net/content.info.apple.com/iTunes9/061-7753.20100201.Jcv4r/iTunesSetup.exe"
$URL = "http://www.bullzip.com/download/pdf/BullzipPDFPrinter_7_1_0_1186.zip"

$sFilename = "c:\test.exe"

ConsoleWrite("Getting FileSize..." & @CRLF)
$FileSize = _GetFileSize($URL)
ConsoleWrite($FileSize & @CRLF)


If $Filesize <> -1 Then

    ConsoleWrite("Downloading " & $URL & " (" & Round($FileSize/1024/1024, 2) & " MB)" & @CRLF)

    $oBinaryStream = ObjCreate("ADODB.Stream")     
    $adTypeBinary = 1
    $adSaveCreateOverWrite = 2     
    FileDelete($sFilename)
    $oBinaryStream.Type = $adTypeBinary
    $oBinaryStream.Open
    
    $ChunkSize =  524288
    $ChunkSize = 1024 * 1024
    
    $HTTPdownload = ObjCreate("MSXML2.ServerXMLHTTP.6.0")
    
    ProgressOn("", "", "0 %")
    
    For $i = 0 to $FileSize Step $ChunkSize
    
        If $i+$ChunkSize > $FileSize Then
            $Start  = $i
            $End    = $FileSize
        Else
            $Start  = $i
            $End    = ($i + $Chunksize)-1
        EndIf
        
        $Range = $Start & "-" & $End
    
        $Progress = ($i / $FileSize) * 100
        
        ;ProgressSet($Progress, Round($Progress) & " %")
    
        ConsoleWrite("Range: " & $Range & @TAB & " (" & Round($Progress) & " %)" & @CRLF)
    
        
        $HTTPdownload.open("GET", $URL, false)
                
        
        $HTTPdownload.setRequestHeader("Range", "bytes=" & $Range)
        
            
        $HTTPdownload.Send()

        If $HTTPdownload.Status = "200" OR "206" Then
            
            $Data = ""
            
            For $w = 0 to ($End - $Start) Step 1024
                
                $Data = BinaryMid($HTTPDownload.ResponseBody, $w, 1024)
                
                ConsoleWrite("STATUS: " & $HTTPDownload.Status & " Writing" & @CRLF)
                ConsoleWrite("TEST: " & $oBinaryStream.Size & @CRLF)
                ;$oBinaryStream.SaveToFile("C:\documents and settings\pniven\desktop\" & $i & ".tmp" , $adSaveCreateOverWrite)
            
                ; Write the stream
                $oBinaryStream.Write($Data)

                ProgressSet((($Start + $w) / $FileSize) * 100, Round((($Start + $w) / $FileSize) * 100) & " %")
            Next
            
        Else
            ConsoleWrite("ERROR: " & $HTTPDownload.Status & " NOT Writing" & @CRLF)
        EndIf
        
    Next
        
    ProgressSet(100, "100 %")
    Sleep(500)
    ProgressOff()
    
    $oBinaryStream.SaveToFile($sFilename, $adSaveCreateOverWrite)

EndIf




Func _GetFileSize($inURL)
    
    $objHTTP = ObjCreate("MSXML2.ServerXMLHTTP.6.0")

    $objHTTP.open("HEAD", $inURL, false)

    $objHTTP.Send("")

    If $objHTTP.Status = 200 Then
        
        ;MsgBox(0,"",Round($objHTTP.GetResponseHeader("Content-Length")/1024/1024, 2) & " MB")
        Return $objHTTP.GetResponseHeader("Content-Length")
    Else
        Return -1
    EndIf
    
    
EndFunc

Thanks,

NiVZ

Edited by NiVZ
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...