最全的cURL命令使用
cURL是什么
curl是Linux命令行工具,可以使用任何可支持的协议(如HTTP、FTP、IMAP、POP3、SCP、SFTP、SMTP、TFTP、TELNET、LDAP或FILE)在服务器之间传输数据。
在Linux下,curl是由libcurl 提供驱动。在此驱动下,curl可以一次传输多个文件。
curl由libcurl支持所有与传输相关的特性
cURL常用参数
| 参数 | 说明 |
|---|---|
| -i | 默认隐藏响应头,此选项打印响应头与 |
| -I/--head | 仅显示响应头 |
| -o | 将相应内容保存指定路径下 |
| -O | 将相应内容保存在当前工作目录下 |
| -C | 断点续传,在 crtl + c终端后,可以从中断后部分开始 |
| -v | 显示请求头与响应头 |
| -x | 使用代理 |
| -X | 指定请求方法,POST GET PUT DELETE等 |
| -d | 如GET/POST/PUT/DELETE 需要传的表单参数,如JSON格式 |
| -u username:password | 当使用ftp有用户名可以使用-u,ftp允许匿名用户访问可以忽略 |
| –-limit-rate 2000B | 限速 |
| -T/--upload-file <file> | 上传一个文件 |
| -c/--cookie-jar <file name> | 将cookie下载到文件内 |
| -k/--insecure | 允许执行不安全的ssl连接 |
--header 'Host: targetapplication.com' |
使用请求头 |
| -L/--location | 接受服务端redirect的请求 |
下面整理了一些常用语法使用格式
cURL使用案例
限制下载速率
curl --limit-rate 100K http://yourdomain.com/yourfile.tar.gz -O
使用代理访问
curl --proxy yourproxy:port https://yoururl.com
限速访问
curl www.baidu.com --limit-rate 1k
存储cookie和使用cookie
[root@VM-0-2-centos ~]# curl --cookie-jar cnncookies.txt https://www.baidu.com/index.html -O -s -v
* About to connect() to www.baidu.com port 443 (#0)
* Trying 14.215.177.39...
* Connected to www.baidu.com (14.215.177.39) port 443 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
* CAfile: /etc/pki/tls/certs/ca-bundle.crt
CApath: none
* SSL connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
* Server certificate:
* subject: CN=baidu.com,O="Beijing Baidu Netcom Science Technology Co., Ltd",OU=service operation department,L=beijing,ST=beijing,C=CN
* start date: Apr 02 07:04:58 2020 GMT
* expire date: Jul 26 05:31:02 2021 GMT
* common name: baidu.com
* issuer: CN=GlobalSign Organization Validation CA - SHA256 - G2,O=GlobalSign nv-sa,C=BE
> GET /index.html HTTP/1.1
> User-Agent: curl/7.29.0
> Host: www.baidu.com
> Accept: */*
>
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
< Connection: keep-alive
< Content-Length: 2443
< Content-Type: text/html
< Date: Wed, 26 May 2021 12:14:41 GMT
< Etag: "58860402-98b"
< Last-Modified: Mon, 23 Jan 2017 13:24:18 GMT
< Pragma: no-cache
< Server: bfe/1.0.8.18
* Added cookie BDORZ="27315" for domain baidu.com, path /, expire 1622117681
< Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
<
{ [data not shown]
* Connection #0 to host www.baidu.com left intact
# Netscape HTTP Cookie File
# http://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.
.baidu.com TRUE / FALSE 1622117681 BDORZ 27315
[root@VM-0-2-centos ~]# curl --cookie cnncookies.txt https://www.baidu.com -s -v -o /dev/null
* About to connect() to www.baidu.com port 443 (#0)
* Trying 14.215.177.39...
* Connected to www.baidu.com (14.215.177.39) port 443 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
* CAfile: /etc/pki/tls/certs/ca-bundle.crt
CApath: none
* SSL connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
* Server certificate:
* subject: CN=baidu.com,O="Beijing Baidu Netcom Science Technology Co., Ltd",OU=service operation department,L=beijing,ST=beijing,C=CN
* start date: Apr 02 07:04:58 2020 GMT
* expire date: Jul 26 05:31:02 2021 GMT
* common name: baidu.com
* issuer: CN=GlobalSign Organization Validation CA - SHA256 - G2,O=GlobalSign nv-sa,C=BE
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: www.baidu.com
> Accept: */*
> Cookie: BDORZ=27315
>
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
< Connection: keep-alive
< Content-Length: 2443
< Content-Type: text/html
< Date: Wed, 26 May 2021 12:23:27 GMT
< Etag: "58860402-98b"
< Last-Modified: Mon, 23 Jan 2017 13:24:18 GMT
< Pragma: no-cache
< Server: bfe/1.0.8.18
* Replaced cookie BDORZ="27315" for domain baidu.com, path /, expire 1622118207
< Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/ # 这里可以看到设置的cookie
<
{ [data not shown]
* Connection #0 to host www.baidu.com left intact
使用application/x-www-form-urlencoded表单类型
这里使用的为application/x-www-form-urlencoded
curl -d "option=value&something=anothervalue" -X POST https://{hostname}/
使用json格式作为body
curl -H "Content-Type: application/json" -X POST https://host.com/ \
-d '
{
"option": "value",
"something": "anothervalue"
}'
使用curl 上传文件
curl {host}/api/v1/upimg -F "file=@/Users/fungleo/Downloads/401.png" \
-H "token: 222" \
-v
仅显示状态码
curl输出的格式变量
curl -w参数提供了一些格式变量,可以达到紧紧获取某些数据
仅获取http状态码
curl -w %{http_code} www.baidu.com -o /dev/null -s
获取整个请求的事件
获取整个请求的耗时,单位秒,显示单位 毫秒
curl -w %{time_total} www.baidu.com -o /dev/null -s
获取域名解析时间
curl -w %{time_namelookup} www.baidu.com -o /dev/null -s
获取TCP连接耗时
curl -w %{time_connect} www.baidu.com -o /dev/null -s
获取SSL/SSH握手到远程主机耗时
curl -w %{time_appconnect} https://www.baidu.com -o /dev/null -s -v
获取所有重定向的耗时
这里是从查找、连接、传输整个事务的完成到开始传送数据之前的耗时
curl -w %{time_redirect} www.baidu.com -o /dev/null -s
获得下载的总字节数
这里是http相应的body长度,而不是加上头部的大小
curl -w %{size_download} www.baidu.com -o /dev/null -s
[root@VM-0-2-centos ~]# curl -w %{size_download} www.baidu.com -o /dev/null -s
2381
[root@VM-0-2-centos ~]# cwww.baidu.com -s|wc l -s
2 159 2381
获得请求体送字节数
curl -w %{size_request} www.baidu.com -o /dev/null -s
获得传输中的连接数
curl -w %{num_connects} www.baidu.com -o /dev/null -s
获得重定向次数
curl -w %{num_redirects} www.360buy.com -o /dev/null -s -L
获得SSL验证结果
0 表示是成功的
curl -w %{ssl_verify_result} https://www.baidu.com -o /dev/null -s -L
获得重定向的地址
当没有指定-L时,会返回被重定向后的地址
curl -w %{redirect_url} https://www.360buy.com -o /dev/null -s
获得上传和下载速度
curl -w %{speed_download} https://www.360buy.com -o /dev/null -s
curl -w %{speed_upload} https://www.360buy.com -o /dev/null -s
根据自己需要拼接特定格式
curl -w "总共请求时长:%{time_total}\n总跳转次数:%{num_redirects}\n" \
www.360buy.com \
-o /dev/null -s \
-L
总共请求时长:1.338
总跳转次数:3
最全的cURL命令使用的更多相关文章
- curl命令大全
curl 命令行工具的使用及命令参数说明 curl是一个开源的用于数据传输的命令行工具与库,它使用URL语法格式,支持众多传输协议,包括:HTTP.HTTPS.FTP.FTPS.GOPHER.TFTP ...
- Curl命令、Elinks命令、lynx命令、Wget命令、lftp命令
一.Curl命令 语法 curl(选项)(参数) 选项 -a/--append 上传文件时,附加到目标文件 -A/--user-agent <string> 设置用户代理发送给服务器 -a ...
- curl命令使用
curl命令可以用来构造http请求.参数有很多,常用的参数如下: 通用语法:curl [option] [URL...]在处理URL时其支持类型于SHELL的名称扩展功能,如http://www.j ...
- curl命令行使用
curl 命令使用 原文地址:http://blog.sina.com.cn/s/blog_4b9eab320100slyw.html 可以看作命令行浏览器 1.开启gzip请求curl -I h ...
- CURL命令报错:dyld: lazy symbol binding failed: Symbol not found: _SSL_load_error_strings解决办法
Mac OS X 10.11.6, curl 命令报错,错误如下: dyld: lazy symbol binding failed: Symbol not found: _SSL_load_erro ...
- CURL命令详解
curl命令是一个强大的网络工具,它能通过http,ftp等方式下载.上传文件.其中curl远不止这些功能,大家可以通过阅读手册获取更多的信息,类似的工具还有wget. curl命令使用了libcur ...
- limux curl命令
linux curl命令很强大: http://blog.chinaunix.net/uid-14735472-id-3413867.html curl是一种命令行工具,作用是发出网络请求,然后得到和 ...
- linux curl 命令详解,以及实例
linux curl是一个利用URL规则在命令行下工作的文件传输工具.它支持文件的上传和下载,所以是综合传输工具,但按传统,习惯称url为下载工具. 一,curl命令参数,有好多我没有用过,也不知道翻 ...
- 使用curl命令操作elasticsearch
使用curl命令操作elasticsearch 大岩不灿 发表于 2015年4月25日 浏览 7,426 次 第一:_cat系列_cat系列提供了一系列查询elasticsearch集群状态的接口.你 ...
随机推荐
- Java例题_31 逆序输出数组的值
1 /*31 [程序 31 数组逆序] 2 题目:将一个数组逆序输出. 3 程序分析:用第一个与最后一个交换. 4 */ 5 6 /*分析 7 * 第一种方法:找到这个数组的中间下标,然后交换两端的数 ...
- SpringBoot-11 扩展功能
SpringBoot-11 扩展功能 异步 同步就是一个任务的完成需要依赖另外一个任务时,只有等待被依赖的任务完成后,依赖的任务才能算完成,这是一种可靠的任务序列.要么成功都成功,失败都失败,两个任务 ...
- 面试官:聊一聊SpringBoot服务监控机制
目录 前言 SpringBoot 监控 HTTP Endpoints 监控 内置端点 health 端点 loggers 端点 metrics 端点 自定义监控端点 自定义监控端点常用注解 来,一起写 ...
- [DFS]特殊的质数肋骨
特殊的质数肋骨 时间限制:1000MS----内存限制:256000KB 题目描述 农民约翰母牛总是产生最好的肋骨. 你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们. 农民约翰确定他卖给买 ...
- C# Linq 延迟查询的执行
在定义linq查询表达式时,查询是不会执行,查询会在迭代数据项时运行.它使用yield return 语句返回谓词为true的元素. var names = new List<string> ...
- 《深入理解计算机系统》学习笔记整理(CSAPP 学习笔记)
简介 本笔记目前已包含 CSAPP 中除第四章(处理器部分)外的其他各章节,但部分章节的笔记尚未整理完全.未整理完成的部分包括:ch3.ch11.ch12 的后面几小节:ch5 的大部分. 我在整理笔 ...
- VsCode调试vue项目
VsCode调试vue项目 VsCode如何调试vue项目,VsCode需要安装插件以及配置launch.json文件. 找到"扩展"或者按快捷键"Ctrl+Shift+ ...
- ASP程序写的项目与微信服务号(公众号)完美结合。仅需一个DLL组建WeixinDLL
因ASP程序开发有很多优点,早年间ASP风靡全球,因此如今还在继续运营的ASP开发的项目仍在运行着,但是随着社交网络不断发达,特别是微信支付.微信通讯.小程序等的出现,导致很多ASP项目对接起来就比较 ...
- 【SpringBoot】Springboot2.x整合Redis(一)
备注: springboto整合redis依赖于spring-boot-starter-data-redis这个jar 一,项目环境和依赖 1.POM.xml配置 <parent> < ...
- 逆向工程第005篇:跨越CM4验证机制的鸿沟(下)
一.前言 本文是逆向分析CM4系列的最后一篇,我会将该游戏的序列号验证机制分析完毕,进而编写出注册码生成器. 二.分析第二个验证循环 延续上一篇文章的内容,来到如下代码处: 图1 上述代码并没有特别需 ...