Linux 系统curl命令使用详解

curl 是一个强大的命令行工具,用于在 Linux 系统中进行数据传输。它支持多种协议,包括 HTTP、HTTPS、FTP 等,用于下载或上传数据,执行 Web 请求等。

curl 命令的常见用法和解释:

1. 发送 HTTP GET 请求:

curl http://example.com

这会向指定的 URL 发送 HTTP GET 请求并将响应输出到终端。

2. 发送 HTTP POST 请求:

curl -X POST -d "param1=value1¶m2=value2" http://example.com

通过 -X POST 指定请求方法,-d 选项用于传递 POST 数据。

3. 下载文件:

curl -O http://example.com/file.zip

使用 -O 选项将远程文件下载到本地,文件名与远程文件相同。

4. 上传文件:

curl -F "file=@localfile.txt" http://example.com/upload

使用 -F 选项将本地文件上传到指定 URL。

5. 自定义请求头:

curl -H "Authorization: Bearer token" http://example.com/api/resource

使用 -H 选项可以设置自定义请求头。

6. 使用代理:

curl -x proxy_server:port http://example.com

-x 选项可用于指定代理服务器。

7. 保存响应到文件:

curl -o output.txt http://example.com

使用 -o 选项将响应保存到本地文件 output.txt。

8. 链接超时和最大执行时间:

curl --connect-timeout 10 --max-time 60 http://example.com

使用 --connect-timeout 设置连接超时时间,--max-time 设置最大执行时间。

9. 使用基本认证:

curl -u username:password http://example.com

使用 -u 选项进行 HTTP 基本认证。

10. 下载文件并显示下载进度:

curl -# -O http://example.com/largefile.zip

-# 选项会显示下载进度条。

11. 传递 JSON 数据:

curl -H "Content-Type: application/json" -d '{"key": "value"}' http://example.com/api/resource

使用 -H 设置请求头为 JSON 类型,-d 选项传递 JSON 数据。

12. 限速下载速度:

curl --limit-rate 100k -O http://example.com/largefile.zip

使用 --limit-rate 选项限制下载速度,此示例限制下载速度为100KB/s。

13. 重试连接:

curl --retry 3 http://example.com

使用 --retry 选项在连接失败时重试请求,此示例最多重试3次。

14. 下载文件时显示文件大小:

curl -I -s -X HEAD http://example.com/largefile.zip | grep Content-Length | awk '{print $2}'

使用 -I 发送 HTTP HEAD 请求以获取文件大小。

15. 处理重定向:

curl -L http://example.com

使用 -L 选项自动处理HTTP重定向。

16. 批量下载:

curl -O http://example.com/{file1,file2,file3}.txt

使用大括号扩展来批量下载多个文件。

17. 使用Cookie:

curl --cookie "session=abcdef123456" http://example.com

使用 --cookie 选项发送Cookie。

18. 发送表单数据:

curl -d "username=user&password=pass" -X POST http://example.com/login

使用 -d 发送表单数据。

19. 查看HTTP请求头:

curl -I http://example.com

使用 -I 查看HTTP响应头。

20. 使用多线程下载:

curl -O http://example.com/largefile.zip --parallel

使用 --parallel 启用多线程下载。


21. 使用代理服务器:

curl -x http://proxyserver:port http://example.com

使用 -x 选项指定代理服务器地址和端口。

22. 上传文件:

curl -F "file=@localfile.txt" http://example.com/upload

使用 -F 选项上传本地文件到远程服务器。

23. 自定义请求方法:

curl -X PUT http://example.com/resource

使用 -X 选项指定自定义HTTP请求方法,如PUT、DELETE等。

24. HTTP Basic认证:

curl -u username:password http://example.com

使用 -u 选项进行HTTP基本认证。

25. 显示HTTP请求和响应信息:

curl -v http://example.com

使用 -v 选项显示HTTP请求和响应的详细信息。

26. 保存响应到文件:

curl -o output.html http://example.com

使用 -o 选项将HTTP响应保存到本地文件。

27. 带有HTTP Header的请求:

curl -H "Accept: application/json" -H "Authorization: Bearer token" http://example.com/api/resource

使用多个 -H 选项添加自定义HTTP头。

28. 跳过SSL证书验证:

curl -k https://example.com

使用 -k 选项跳过SSL证书验证。

29. 使用socks5代理:

curl --socks5 host:port http://example.com

使用 --socks5 选项连接到socks5代理服务器。

30. 使用连接超时:

curl --connect-timeout 10 http://example.com

使用 --connect-timeout 选项设置连接超时时间。

31. 限制最大重定向次数:

curl --max-redirs 3 http://example.com

使用 --max-redirs 选项限制最大重定向次数。

展开阅读全文

页面更新:2024-02-19

标签:文件大小   表单   示例   批量   详解   选项   命令   上传   本地文件   文件   方法   数据   系统

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top