在Windows命令提示符(CMD)或PowerShell中使用`curl`下载文件有多种方法和技巧,以下是详细说明及扩展内容:
1. 基本下载命令
Windows 10及更高版本内置了`curl`工具(基于Libcurl),基本语法与Linux类似:
cmd
curl -O https://example.com/file.zip
`-O`(大写字母O)表示将文件保存到当前目录,使用服务器上的原始文件名。
若需自定义本地文件名,改用 `-o`(小写字母o)并指定名称:
cmd
curl -o local_name.zip https://example.com/file.zip
2. 处理HTTPS证书问题
若遇到SSL证书错误,可添加`-k`或`--insecure`跳过验证(非安全场景使用):
cmd
curl -kO https://example.com/file.zip
3. 断点续传与大文件下载
使用`-C -`启用断点续传:
cmd
curl -C - -O https://example.com/large_file.iso
配合`--limit-rate`限制下载速度(如限制为1MB/s):
cmd
curl --limit-rate 1M -O https://example.com/file.zip
4. 静默模式和进度显示
`-s`参数隐藏进度信息,`-S`在出错时显示错误:
cmd
curl -sSO https://example.com/file.zip
`-#`显示简单的进度条:
cmd
curl -# -O https://example.com/file.zip
5. 代理设置
通过`-x`或`--proxy`指定代理服务器:
cmd
curl -x http://proxy-server:8080 -O https://example.com/file.zip
6. HTTP头部与认证
自定义请求头(如模拟浏览器):
cmd
curl -H "User-Agent: Mozilla/5.0" -O https://example.com/file.zip
Basic认证(用户名密码):
cmd
curl -u username:password -O https://example.com/protected/file.zip
7. 批量下载
使用`-Z`配合`-O`实现并行下载多个文件:
cmd
curl -Z -O https://example.com/file1.zip -O https://example.com/file2.zip
8. 与PowerShell的结合
在PowerShell中,`curl`是`Invoke-WebRequest`的别名。若要使用原生`curl`,需使用`curl.exe`:
powershell
curl.exe -O https://example.com/file.zip
扩展知识:
`wget`替代方案:Windows版`wget`(如通过Chocolatey安装)提供更简单的下载语法。
环境变量配置:若`curl`未识别,检查系统环境变量`PATH`是否包含`curl`路径(通常位于`C:\Windows\System32`)。
版本差异:旧版Windows可能需手动安装`curl`(如通过[Curl官方二进制包](https://curl.se/windows/))。
API调用:`curl`可用于测试REST API,例如发送POST请求:
cmd
curl -X POST -H "Content-Type: application/json" -d "{\"key\":\"value\"}" https://api.example.com
掌握这些方法可覆盖绝大多数文件下载和网络请求需求。