zoukankan      html  css  js  c++  java
  • curl用法

    简介

    curl是一个和服务器交互信息(发送和获取信息)的命令行工具,支持DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, TELNET和TFTP等协议。curl支持代理、用户认证、FTP上传、HTTP POST请求、SSL连接、cookies、文件传输、Metalink等功能。

    URL

    curl支持如下几种方式的URL:

    可以指定多个url,或者在花括号中指定url的多个部分。

    http://site.{one,two,three}.com
    

    可以用中括号指定数字或字母序列。

    ftp://ftp.numericals.com/file[1-100].txt
    ftp://ftp.numericals.com/file[001-100].txt    (with leading zeros)
    ftp://ftp.letters.com/file[a-z].txt
    

    可以指定多个序列。

    http://any.org/archive[1996-1999]/vol[1-4]/part{a,b,c}.html
    

    可以在命令行指定任意数量的url,curl会按指定顺序获取url的内容。

    可以在一个范围内指定跳跃的步数。

    http://www.numericals.com/file[1-100:10].txt
    http://www.letters.com/file[a-z:2].txt
    

    如果没有指定协议前缀,curl会尝试猜测协议。它默认会选择http协议,但是当遇见常用的host名字时,会选择尝试其他协议。例如ftp.xxx.com,curl会尝试ftp协议。

    查看http响应头

    curl -i http://www.baidu.com
    

    查看交互过程

    curl -v http://www.baidu.com
    

    GET请求

    当发起http请求时,curl会默认发起GET请求,也可以"-X GET"方式指定。

    curl -X GET http://www.baidu.com
    

    POST请求

    当使用POST请求方式,需要通过指定“-d”,向服务器传递数据。

    curl -X POST http://www.example.com/posts
    

    DELETE请求

    DELETE请求用于删除服务器端的数据。

    curl -X DELETE http://www.example.com/posts/1
    

    PUT请求

    PUT请求用于修改服务器端的数据

    curl -X PUT http://www.example.com/posts/1
    

    HTTP认证

    常用的HTTP认证方式有:Basic认证、Digest认证、OAuth2认证。

    Basic认证

    curl --basic  -u user:password http://www.example.com/posts/1
    

    Digest认证

    curl --digest -u user:password http://www.example.com/posts/1
    

    OAuth2认证

    curl -u clientId:clientSecret -X POST -d "username=test&password=test&grant_type=password&scope=read" http://www.example.com/oauth/token
    curl -H "Authorization: Bearer [bearer]" http://www.example.com/posts/1
    

    文件上传

    假定文件上传的表单如下所示:

    <form method="POST" enctype='multipart/form-data' action="upload.cgi">
    	<input type=file name=upload>
    	<input type=submit name=press value="OK">
    </form>
    

    可使用curl按如下方式上传文件:

    curl --form upload=@localfilename --form press=OK http://www.example.com
    

    User Agent字段

    这个字段用来表示客户端的设备信息。服务器有时会根据这个字段,针对不同的设备,返回不同格式的网页,比如移动端和PC端。

    curl --user-agent "[user agent]" http://www.example.com
    

    curl可以发送cookie

    curl --cookie "name1=value1" http://www.example.com
    

    下载网页

    curl -o file.html http://www.example.com
    

    -O选项可以按照服务器的文件名保存文件

    curl -O http://www.example.com/1.jpg
    

    代理服务器

    curl -x 代理服务器地址:端口 http://www.example.com
    

    保存cookie信息

    curl -D cookiefile01.txt http://www.example.com
    

    使用保存cookie信息的文件

    curl -D cookiefile02.txt -b cookiefile01.txt http://www.example.com
    

    输出详细的交互信息

    curl http://www.example.com --trace-ascii /dev/stdout
  • 相关阅读:
    896. 单调数列
    819. 最常见的单词
    collections.Counter()
    257. 二叉树的所有路径
    万里长征,始于足下——菜鸟程序员的学习总结(三)
    Ogre启动过程&原理
    Ogre导入模型
    四元数
    Ogre3D嵌入Qt框架
    如何搭建本地SVN服务
  • 原文地址:https://www.cnblogs.com/coderland/p/5883763.html
Copyright © 2011-2022 走看看