I've introduced the power of VBScript language before. Today, we give you a VBS code that supports a breakpoint download.
and attached to the VBS code parsing, do not know friends can cooperate with Microsoft's Script56.chm document self-study. Very simple
The benefit of VBS is that the code is easy to understand. Basically every line of code execution function is expressed in English.
This code is also a supplement to the VBS download feature I described earlier.
As usual, the copy is saved as DL.VBE.
However, the code of this VBS differs not by double-clicking, but by executing it under the cmd command line.
The download function executes the format: Cscript.exe dl.vbs (destination file address)
[For example, the WIn32 version of Metasploit is shown below.] Enter in cmd: cscript.exe dl.vbs Http://spool.metasploit.com/releases/framework-3.2.exe]
36.7M file download took more than 7 minutes, and the Thunderbolt used 1 minute 50 seconds.
The code is as follows:
Copy Code code as follows:
if (LCase (wscript.fullname,11) = "Wscript.exe") Then ' judge the name of the script Host '
Die ("Script host must be CScript.exe.") ' The script host is not cscript, so it die '
End If
If Wscript.arguments.count<1 then ' must have at least one argument '
Die ("usage:cscript webdl.vbs URL [filename]") ' Though small spite, Usage can't forget '
End If
Url=wscript.arguments (0) ' parameter array subscript starting from 0 '
If Url= "" "Then Die (" url can ' t be null. ") "Dare to scare me, empty URL can not do"
If Wscript.arguments.count>1 then ' first determine if the number of parameters is greater than 1 '
Filename=wscript.arguments (1) ' re-access second parameter '
Else ' If the filename is not given, it is obtained from the URL '
T=instrrev (URL, "/") ' Get last '/' position '
If t=0 or T=len (URL) then die ("Can not get filename to save.") ' No '/' or '/' end '
Filename=right (Url,len (URL)-T) ' gets the filename to save '
End If
If not left (url,7) = "http://" then url= "http://" if Careless "&url" forget, plus '
Set Fso=wscript.createobject ("Scripting.FileSystemObject") ' fso,aso,http three objects can be less than one '
Set Aso=wscript.createobject ("ADODB. Stream ")
Set Http=wscript.createobject ("Microsoft.XMLHTTP")
If fso.fileexists (filename) Then ' Determine if the file being downloaded already exists '
Start=fso.getfile (filename). Size ' exists with the current file size as the starting position '
Else
Start=0 ' not exist, everything starts from scratch '
Fso.createtextfile (filename). Close ' New file '
End If
Wscript.stdout.write "Connectting ..." ' The show just started '
Current=start ' current position as starting position '
Todo
Http.open "Get", url,true ' here asynchronously calls HTTP '
Http.setrequestheader "Range", "bytes=" &start& "-" &cstr (start+20480) "The mystery of the continuation of the breakpoint is here '
Http.setrequestheader "Content-type:", "Application/octet-stream"
Http.send ' constructs the packet and starts sending '
For I=1 to 120 ' Loop wait '
If Http.readystate=3 then Showplan () ' State 3 means start receiving data, show progress '
If http.readystate=4 then exit for ' state 4 indicates data acceptance completed '
Wscript.Sleep 500 ' Wait 500ms '
Next
If not http.readystate=4 then die ("Timeout.") ' 1 minutes not finished 20k? Timeout! '
If http.status>299 then die ("Error:" &http.status& "&http.statustext)" Is it wrong? '
If not http.status=206 then die ("Server not Support Partial Content.") ' Server does not support breakpoint continuation '
Aso.type=1 ' data flow type set to Byte '
Aso.open
Aso.loadfromfile filename ' Open file '
Aso.position=start ' Set the initial position of the file pointer '
Aso.write http.responsebody ' Write Data '
Aso.savetofile filename,2 ' overwrite save '
Aso.close
Range=http.getresponseheader ("Content-range") ' Gets the "content-range" in the HTTP header
If range= "" "Then Die (" Can not get range. ") ' Without it, I don't know if the download is over.
Temp=mid (RANGE,INSTR (range, "-") +1) ' Content-range is similar to 123-456/789 '
CURRENT=CLNG (Left (temp,instr (temp, "/")-1)) ' 123 is the starting position, 456 is the end position '
TOTAL=CLNG Mid (Temp,instr (temp, "/") +1)) ' 789 is the total number of files bytes '
If Total-current=1 then exit Do ' end position 1 less than total size means the transfer is complete '
start=start+20480 ' Otherwise download 20k '
Loop while True
WScript.Echo Chr & "Download (" &total& ") done." ' Download finished, show total bytes '
Function Die (msg) ' Functions name from Perl built-in functions die '
WScript.Echo msg ' confessed ^_^ '
Wscript.Quit ' to see Max '
End Function
Function showplan () ' Show Download Progress '
If I mod 3 = 0 Then c= "/" ' simple dynamic effect '
If I mod 3 = 1 then c= "-"
If I mod 3 = 2 then c= "\"
Wscript.stdout.write CHR & "Download (" ¤t& ")" &C&CHR (8) ' 13th "ASCII code is back to the beginning, 8th is backspace '
End Function
The above is the complete use of VBS to support the breakpoint of the download code, very suitable for the company to prohibit the use of Xunlei, flashget situation. Just speed is a problem. Need to be perfected to multi-threaded downloads.