curl是一个利用URL语法在命令行下工作的文件传输工具,作用是发出网络请求,然后获取数据;它支持文件的上传和下载;支持多种通信协议。

一、查看网页源码

直接在 curl 命令后加上网址,默认会发送 GET 请求来获取链接内容到标准输出

curl www.jollypay.com

二、显示http头信息

-i 参数可以显示 http response 的头信息,连同网页源码一起。-I 参数则只显示 http response 的头信息。

curl -i http://www.codebelief.com 

HTTP/1.1 200 OK
Server: nginx/1.10.3
Date: Thu, 11 May 2017 08:25:46 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 24206
Connection: keep-alive
X-Powered-By: Express
Cache-Control: public, max-age=0
ETag: W/"5e8e-Yw5ZdnVVly9/aEnMX7fVXQ"
Vary: Accept-Encoding <!DOCTYPE html>
<html lang="en">
......
</html>
curl -I http://www.codebelief.com 

HTTP/1.1 200 OK
Server: nginx/1.10.3
Date: Thu, 11 May 2017 08:24:45 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 24206
Connection: keep-alive
X-Powered-By: Express
Cache-Control: public, max-age=0
ETag: W/"5e8e-Yw5ZdnVVly9/aEnMX7fVXQ"
Vary: Accept-Encoding

三、显示通信过程

-v 参数可以显示一次 http 通信的整个过程,包括端口连接和 http request 头信息。

四、保存下载网页

1、使用linux的重定向功能保存

# curl http://www.linux.com >> linux.html

2、可以使用curl的内置option:-o(小写)保存网页,文件名为自定义

curl -o home.html  http://www.sina.com.cn 

3、用-O(大写的),后面的url要具体到某个真实存在的文件

curl -O http://www.linux.com/hello.sh

4、通过ftp下载文件

curl -O -u 用户名:密码 ftp://www.linux.com/dodo1.JPG

5、下载多个文件

curl -O http://www.codebelief.com/page/2/ -O http://www.codebelief.com/page/3/
curl -O http://www.linux.com/dodo[1-5].JPG

五、上传文件

curl不仅仅可以下载文件,还可以上传文件。通过内置option:-T来实现

curl -T dodo1.JPG -u 用户名:密码 ftp://www.linux.com/img/

六、使用-L跟随链接重定向

如果直接使用 curl 打开某些被重定向后的链接,这种情况下就无法获取我们想要的网页内容。例如:

curl  www.jollypay.com

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<h1>301 Moved Permanently</h1>
<p>The requested resource has been assigned a new permanent URI.</p>
<hr/>Powered by Tengine</body>
</html>

301即为重定向,此时我们想要 curl 做的,就是像浏览器一样跟随链接的跳转,获取最终的网页内容。我们可以在命令中添加 -L 选项来跟随链接重定向:

curl  -L www.jollypay.com

七、使用 -A 自定义 User-Agent

我们可以使用 -A 来自定义用户代理,例如下面的命令将伪装成安卓火狐浏览器对网页进行请求:

curl -A “Mozilla/5.0 (Android; Mobile; rv:35.0) Gecko/35.0 Firefox/35.0” http://www.baidu.com 

这个例子,我们使用 -H 可以实现同样的目的。

八、使用 -H 自定义 header

当我们需要传递特定的 header 的时候,可以仿照以下命令来写: 
curl -H “Referer: www.example.com” -H “User-Agent: Custom-User-Agent” http://www.baidu.com 
可以看到,当我们使用 -H 来自定义 User-Agent 时,需要使用 “User-Agent: xxx” 的格式。

我们能够直接在 header 中传递 Cookie,格式与上面的例子一样: 
curl -H “Cookie: JSESSIONID=D0112A5063D938586B659EF8F939BE24” http://www.example.com

九、 使用 -c 保存 Cookie

当我们使用 cURL 访问页面的时候,默认是不会保存 Cookie 的。有些情况下我们希望保存 Cookie 以便下次访问时使用。例如登陆了某个网站,我们希望再次访问该网站时保持登陆的状态,这时就可以现将登陆时的 Cookie 保存起来,下次访问时再读取。

-c 后面跟上要保存的文件名。

curl -c “cookie-example” http://www.example.com

十、使用 -b 读取 Cookie

前面讲到了使用 -H 来发送 Cookie 的方法,这种方式是直接将 Cookie 字符串写在命令中。如果使用 -b 来自定义 Cookie,命令如下:

curl -b “JSESSIONID=D0112A5063D938586B659EF8F939BE24” http://www.example.com 

如果要从文件中读取 Cookie,-H 就无能为力了,此时可以使用 -b 来达到这一目的:

curl -b “cookie-example” http://www.example.com 

即 -b 后面既可以是 Cookie 字符串,也可以是保存了 Cookie 的文件名。

十一、 使用 -d 发送 POST 请求

curl默认使用get请求的方式。

我们以登陆网页为例来进行说明使用 curl 发送 POST 请求的方法。假设有一个登录页面 www.example.com/login,只需要提交用户名和密码便可登录。我们可以使用 curl来完成这一 POST 请求,-d 用于指定发送的数据,-X 用于指定发送数据的方式:

curl -d “userName=tom&passwd=123456” -X POST http://www.example.com/login

在使用 -d 的情况下,如果省略 -X,则默认为 POST 方式: 与上面等效

curl -d “userName=tom&passwd=123456” http://www.example.com/login

使用-d发送数据时,如果想强制使用 GET 方式:

curl -d “somedata” -X GET http://www.example.com/api

或者使用 -G 选项:

curl -d “somedata” -G http://www.example.com/api

从文件中读取要发送的数据:

curl -d “@data.txt” http://www.example.com/login

带 Cookie 登录 
当然,如果我们再次访问该网站,仍然会变成未登录的状态。我们可以用之前提到的方法保存 Cookie,在每次访问网站时都带上该 Cookie 以保持登录状态。

curl -c “cookie-login” -d “userName=tom&passwd=123456” http://www.example.com/login 

再次访问该网站时,使用以下命令:

curl -b “cookie-login” http://www.example.com/login 

这样,就能保持访问的是登录后的页面了。

十二、测试网页返回值

-w,–write-out参数:用于在一次完整且成功的操作后输出指定格式的内容到标准输出。

输出格式由普通字符串和任意数量的变量组成,输出变量需要按照%{variable_name}的格式,如果需要输出%,double一下即可,即%%,同时,\n是换行,\r是回车,\t是TAB。curl会用合适的值来替代输出格式中的变量,所有可用变量如下:

url_effective 最终获取的url地址,尤其是当你指定给curl的地址存在301跳转,且通过-L继续追踪的情形。

http_code http状态码,如200成功,301转向,404未找到,500服务器错误等。(The numerical response code that was found in the last retrieved HTTP(S) or FTP(s) transfer. In 7.18.2 the alias response_code was added to show the same info.)

http_connect The numerical code that was found in the last response (from a proxy) to a curl CONNECT request. (Added in 7.12.4)

time_total 总时间,按秒计。精确到小数点后三位。 (The total time, in seconds, that the full operation lasted. The time will be displayed with millisecond resolution.)

time_namelookup DNS解析时间,从请求开始到DNS解析完毕所用时间。(The time, in seconds, it took from the start until the name resolving was completed.)

time_connect 连接时间,从开始到建立TCP连接完成所用时间,包括前边DNS解析时间,如果需要单纯的得到连接时间,用这个time_connect时间减去前边time_namelookup时间。以下同理,不再赘述。(The time, in seconds, it took from the start until the TCP connect to the remote host (or proxy) was completed.)

time_appconnect 连接建立完成时间,如SSL/SSH等建立连接或者完成三次握手时间。(The time, in seconds, it took from the start until the SSL/SSH/etc connect/handshake to the remote host was completed. (Added in 7.19.0))

time_pretransfer 从开始到准备传输的时间。(The time, in seconds, it took from the start until the file transfer was just about to begin. This includes all pre-transfer commands and negotiations that are specific to the particular protocol(s) involved.)

time_redirect 重定向时间,包括到最后一次传输前的几次重定向的DNS解析,连接,预传输,传输时间。(The time, in seconds, it took for all redirection steps include name lookup, connect, pretransfer and transfer before the final transaction was started. time_redirect shows the complete execution time for multiple redirections. (Added in 7.12.3))

time_starttransfer 开始传输时间。在发出请求之后,Web 服务器返回数据的第一个字节所用的时间(The time, in seconds, it took from the start until the first byte was just about to be transferred. This includes time_pretransfer and also the time the server needed to calculate the result.)

size_download 下载大小。(The total amount of bytes that were downloaded.)

size_upload 上传大小。(The total amount of bytes that were uploaded.)

size_header  下载的header的大小(The total amount of bytes of the downloaded headers.)

size_request 请求的大小。(The total amount of bytes that were sent in the HTTP request.)

speed_download 下载速度,单位-字节每秒。(The average download speed that curl measured for the complete download. Bytes per second.)

speed_upload 上传速度,单位-字节每秒。(The average upload speed that curl measured for the complete upload. Bytes per second.)

content_type 就是content-Type,不用多说了,这是一个访问我博客首页返回的结果示例(text/html; charset=UTF-8);(The Content-Type of the requested document, if there was any.)

num_connects Number of new connects made in the recent transfer. (Added in 7.12.3)

num_redirects Number of redirects that were followed in the request. (Added in 7.12.3)

redirect_url When a HTTP request was made without -L to follow redirects, this variable will show the actual URL a redirect would take you to. (Added in 7.18.2)

ftp_entry_path The initial path libcurl ended up in when logging on to the remote FTP server. (Added in 7.15.4)

ssl_verify_result ssl认证结果,返回0表示认证成功。( The result of the SSL peer certificate verification that was requested. 0 means the verification was successful. (Added in 7.19.0))

以下是诊断服务器到sts渠道的网络访问情况

curl -w "  time_namelookup:  %{time_namelookup}s\n  time_connect:  %{time_connect}s\n  time_appconnect:  %{time_appconnect}s\n  time_pretransfer:  %{tme_pretransfer}s\n  tme_redirect:  %{time_redirect}s\n time_starttransfer:  %{time_starttransfer}s\n  time_total:  %{time_total}s\n " -o /dev/null -s https://srtest.stspayone.com
-o /dev/null:把curl 返回的内容写到垃圾回收站即/dev/null
-s 不显示统计信息(不加的话会有一个百分比的下载进度信息)
 
结果如下:
 time_namelookup:  0.026s
time_connect: 0.127s
time_appconnect: 0.404s
time_pretransfer: 0.404s
tme_redirect: 0.000s
time_starttransfer: 1.373s
time_total: 1.373s

以上显示网络连接时间为0.127秒(其中DNS解析为0.026秒),总体请求1.373秒。

参考:

https://blog.csdn.net/weifangan/article/details/80741981

https://www.cnblogs.com/chenxiaomeng/p/10470481.html

linux之curl工具的更多相关文章

  1. [Linux][转载]Curl命令详解

    命令:curl 在Linux中curl是一个利用URL规则在命令行下工作的文件传输工具,是一款很强大的http命令行工具,当处在无界面的服务器上的时候,利用curl下载上传文件是较为方便的事情. 语法 ...

  2. curl工具

    在Linux中curl是一个利用URL规则在命令行下工作的文件传输工具,可以说是一款很强大的http命令行工具.它支持文件的上传和下载,是综合传输工具,但按传统,习惯称url为下载工具. 用法: cu ...

  3. Linux中curl的用法

    一.简介:在Linux中curl是一个利用URL规则在命令行下工作的文件传输工具,是一款强大的http命令行工具.支持文件的上传和下载,是综合传输工具. 二.语法:curl [option] [url ...

  4. linux多线程下载工具mwget

    linux多线程下载工具mwget 经常使用wget进行文件下载,然而wget的处理速度并不如人意.遇到一些国外的站点,经常慢得像蜗牛一般.然而为了解决这个问题,便有了mwget:m表示multi多线 ...

  5. 工作常用的linux/mysql/php/工具命令

    工作常用的linux/mysql/php/工具命令: 1. tar备份目录 tar zcvf ****.tar.gz ****/ tar 备份跳过目录 tar --exclude=test1 3. s ...

  6. 20个linux命令行工具监视性能(下)

    昨天晚上第一次翻译了<20 Command Line Tools to Monitor Linux Performance>中的前十个命令,翻译得不是很好,今天晚上继续把后面的十个也翻译给 ...

  7. linux自动交互工具expect,tcl安装和安装包,以及自动互信脚本

    linux自动交互工具expect,tcl安装,以及自动互信脚本 工作中需要对几十台服务器做自动互信,无意中发现expect命令,研究一番. 在网上找了许多资料也没有安装成功,摸索着总算成功了.现分享 ...

  8. 77个常用Linux命令和工具

    77个常用Linux命令和工具 Linux管理员不能单靠GUI图形界面吃饭.这就是我们编辑这篇最实用Linux命令手册的原因.这个指南是特别为Linux管理员和系统管理员 设计的,汇集了最有用的一些工 ...

  9. Linux ---> 监控JVM工具

    Linux ---> 监控JVM工具shkingshking 发布时间: 2013/10/10 01:27 阅读: 2642 收藏: 26 点赞: 1 评论: 0 JDK内置工具使用 jps(J ...

随机推荐

  1. 将命令行提示符里的执行结果导出到text文件中

    为便于查看和保存命令行提示符里的执行结果, 可以使用 ">" 将执行结果导入到指定.txt文件中. 例如: 在命令行提示符里查看C盘文件,并将结果导入到E盘dir-c-out ...

  2. Redis存储对象(序列化和反序列化)

    代码以及实例: package com.hp.test; import redis.clients.jedis.Jedis; import java.io.*; public class Test3 ...

  3. centos 7.5搭建oracle DG

    一.背景 1.IP分配 主库:192.168.12.5 node1 备库:192.168.12.6 node2 2.环境 主库已安装数据库软件,已建库,并有业务数据 备库已安装数据库软件,未建库 二. ...

  4. 精尽Spring MVC源码分析 - MultipartResolver 组件

    该系列文档是本人在学习 Spring MVC 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释 Spring MVC 源码分析 GitHub 地址 进行阅读 Spring 版本:5.2. ...

  5. 安卓 Android Studio 下载

    http://www.android-studio.org/    下载地址 https://blog.csdn.net/qq_41976613/article/details/91432304    ...

  6. IIS应用程序池配置详解及优化

    参数说明 1.常规 属性名称 属性详解 NET CLR 版本 配置应用程序池,以加载特定版本的 .NET CLR.选定的 CLR版本应与应用程序所使用的相应版本的 .NET Framework 对应. ...

  7. 在wildfly中使用SAML协议连接keycloak

    目录 简介 OpenID Connect和SAML SAML的工作流程 在keycloak中使用SAML 准备wildfy和应用程序 简介 我们知道SSO的两个常用的协议分别是SAML和OpenID ...

  8. C#读取DLL文件获取所有类

    说明 调用Web.dll 文件,获取其中的所有的WebService 参考 https://blog.csdn.net/huoliya12/article/details/78873123 流程 使用 ...

  9. mysql数据安全之利用二进制日志mysqlbinlog恢复数据

    mysql数据安全之利用二进制日志mysqlbinlog恢复数据 简介:如何利用二进制日志来恢复数据 查看二进制日志文件的内容报错: [root@xdclass-public log_bin]# my ...

  10. 线程 - AtomicInteger

    原理 AtomicInteger是如何使用非阻塞算法来实现并发控制的 性能提升 避免多线程的优先级倒置和死锁情况的发生 任然可能存在问题 ABA问题 CAS原理 调整具有竞争的并发应用程序的可伸缩性的 ...