curl简单入门

curl是利用URL语法在命令行方式下工作的开源文件传输工具。它被广泛应用在Unix、多种Linux发行版中,并且有DOS和Win32、Win64下的移植版本。

curl官网下载你需要的curl版本,这儿我使用的win64位,解压后,将curl.exe文件放置到C:WindowsSystem32路径下,可是使用命令行敲入curl出现以下图片,表示安装成功。
curl安装截图

curl常用参数

-A:随意指定自己这次访问所宣称的自己的浏览器信息

-b/–cookie cookie字符串或文件读取位置:使用option来把上次的cookie信息追加到http request里面去。

-c/–cookie-jar 操作结束后把cookie写入到这个文件中

-C/–continue-at 断点续转

-d/–data : HTTP POST方式传送数据

-D/–dump-header 把header信息写入到该文件中

-F/–form 模拟http表单提交数据

-v/–verbose 小写的v参数,用于打印更多信息,包括发送的请求信息,这在调试脚本是特别有用。

-m/–max-time 指定处理的最大时长

-H/–header

-s/–slient 减少输出的信息,比如进度

–connect-timeout 指定尝试连接的最大时长

-x/–proxy 指定代理服务器地址和端口,端口默认为1080

-T/–upload-file 指定上传文件路径

-o/–output 指定输出文件名称

–retry 指定重试次数

-e/–referer 指定引用地址

-I/–head 仅返回头部信息,使用HEAD请求

-u/–user 设置服务器的用户和密码

-O:按照服务器上的文件名,自动存在本地

-r/–range 检索来自HTTP/1.1或FTP服务器字节范围

-T/–upload-file 上传文件

curl实现GET/POST请求

  1. 直接访问站点

    curl http:
    # https地址可以直接访问    
    curl https:
    
  2. 保存访问页面/资源

    # 将网站的首页保存到当前目录index.html文件中
    curl -o index.html http:
    # 将网站上面的资源以原来的名称 保存到当前目录
    curl -O http://www.XXXX.com/banner.jpg
    
  1. GET方式请求

    curl http://www.example.com?username=kuku&passwd=123456
    # A/001.JPG -> 下载后: 001-A.JPG 原来: B/001.JPG -> 下载后: 001-B.JPG
    curl -o #2_#1.jpg http://www.XXXX.com/~{A,B}/[001-201].JPG
    
  2. Post方式请求

    curl -d "username=kuku&passwd=123456" http://www.example.com
    
  3. 模拟表单信息,模拟登录

    # 保存cookie信息
    curl -c ./cookie_c.txt -F log=aaaa -F pwd=*** http://www.XXXX.com/login.htm
    # 保存header信息
    curl -D ./cookie_D.txt -F log=aaaa -F pwd=*** http://www.XXXX.com/login.htm
    # 使用cookie文件
    curl -b ./cookie_c.txt http://www.XXXX.com/admin.htm
    
  4. 断点续传 -C(大写)

    curl -C -O http://www.XXXX.com/banner.jpg
    
  5. 伪造来源地址,防盗链

    curl -e http://localhost http://www.XXXX.com/login.htm
    
  6. 代理IP

    curl -x xx.xx.xx.xx:xxxxx -o index.html http://www.XXXX.com
    
  7. 分段下载

    curl -r 0-100 -o img.part1 http://www.XXXX.com/banner.jpg
    curl -r 100-200 -o img.part2 http://www.XXXX.com/banner.jpg
    curl -r 200- -o img.part3 http://www.XXXX.com/banner.jpg
    cat img.part* > img.jpg
    
  8. 显示/隐藏下载进度条

    # 隐藏进度条
    curl -s -o aaa.jpg http://www.baidu.com/img/bdlogo.gif
    # 显示进度条
    curl -O aaa.jpg http://www.baidu.com/img/bdlogo.gif
    
  9. 模拟浏览器头

    curl -A "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" -x 123.45.67.89:1080 -o index.html -D cookie0001.txt http://www.baidu.com
    
  10. 文件上传

    一点需要注意的是,POST模式下的文件上的文件上传,比如
        <form method="POST" enctype="multipar/form-data" action="http://www.xxx.com/uploadImg">
        <input type=file name=upload>
        <input type=submit name=nick value="go">
        </form>
    这样一个HTTP表单,我们要用curl进行模拟,就该是这样的语法:
    curl -F upload=@localfilename -F nick=go http://www.xxx.com/uploadImg
    
  11. 引用,某些资源需要经过一个网络地址跳转过去

    curl -e http://www.xx.com/redict
    
  12. 上传一个资源到http服务器,最好使用PUT方法

    curl -T uploadfile http://www.xxx.com
    curl --upload-file uploadfile http://www.xxx.com
    

参考

CURL实现HTTP的GET/POST方法
CURL详解
curl官网

原文:大专栏  curl简单入门


猜你喜欢

转载自www.cnblogs.com/chinatrump/p/11601434.html