curl请求

一、 curl

1 版本

Curl:默认用的是http/1.1,加-0则用的是http/1.0;

2 参数

想了解详细参数介绍,可输入curl --help查看;
请求时添加限制参数:
curl连接最长时间:–max-time 2
限制客户端下载速率:–limit-rate 30k
超时连接断开: --connect-timeout 1
请求结束后可输出参数:
curl获取响应时间: curl -x 127.1:3128 " http://www.mytest.com/test.flv" -vo/dev/null -w “%{time_total}\n”
http: %{http_code}\n
dns: %{time_namelookup}s\n
redirect: %{time_redirect}s\n
time_connect: %{time_connect}s\n
time_appconnect: %{time_appconnect}s\n
time_pretransfer: %{time_pretransfer}s\n
time_starttransfer: %{time_starttransfer}s\n
size_download: %{size_download}bytes\n
speed_download: %{speed_download}B/s\n
time_total: %{time_total}s\n

3 ipv6

请求需加参数-g, --globoff Disable URL sequences and ranges using {} and []
curl -kg “https://[::1]:443/1.txt” -H “Host: www.nginx.com” -vo/dev/null

4 http请求方法

根据HTTP标准,HTTP请求可以使用多种请求方法。
HTTP的1.0版本中只有三种请求方法: GET, POST 和 HEAD方法。到了1.1版本时,新增加了五种请求方法:OPTIONS, PUT, DELETE, TRACE 和 CONNECT 方法。

4.1 默认get请求

请求指定的页面信息,并返回实体主体。
GET请求请提交的数据放置在HTTP请求协议头中,GET方法通过URL请求来传递用户的输入,GET方式的提交你需要用Request.QueryString来取得变量的值。
GET方法提交数据,可能会带来安全性的问题,数据被浏览器缓存。
GET请求有长度限制。

  1. 一般请求如下:
    curl -x 127.0.0.1:3128 “http://www.test1.com/test_file/test1.mp4” -vo/dev/null
  2. 带-X purge 清空缓存:
    curl -x 127.0.0.1:3128 “http://www.mytest.com/ test_file/test1.mp4” -vo/dev/null -X purge
  3. 页面跳转
    带-L,响应某个跳转页面,在响应头体现
    curl -x 127.0.0.1:3128 “http://www.mytest1.com/test_file/test1.mp4” -vo/dev/null -L
  4. 同时发送两个curl
    curl -x 127.0.0.1:3128 “http://www.test1.com/test_file/test1.mp4” -s >/dev/null 2>&1 & curl -x 127.0.0.1:3128 “http://www.test1.com/test_file/test1.mp4” -vo/dev/null ;这样会显示第二个请求的屏显信息;
  5. 发送多条请求
    发送多条请求i为变量:for i in {1…4};do curl -x 127.0.0.1:80 “http://www.mytest${i}.com/test.flv” -vo/dev/null;done
    Shell脚本:
    #!/bin/bash
    rm -rf /tmp/tmp_file*
    for i in {1…500}
    do
    curl -x 127.0.0.1:80 “http://www.test1.com/wstest/test.html?a=$i” -vo/tmp/tmp_file1_$i >/dev/null 2>&1 &
    curl -x 127.0.0.1:80 "http://www.test1.com/wstest/h4vtest.flv?a=$i" -vo/tmp/tmp_file2$i >/dev/null 2>&1 &
    curl -x 127.0.0.1:80 “http://www.test1.com/wstest/test.mp4?a=$i” -vo/tmp/tmp_file3_$i >/dev/null 2>&1 &
    done

-H “Range: bytes=0-10000”

4.2 head 请求

类似于get请求,只不过返回的响应中没有具体的内容,用于获取报头。

  1. 直接加-I
    curl -x 127.0.0.1:3128 -H “Range:bytes=10000001-10065536” “http://www.test4.com/large_file/150M.mp4” -voa -I
  2. 加-i -X HEAD
    curl -x 127.0.0.1:3128 -H “Range:bytes=10000001-10065536” “http://www.test4.com/large_file/150M.mp4” -voa -i -X HEAD
  3. ab方式
    ab -n 500 -c 100 -X 127.0.0.1:3128 -H “Range:bytes=10000001-10065536” -i “http://www.test4.com/large_file/150M.mp4”

4.3 POST请求:

向指定资源提交数据进行处理请求(例如提交表单或者上传文件)。
POST请求可能会导致新的资源的建立和/或已有资源的修改。
POST方式提交时,你必须通过Request.Form来访问提交的内容

  1. 没有发送body
    post请求可以curl请求后直接加-X POST -H “Content-Length: 0”,这种是没有发送body的
  2. 有发送body
    加-d “x-wasu-test: 123abc”,这种头部即是请求的body
    发送文件 -F “file=/www/wstest/5k.mp4”
    post请求需要消息头
    header_control ADD SER_REQ Content-Length 0 allow all
    telnet 127.0.0.1 3128
    POST /test.html HTTP/1.1
    Host: www.mytest1.com
    Content-Length: 0

telnet 127.0.0.1 3128
GET /test.html HTTP/1.1
Host: www.mytest1.com
Content-Length: 5

12345(这输入的即为post的body,注意前面有两个换行)

Telnet的脚本写法:
echo -e “POST /test.html HTTP/1.1\r\nHost: www.mytest1.com\r\nContent-Length: 0\r\n Proxy-Connection: Keep-Alive\r\n\r\n\r\n” | nc 127.0.0.1 3128

4.4 PUT请求:

从客户端向服务器传送的数据取代指定的文档的内容。
加-T /www/test_file/mytest.txt --max-time 2
或是 -X PUT -d aaa

4.5 DELETE请求

请求服务器删除指定的页面。
DELETE请求一般返回3种码
200(OK)——删除成功,同时返回已经删除的资源。
202(Accepted)——删除请求已经接受,但没有被立即执行(资源也许已经被转移到了待删除区域)。
204(No Content)——删除请求已经被执行,但是没有返回资源(也许是请求删除不存在的资源造成的)。
加-X DELETE --max-time 2

4.6 OPTIONS请求

允许客户端查看服务器的性能。
加-X OPTIONS

4.7 CONNECT

HTTP/1.1协议中预留给能够将连接改为管道方式的代理服务器。

4.8 TRACE

回显服务器收到的请求,主要用于测试或诊断。

5 https请求

一般是:curl -k “https://127.0.0.2${uri}${args}” -H “Host:${domain}” ${header} -vo/dev/null
示例:curl -k “https://127.0.0.2/test_file/test.mp4” -H “Host:www.test.com” -H “Cdn-Src-Ip: 127.0.0.1” -vo/dev/null

6 其他备注

CentOS 5 的 curl -x 默认带 Pragma: no-cache,很容易产生 TCP_CLIENT_REFRESH_MISS,可以加个 -H “Pragma:” 去掉

二、 pollurl

pollurl, 默认用的是http/1.0(shark已有脚本大都用pollurl, pollurl为公司内部封装库,centos7不支持,故新增脚本不建议再使用pollurl);
pollurl -h 127.0.0.1 -p 3128 “${url}”

三、 ab并发

1 GET请求

ab, 默认用的是http/1.0,也支持1.0;
ab并发:-n 20总请求数为20; -c 10并发用户数为10;ab带头部需放在url前面,否则无法识别请求失败
更多参数可以输入:ab --help了解
ulimit -a查下限制参数,ulimit -n 3000可以改-c默认限制的最大数1024,实验无效还是报错
ab -n 20 -c 10 -X 127.0.0.1:3128 “http://www.test.com/test.flv”
ab -n 20 -c 10 -X 127.0.0.1:3128 -H “Range:bytes=100-200” “http://www.mytest.com/index.html”

curl -k “https://127.0.0.1/wstest/index.html” -H “Host:www.mytest1.com” -vo/dev/null
ab -n 4 -c 2 -H “Host:www.mytest1.com” -k “https://127.0.0.1/wstest/index.html”

2 HEAD请求

加参数-i即可,如下
ab -c 1 -n 2 -X 127.0.0.1:8080 -i “http://www.test2.com/post.txt”

3 POST请求

ab -c 1 -n 2 -X 127.0.0.1:8080 -p ‘/www/post.txt’ -T ‘application/x-www-form-urlencoded’ http://www.test1.com/post.txt
post.txt是发送的body

4 参数详解

参考连接:https://www.cnblogs.com/myvic/p/7703973.html
并发数有的测试机器会限制1024,可以用ulimit -a 查看,更改数量用:ulimit -n 5000

四、 openssl

openssl s_client -connect 127.0.0.1:8080 -servername www.test.com

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值