linux之curl使用技巧

> curl -i https://json.im

链接保存到文件

我们可以使用 > 符号将输出重定向到本地文件中。

> curl https://json.im > index.html

也可以通过 curl 自带的 -o/-O 选项将内容保存到文件中。

> curl -o index.html https://json.im
> curl -O https://json.im/index.html

注意:使用 -O 选项时,必须确保链接末尾包含文件名,否则 curl 无法正确保存文件。如果遇到链接中无文件名的情况,应该使用 -o 选项手动指定文件名,或使用重定向符号

同时下载多个文件

我们可以使用-o 或-O选项来同时指定多个链接,按照以下格式编写命令:

> curl -O html https://json.im/1.html   -O html https://json.im/2.html
> curl -o p1.html https://json.im/1.html  -o p2.html https://json.im/2.html

使用 -L 跟随链接重定向

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

> curl http://json.im

301 Moved Permanently

301 Moved Permanently


openresty

而当我们通过浏览器打开该链接时,会自动跳转到 。此时我们想要 curl 做的,就是像浏览器一样跟随链接的跳转,获取最终的网页内容。我们可以在命令中添加 -L 选项来跟随链接重定向:

> curl -L http://json.im

使用 -A 自定义 User-Agent

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

> curl -A "Mozilla/5.0 (Android; Mobile; rv:35.0) Gecko/35.0 Firefox/35.0" https://json.im

使用 -H 自定义 header

当我们需要传递特定的 header 的时候,可以仿照以下命令来写:

> curl -H "Referer: https://rumenz.com" -H "User-Agent: Custom-User-Agent" https://json.im

header 中传递 Cookie

> curl -H "Cookie: JSESSIONID=xxx" https://json.im

使用-c保存Cookie

使用 cURL 访问页面的时候,默认是不会保存 Cookie 的

> curl -c "cookie.txt" https://json.im

使用 -b 读取 Cookie

> curl -b "cookie.txt" https://json.im

使用-d发送POST请求

有一个登录页面 ,只需要提交用户名和密码便可登录。我们可以使用 cURL 来完成这一 POST 请求,-d 用于指定发送的数据,-X 用于指定发送数据的方式

> curl -d "userName=rumenz&passwd=123456" -X POST https://json.im/login

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

> curl -d "userName=rumenz&passwd=123456" https://json.im/login

从文件中读取data.txt文本

> curl -d "@data.txt" https://json.im/upload

恢复中断下载

> curl -C - -O https://json.im/jdk.tar.gz

从文件下载URL

如果将curl与xargs结合使用,则可以从文件中的URL列表中下载文件。

> xargs -n 1 curl -O < urls.txt

CURL设置代理

> curl https://json.im/  -U user:password  -x 127.0.0.1:3128

如果您的代理不需要身份验证,您可以跳过-U user:password 。

比如Nginx配置代理

server {
    listen                         3128;
    resolver                       8.8.8.8;
    proxy_connect;
    proxy_connect_allow            443 563;
    proxy_connect_connect_timeout  10s;
    proxy_connect_read_timeout     10s;
    proxy_connect_send_timeout     10s;
    location / {
        proxy_pass http://$host;
        proxy_set_header Host $host;
    }
}

上传文件到ftp

> curl -u username:password -T jdk.tar.gz ftp://json.im

修改名称解析

> curl --resolve json.im:443:127.0.0.1 https://json.im:443/

对的查询将告诉curl从127.0.0.1请求该站点,而不是使用DNS或/etc/hosts文件。

限制下载率

> curl --limit-rate 100K https://json.im/jdk.tar.gz -O

限时特惠:本站每日持续更新海量各大内部创业教程,永久会员享受全站资源免费下载.www.heook.com
站长微信:wc764800

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容