前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux学习笔记之curl在http的多种用法

Linux学习笔记之curl在http的多种用法

作者头像
Jetpropelledsnake21
发布2021-08-06 09:59:55
4100
发布2021-08-06 09:59:55
举报
文章被收录于专栏:JetpropelledSnakeJetpropelledSnake
代码语言:javascript
复制
curl命令参数很多,这里只列出我曾经用过、特别是在shell脚本中用到过的那些。
 
-v/--verbose 小写的v参数,用于打印更多信息,包括发送的请求信息,这在调试脚本是特别有用。
 
-m/--max-time <seconds> 指定处理的最大时长
 
-H/--header <header> 指定请求头参数
 
-s/--slient 减少输出的信息,比如进度
 
--connect-timeout <seconds> 指定尝试连接的最大时长
 
-x/--proxy <proxyhost[:port]> 指定代理服务器地址和端口,端口默认为1080
 
-T/--upload-file <file> 指定上传文件路径
 
-o/--output <file> 指定输出文件名称
 
-d/--data/--data-ascii <data> 指定POST的内容
 
--retry <num> 指定重试次数
 
-e/--referer <URL> 指定引用地址
 
-I/--head 仅返回头部信息,使用HEAD请求
使用示例
示例一 获取指定网页
 
[root@jfht ~]# curl http://www.sunrisecorp.net/ 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<meta name="title" content="欢迎您 - 上海腾一" />
<meta name="keyword" content="上海腾一,融合通信,电子商务,语音通信,数据通信,基站工程外包托管,物联网,网站建设,电子商务解决方案,移动互联网,短信,彩信,呼叫中心,多方会议,PBX,IVR,电子商务策划方案,设备代维,网络优化,通信工程,电信增值,3G" />
<meta name="description" content="上海腾一信息技术有限公司专注于电信增值、通信工程、电子商务等领域,拥有近十年的经验。" />
<title>
欢迎您 - 上海腾一
</title>
</body>
</html>[root@jfht ~]#
示例二 查看响应头信息
 
[root@jfht ~]# curl -I http://www.sunrisecorp.net/
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Accept-Ranges: bytes
ETag: W/"17801-1285643951000"
Last-Modified: Tue, 28 Sep 2010 03:19:11 GMT
Content-Type: text/html
Content-Length: 17801
Date: Tue, 12 Oct 2010 12:49:20 GMT
 
[root@jfht ~]#
 
===========================================================================
 
通过curl的-w参数我们可以自定义curl的输出
 
代码如下 
  # curl -I -m 10 -o /dev/null -s -w %{http_code} IP地址或者网址
  上面的输出是不含换行的,如果需要换行的话,加上\n
  代码如下 
  # curl -I -m 10 -o /dev/null -s -w %{http_code} IP地址或者网址
  200
 
    # curl -I -m 10 -o /dev/null -s -w %{http_code}"\n" IP地址或者网址
 
  200
 
============================================================================
curl下载
 
在官网下载win32or64.zip,官网下载
 
下载缺失的dll文件
 
用dos进入解压目录,运行curl命令即可
命令实例
 
1、开启gzip请求
curl -I http://www.sina.com.cn/ -H Accept-Encoding:gzip,defalte
 
2、监控网页的响应时间
curl -o /dev/null -s -w "time_connect: %{time_connect}\ntime_starttransfer: %{time_starttransfer}\ntime_total: %{time_total}\n" "http://www.kklinux.com"
 
3. 监控站点可用性
curl -o /dev/null -s -w %{http_code} "http://www.kklinux.com"
 
4、以http1.0协议请求(默认为http1.1)
curl -0 ..............
 
监控站点首页下载时间:
 
curl -o /dev/null -s -w ‘%{time_total}’ http://www.miotour.com
 
curl -o /dev/null -s -w ‘%{http_code}’ http://www.miotour.com
 
curl -o /dev/null -s -w %{http_code}:%{time_connect}:%{time_starttransfer}:%{time_total} http://www.miotour.com
 
结果:2.547
 
-s 静默输出;没有-s的话就是下面的情况,这是在脚本等情况下不需要的信息。
 
[ec2-user@ip-10-122-250-19 ~]$ curl -o /dev/null  -w ‘%{time_total}’ http://www.miotour.com
 
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
 
Dload  Upload   Total   Spent    Left  Speed
 
100 67770    0 67770    0     0  19228      0 –:–:–  0:00:03 –:–:– 20705
 
结果:3.524
 
监控首页各项时间指标:
 
curl -o /dev/null -s -w ‘%{time_connect}:%{time_starttransfer}:%{time_total}’ http://www.miotour.com
 
结果:                                                0.244:                             1.044:                         2.672
 
时间指标解释 :
 
time_connect    建立到服务器的 TCP 连接所用的时间
 
time_starttransfer    在发出请求之后,Web 服务器返回数据的第一个字节所用的时间
 
time_total   完成请求所用的时间
 
在 发出请求之后,Web 服务器处理请求并开始发回数据所用的时间是
 
(time_starttransfer)1.044 - (time_connect)0.244 = 0.8 秒
 
客户机从服务器下载数据所用的时间是
 
(time_total)2.672 - (time_starttransfer)1.044 = 1.682 秒
 
指定特定主机IP地址访问网站
 
curl -x 61.135.169.105:80 http://www.baidu.com
 
curl -x 61.135.169.125:80 http://www.baidu.com
 
 
curl用法大全
 
-x 指定访问IP与端口号
 
curl -x 192.168.1.1:80  http://www.miotour.com
 
-I 仅仅取文件的http头部
 
curl   -I  -x 192.168.1.1:80  http://www.miotour.com
 
用referer做的防盗链,就可以使用-e来设置
 
curl -e “http://www.qiecuo.org”    http:// www.miotour.com -v  -I
 
-H去构造你想要的http头部
 
curl -H “X-Forward-For:8.8.8.8″ http://www.miotour.com  -v  -I
 
curl反馈时间,例如连接时间,下载时间等信息
 
curl -w %{time_connect}:%{time_starttransfer}:%{time_total} -s -o /dev/null
 
将一个文件保存到硬盘上,命名为file.html
 
curl -o file.html  http://www.miotour.com/index.html
 
下载index.html文件, -O是大写的字母
 
curl -O http://www.miotour.com/index.html
 
curl提交用户名和密码
 
curl http://name:passwd@www.miotour.com
curl -u name:passwd http://www.miotour.com
 
-b “cookie” 此参数用来构造一个携带cookie的请求
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-08-02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
短信
腾讯云短信(Short Message Service,SMS)可为广大企业级用户提供稳定可靠,安全合规的短信触达服务。用户可快速接入,调用 API / SDK 或者通过控制台即可发送,支持发送验证码、通知类短信和营销短信。国内验证短信秒级触达,99%到达率;国际/港澳台短信覆盖全球200+国家/地区,全球多服务站点,稳定可靠。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档