网络请求全能工具---Curl&Wget

发布时间 2023-11-25 14:25:19作者: 梭梭666

Top

Curl --- 文件传输工具

Curl是一种命令行工具,作用是发出网络请求,然后得到和提取数据,显示在"标准输出"(stdout)上面。它支持多种协议;
curl 命令不带有任何参数时,curl 默认发出 GET 请求,默认不跟随重定向

选项及含义

选项 含义
-o,--output 指定文件名保存输出信息
-O (大写),--remote-name 使用默认文件名(服务器上的名字),存放到本地
-I (大写) 仅返回头部信息
-A 指定用户代理标头User-Agent
-H 指定请求头参数
-b 请求设置Cookie 信息
-c 保存服务端 所设置的 Cookie 信息
-d 设置 POS请求体参数信息
-e 设置请求 头 Referer , 代表请求来源
-F 向服务器上传二进制文件
-G 强制 为 Get 请求 ,可为 URL 设置 锚参
-i 打印 请求头 和 请求体, 空行分割
-I 等同于 --head 只打印请求头
-k 指定跳过 SSL 检测
-L 请求跟随服务器的重定向
-s 【silent】 不输出错误和进度信息
-S 只输出错误信息,通常与-s一起使用
-u 设置服务器认证的用户名和密码
-v 输出通信的整个过程,用于调试
-x 指定 HTTP 请求的代理
-X 指定 HTTP 请求的方法, 默认 GET

命令示例

指定用户代理 User-Agent

curl -A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36' https://google.com  # 伪装为谷歌浏览器 
curl -A '' https://www.baidu.com                        # UA 为空
curl -H 'User-Agent: php/1.0' https://google.com        # 使用 -H 参数

设置请求 发送Cookie信息

curl -b 'foo=bar' https://google.com             # 一个参数
curl -b 'foo1=bar;foo2=bar2' https://google.com  # 两个参数
curl -b cookies.txt https://www.google.com       # 指定保存有 Cookie 参数的文件 
curl -c cookies.txt https://www.google.com
点我回顶部

设置 POST请求头参数信息

curl -d'login=emma&password=123'-X POST https://google.com/login
curl -d 'login=emma' -d 'password=123' -X POST  https://google.com/login
注意: 加上 -d 参数信息以后,HTTP 请求会自动加上标头Content-Type : application/x-www-form-urlencoded。并且会自动将请求转为 POST 方法,因此可以省略-X POST
curl -d '@data.txt' https://google.com/login   # 也可从指定文件 读取参数信息
curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json' https://google.com/login  

设置 Referer 来源

curl -e 'https://google.com?q=example' https://www.example.com
curl -H 'Referer: https://google.com?q=example' https://www.example.com  # 也可通过 -H 参数设置 

上传二进制文件

curl -F 'file=@photo.png' https://google.com/profile  # 图片
# 其中 file 是接收的key 后面的 photo.png 是要上传的文件,在 = 后面加上 @ 符号表示要上传的是文件
注意: 上面命令会给 HTTP 请求加上标头Content-Type: multipart/form-data,然后将文件photo.png作为file字段上传
curl -F 'file=@photo.png;type=image/png' https://google.com/profile   # 指定 MIME类型
curl -F 'file=@photo.png;filename=me.png' https://google.com/profile  #  指定服务器接收 文件名

下拉文件

curl http://172.16.1.10/suosuo/virt/virt_define_dandan  # 自动在下方打印文件内容
curl -O http://172.16.1.10/suosuo/virt/virt_define_dandan  # 使用默认文件名保存到文件中
curl -o shiwei.sh http://172.16.1.10/suosuo/virt/virt_define_dandan   # 使用指定文件名保存到文件中
# 下拉脚本文件并执行
curl http://172.16.1.10/suosuo/virt/virt_define_dandan | sh  

GET请求设置 锚参

curl -G -d 'q=kitties' -d 'count=20' https://google.com/search
上述命令实际发送请求为: https://google.com/search?q=kitties&count=20, 若忽略 -G, 则会发送POST 请求
点我回顶部

打印 Response

curl -i https://www.example.com   # 完整打印
curl -I https://www.example.com   # 只打印 请求头 

跳过 SSL 证书检测

curl -k https://www.example.com

跟随服务器重定向

# 默认不跟随
curl -L -d 'tweet=hi' https://api.twitter.com/tweet  # 默认不跟随

限制 HTTP 请求和回应的带宽

# 模拟慢网速的环境
curl --limit-rate 200k https://google.com # 将带宽限制在每秒 200K 字节。

保存 Response, 下载

# -o 参数将服务器的回应保存成文件,等同于 wget 命令。
curl -o example.html https://www.example.com  # 自定义文件名 example.html
curl -O https://www.example.com/foo/bar.html  # 使用默认文件名 bar.html

禁止输出(错误和进度)

curl -s https://www.example.com            # 不输出错误和进度信息
curl -s -o /dev/null https://google.com    # 不产生任何输出 
curl -S -s -o /dev/null https://google.com # -S 参数指定只输出错误信息,通常与-s一起使用
点我回顶部

登录认证

curl -u 'bob:12345' https://google.com/login
# 设置用户名为bob,密码为12345,然后将其转为 HTTP 标头Authorization: Basic Ym9iOjEyMzQ1
# curl 还能够识别 URL 里面的用户名和密码。命令如下:
curl https://bob:12345@google.com/login # 能够识别 URL 里面的用户名和密码,将其转为上个例子里面的 HTTP 标头

通信调试

curl -v https://www.example.com # 输出通信的整个过程,用于调试
curl --trace - https://www.example.com # --trace参数也可以用于调试,还会输出原始的二进制数据

设置请求代理

curl -x socks5://james:cats@myproxy.com:8080 https://www.example.com  # 指定 HTTP 请求的代理
# 指定 HTTP 请求通过myproxy.com:8080的 socks5 代理发出, 若没有指定代理协议,默认为 HTTP

参考网址

curl 的用法指南 ----- 阮一峰

Curl CookBook

点我回顶部

Wget --- 访问网络文件

选项及含义

选项 含义

命令示例

下拉文件

wget http://172.16.1.10/suosuo/virt/demo001   # 以文件默认文件名保存文件
wget -O dddd.sh http://172.16.1.10/suosuo/virt/demo001   # 以指定文件名保存文件
wget http://172.16.1.10/suosuo/virt/demo001 -O- 2> /dev/null  # 在当前目录打印文件内容
# 下拉脚本并自动执行 
wget http://172.16.1.10/suosuo/virt/demo001 -O- 2> /dev/null | sh
	# 等价于
	wget -qO- http://172.16.1.10/suosuo/virt/demo001 | sh

# 第二种方法
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

参考网址

博客 -- wget 详解