使⽤curl断点续传下载⽂件
办公⽹络⽹速不是很好,使⽤Chrome下载⼀些软件时不时会中断,恶⼼的是Chrome居然不⽀持断点续传下载(),迅雷⾃然是不能装的,那怎么办?还好我有⼤名⿍⿍的,看官⽹的介绍,是不是有点6到没朋友 ~~
<
DICT, FILE, FTP, FTPS, GOPHER, GOPHERS, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, MQTT, POP3, POP3S, RTMP, RTMPS, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET and TFTP. curl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, HTTP/2, HTTP/3, cookies, user+password authentication
(Basic, Plain, Digest, CRAM-MD5, SCRAM-SHA, NTLM, Negotiate and Kerberos), file transfer resume, proxy tunneling and
more.
What's curl used for?
curl is used in command lines or scripts to transfer data. curl is also used in cars, television sets, routers, printers, audio
equipment, mobile phones, tablets, settop boxes, media players and is the Internet transfer engine for thousands of software
applications in over ten billion installations.
curl is used daily by virtually every Internet-using human on the globe.
⽽且 Windows 10 已经内置⽀持curl(),通过 PowerShell/CMD 可以直接使⽤,⼀⼝⽓上六楼,很⽅便 !
C:\>curl
curl: try 'curl --help' for more information
C:\>curl -V -v
curl 7.55.1 (Windows) libcurl/7.55.1 WinSSL
Release-Date: 2017-11-14, security patched: 2019-11-05
Protocols: dict file ftp ftps http https imap imaps pop3 pop3s smtp smtps telnet tftp
Features: AsynchDNS IPv6 Largefile SSPI Kerberos SPNEGO NTLM SSL
在命令⾏语法中,某些字符与格式有着特殊的意义与含义,⼀般的约定如下
<>:表⽰必选, 即:该参数、值或信息必传。
[]:表⽰可选,其内的参数、值或信息可传可不传。
… : 由三个句点组成的省略号表⽰"等等", 表⽰前述元素(参数、值或信息)可以重复多次。
{}:表⽰枚举,所传值必须在{}中给定的范围内。
|:管道符号(竖线)表⽰"或者",表⽰在⼀个集合范围中的⼀个选项。
/:同上,表⽰在⼀个集合范围中的⼀个选项。
斜体: 表⽰要提供的信息,要⽤该实际值来替换的选项或参数,该约定不常见。
再结合简单说下curl的基本使⽤,即curl [] <url>
通过curl --help列出所有命令(原列表较长,已⼤幅删减,若安装最新版则会更多,下⾯只列举了最常⽤的)
其中options(选项)由--加⼩写英⽂单词组成,如--help,也有少量缩写,如--ipv4
若英⽂单字超过两个,则⽤-连接,如--progress-bar
有的选项可以有参数,help⽂档⾥以<param>的形式展⽰,如--data <data>, --user <user:password>
部分常⽤选项有简写,由-加⼀个字符组成,如-d, -4, -#, -:等,需要注意的是,单个字母是区分⼤⼩写的,如-H(--head)和-h(--help)表⽰不同的含义
选项可以是1个到多个不等(最简单的curl <url>是curl --url <url>的缩写,所以⾄少有⼀个选项)
C:\>curl --help
Usage: curl [] <url>
-a, --append        Append to target file when uploading
--basic        Use HTTP Basic Authentication
-E, --cert <certificate[:password]> Client certificate file and password
-C, --continue-at <offset> Resumed transfer offset
-b, --cookie <data> Send cookies from string/file
-c, --cookie-jar <filename> Write cookies to <filename> after operation
--create-dirs  Create necessary local directory hierarchy
-d, --data <data>  HTTP POST data
--data-ascii <data> HTTP POST ASCII data
--data-binary <data> HTTP POST binary data
--data-raw <data> HTTP POST data, '@' allowed
--data-urlencode <data> HTTP POST data url encoded
-
f, --fail          Fail silently (no output at all) on HTTP errors
--fail-early    Fail on first transfer error, do not continue
-F, --form <name=content> Specify HTTP multipart POST data
--form-string <name=string> Specify HTTP multipart POST data
-G, --get          Put the post data in the URL and use GET
-I, --head          Show document info only
-H, --header <header/@file> Pass custom header(s) to server
-h, --help          This help text
-k, --insecure      Allow insecure server connections when using SSL
-i, --include      Include protocol response headers in the output
-4, --ipv4          Resolve names to IPv4 addresses
-
6, --ipv6          Resolve names to IPv6 addresses
-L, --location      Follow redirects
-:, --next          Make next URL use its separate set of options
-N, --no-buffer    Disable buffering of the output stream
--oauth2-bearer <token> OAuth 2 Bearer Token
-o, --output <file> Write to file instead of stdout
-#, --progress-bar  Display transfer progress as a bar
-x, --proxy [protocol://]host[:port] Use this proxy
-U, --proxy-user <user:password> Proxy user and password
-O, --remote-name  Write output to a file named as the remote file
--remote-name-all Use the remote file name for all URLs
-
-retry <num>  Retry request if transient problems occur
--retry-connrefused Retry on connection refused (use with --retry)
--retry-delay <seconds> Wait time between retries
--retry-max-time <seconds> Retry only within this period
-S, --show-error    Show error even when -s is used
-s, --silent        Silent mode
-Y, --speed-limit <speed> Stop transfers slower than this
-y, --speed-time <seconds> Trigger 'speed-limit' abort after this time
-t, --telnet-option <opt=val> Set telnet option
--trace <file>  Write a debug trace to FILE
--trace-ascii <file> Like --trace, but without hex output
-
-trace-time    Add time stamps to trace/verbose output
-T, --upload-file <file> Transfer local FILE to destination
--url <url>    URL to work with
-B, --use-ascii    Use ASCII/text transfer
-u, --user <user:password> Server user and password
-A, --user-agent <name> Send User-Agent <name> to server
-v, --verbose      Make the operation more talkative
-V, --version      Show version number and quit
⾔归正传,下⾯我们使⽤酷酷的curl下载⼀些⽂件(注:以下演⽰均在⾼速⽹络⚡下进⾏)
1. 最简单的使⽤curl --remote-name(-O) URL或者curl --output(-o) fileName URL,以下载最新64位为例
C:\>mkdir Software
C:\>cd Software
C:\Software>curl -O curl.se/windows/dl-7.77.0_2/curl-7.77.0_2-win64-mingw.zip
% Total    % Received % Xferd  Average Speed  Time    Time    Time  Current
Dload  Upload  Total  Spent    Left  Speed
100 5154k  100 5154k    0    0  5154k      0  0:00:01 --:--:--  0:00:01 6467k
2. 使⽤-continue-at(-C)断点续传下载,这次⽤PowerShell下载⼀个⼤⽂件(SSMS),关于该选项的使⽤,参见以下官⽅注解
-C, --continue-at
Continue/Resume a previous file transfer at the given offset. The given offset is the exact number of bytes that will be
skipped, counting from the beginning of the source file before it is transferred to the destination. If used with uploads, the
FTP server command SIZE will not be used by curl.
Use "-C -" to tell curl to automatically find out where/how to resume the transfer. It then uses the given output/input files to
figure that out.
If this option is used several times, the last one will be used.
在Powershell中使⽤需要带上后缀名,即,不然不能正确识别(会被替换成Invoke-WebRequest)
PS C:\Software> curl -C - -O download.microsoft/download/4/6/8/
Invoke-WebRequest : ⽆法处理参数,因为参数名称“C”具有⼆义性。可能的匹配项包括:  -Credential -CertificateThumbprint -Certificate -ContentType。
所在位置⾏:1 字符: 6
+ curl -C - -O download.microsoft/download/4/6/8/4681f3b2-f ...
+      ~~
+ CategoryInfo          : InvalidArgument: (:) [Invoke-WebRequest],ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameter,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
PS C:\Software> curl --continue-at - -O download.microsoft/download/4/6/8/ Invoke-WebRequest : 不到接受实际参数“-”的位置形式参数。
所在位置⾏:1 字符: 1
+ curl --continue-at - -O download.microsoft/download/4/6/8 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [Invoke-WebRequest],ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
通过关闭⽆线/有线⽹络,模拟⽹络不稳定的情况
PS C:\Software> -C - -O download.microsoft/download/4/6/8/
% Total    % Received % Xferd  Average Speed  Time    Time    Time  Current
Dload  Upload  Total  Spent    Left  Speed
16  565M  16 94.7M    0    0  4852k      0  0:01:59  0:00:20  0:01:39    0
curl: (56) Send failure: Connection was reset
PS C:\Software> -C - -O download.microsoft/download/4/6/8/
** Resuming transfer from byte position 172521856
% Total    % Received % Xferd  Average Speed  Time    Time    Time  Current
Dload  Upload  Total  Spent    Left  Speed
9  470M    9 46.9M    0    0  1502k      0  0:05:20  0:00:32  0:04:48 3720k
curl: (56) Send failure: Connection was reset
可以使⽤--retry <num>选项在下载失败后⾃动重试(此时⽹络没连接,在观察到重试现象后,打开⽹络连接),另外:请动⼿尝试前⽂curl --help列举的其他retry选项,这⾥不再逐⼀举例
PS C:\Software> -C - -O --retry 10 download.microsoft/download/4/6/8/ ** Resuming transfer from byte position 221771295
% Total    % Received % Xferd  Average Speed  Time    Time    Time  Current
Dload  Upload  Total  Spent    Left  Speed
0    0    0    0    0    0      0      0 --:--:-- --:--:-- --:--:--    0Warning: Transient problem: timeout Will retry in 1 seconds. 10 retries left.
0    0    0    0    0    0      0      0 --:--:-- --:--:-- --:--:--    0Warning: Transient problem: timeout Will retry in 2 seconds. 9 retries left.
0    0    0    0    0    0      0      0 --:--:-- --:--:-- --:--:--    0Warning: Transient problem: timeout Will retry in 4 seconds. 8 retries left.
0    0    0    0    0    0      0      0 --:--:-- --:--:-- --:--:--    0Warning: Transient problem: timeout Will retry in 8 seconds. 7 retries left.
0    0    0    0    0    0      0      0 --:--:-- --:--:-- --:--:--    0Warning: Transient problem: timeout Will retry in 16 seconds. 6 retries left.
100  423M  100  423M    0    0  4667k      0  0:01:33  0:01:33 --:--:-- 7028k
PS C:\Software>
下载完成后使⽤断点续传及重试选项,不会覆盖已下载的⽂件;反之,则不成⽴
PS C:\Software> -C - -O download.microsoft/download/4/6/8/
** Resuming transfer from byte position 666228072
% Total    % Received % Xferd  Average Speed  Time    Time    Time  Current
Dload  Upload  Total  Spent    Left  Speed
0  237    0    0    0    0      0      0 --:--:-- --:--:-- --:--:--    0
curl: (33) HTTP server doesn''t seem to support byte ranges. Cannot resume.
PS C:\Software> -C - -O --retry 3 download.microsoft/download/4/6/8/ ** Resuming transfer from byte position 666228072
% Total    % Received % Xferd  Average Speed  Time    Time    Time  Current
Dload  Upload  Total  Spent    Left  Speed
0  237    0    0    0    0      0      0 --:--:-- --:--:-- --:--:--    0
Warning: Transient problem: FTP error Will retry in 1 seconds. 3 retries left.
0  237    0    0    0    0      0      0 --:--:-- --:--:-- --:--:--    0
Warning: Transient problem: FTP error Will retry in 2 seconds. 2 retries left.
0  237    0    0    0    0      0      0 --:--:-- --:--:-- --:--:--    0
Warning: Transient problem: FTP error Will retry in 4 seconds. 1 retries left.
0  237    0    0    0    0      0      0 --:--:-- --:--:-- --:--:--    0
curl: (33) HTTP server doesn''t seem to support byte ranges. Cannot resume.
PS C:\Software> -O download.microsoft/download/4/6/8/
% Total    % Received % Xferd  Average Speed  Time    Time    Time  Current
Dload  Upload  Total  Spent    Left  Speed
100  635M  100  635M    0    0  16.2M      0  0:00:39  0:00:39 --:--:-- 24.2M
对于有些资源,⽹速不好的时候,甚⾄跳转到具体下载链接都很困难,以下载最新的 windows 64 位版本的 PostgreSQL 13.X为例,官⽹到下载资源列表,页⾯跳转后⼀直不开始下载,点击下载链接也没反应。。。
别捉急,curl⼀样可以帮到你。⿏标右键复制下载链接,使⽤--head(-I)或者--include(-i)选项(也可使⽤--verbose(-v)选项,这样,你可以看到所谓的TCP三次握⼿是怎样发⽣的 ),具体下载链接在Location⾸部字段⾥,然后使⽤上⾯提到的断点续传下载选项,DONE✌!
C:\>curl --head prisedb/getfile.jsp?fileid=1257713
HTTP/1.1 302 Found
Content-Type: text/html; charset=utf-8
Content-Length: 0
Connection: keep-alive
Server: nginx/1.16.1
Date: Sat, 10 Jul 2021 08:50:09 GMT
Location: prisedb/postgresql/
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
X-Cache: Miss from cloudfront
Via: 1.1 a43db2746d5ea9543e11897b6654f9b6.cloudfront (CloudFront)
X-Amz-Cf-Pop: HKG62-C1
X-Amz-Cf-Id: KuxMKejZWH-BMtFbzdKMCXsJE1ctLNMoSIcSYHLIkWQmbFnbexDsNg==
C:\>curl -v prisedb/getfile.jsp?fileid=1257713
*  Trying 13.32.
* TCP_NODELAY set
* Connected prisedb (13.32.53.65) port 443 (#0)
* schannel: SSL/TLS connection prisedb port 443 (step 1/3)
* schannel: checking server certificate revocation
* schannel: sending initial handshake data: sending
* schannel: sent initial handshake data: sent 191 bytes
* schannel: SSL/TLS connection prisedb port 443 (step 2/3)
* schannel: failed to receive handshake, need more data
* schannel: SSL/TLS connection prisedb port 443 (step 2/3)
* schannel: encrypted data got 4096
chrome直接下载
* schannel: encrypted data buffer: offset 4096 length 4096
* schannel: encrypted data length: 3996
* schannel: encrypted data buffer: offset 3996 length 4096
* schannel: received incomplete message, need more data
* schannel: SSL/TLS connection prisedb port 443 (step 2/3)
* schannel: encrypted data got 1024
* schannel: encrypted data buffer: offset 5020 length 5020
* schannel: encrypted data length: 164
* schannel: encrypted data buffer: offset 164 length 5020
* schannel: received incomplete message, need more data
* schannel: SSL/TLS connection prisedb port 443 (step 2/3)
* schannel: encrypted data got 150
* schannel: encrypted data buffer: offset 314 length 5020
* schannel: sending next handshake data: sending
* schannel: SSL/TLS connection prisedb port 443 (step 2/3)
* schannel: encrypted data got 170
* schannel: encrypted data buffer: offset 170 length 5020
* schannel: SSL/TLS handshake complete
* schannel: SSL/TLS connection prisedb port 443 (step 3/3)
* schannel: stored credential handle in session cache
...
写在最后,准备这篇随笔的时候,翻阅了不少资料,发现有两个资源是很不错的, ⼀个是官⽹的, 另⼀个是官⽹提到的,Everything curl另有, 到章节,⼀个感觉就是:我有必要写这篇博客吗?写个寂寞 。。。最后,附上, 原创不易,且⾏且珍惜 ~~
更新: 另有中⽂版图书《cURL必知必会》, 在可免费阅读。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。