curl命令介绍(结合restful web接口)

本文详细介绍了curl命令的基本用法及常用选项,包括如何通过curl发送GET、POST等HTTP请求,如何处理cookie,以及如何使用代理服务器等。同时,还提供了如何通过curl发送JSON格式的数据作为请求体的具体示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 Representational State Transfer,简称REST 是一种全新的软件架构风格、设计风格(特别在http web服务领域)。

curl是利用URL语法在命令行方式下工作的开源文件传输工具。它被广泛应用在Unix、多种Linux发行版中,windows中需要另行安装。

curl基本语法
curl [option] [url]

可选的option有很多,可通过 curl --help 查看,本篇我们重点介绍下几个常用的选项。 
-X    或者 --request    指定请求方式,如GET 、POST 、PUT 、DELETE 、HEAD 等七种方式
-i   或者  --include    显示服务器response 响应头部信息
-v  或者  --verbose    显示详细(冗长)信息
-H  或者  --header  指定http 请求头 ,如  -H  "Content-Type:application/json" 
-d  或者  --data  指定请求体body参数 , 如有多个参数可以用&隔开或者使用多个-d 选项。 如  -d "a=abc&b=110&c=true" (指定三个参数)或  -d a=abc -d b=110 -d c=true 亦可。

-F  或者  --form  指定multipart Form 请求体,如文件域参数,或者普通表单域参数。
-u 或者  --user  指定用户名:密码 
-C  或者  --continue-at  offset  用于断点续传。
-c   或者 --cookie-jar   请求返回回写cookie到指定文件中
-D  或者  --dump-header  请求返回回写response header信息到指定文件中
-b  或者  --cookie  请求时携带上cookie,指定本地cookie所在文件 
-x  或者  --proxy   指定 http代理服务器ip:port 
-O  请求Url 并保存到本地

-o  请求Url并保存到指定文件



1、直接请求
  curl    http://www.tingcream.com    #默认  使用get 请求方式,content-type为form-unlencoded  
  curl   -i    http://www.tingcream.com   # -i 显示response head信息

2 、下载url资源(文件、图片)到本地
 curl -O   http://www.tingcream.com/imgSev/tcblog/image/20180320/20180320235916.522_127.png   #下载图片到本地,图片名称为服务器默认确定的名称
  curl  -o 127.png http://www.tingcream.com/imgSev/tcblog/image/20180320/20180320235916.522_127.png  #下载图片到本地并指定文件名称

3 、请求时指定请求方式

   curl    www.tingcream.com  -X POST   或者 -X post   ,-X PUT 或者 -X put    # 使用post、put方式请求服务

4 、使用查询字符串传参(get)

   curl  http://xxx.com?username=zhangsan&pwd=123  
5 、使用post body传参数
   curl  -v http://xxxx.com  -X POST -d username=zhangsan -d password=123456  或者 
   curl -v  http://xxxx.com  -X POST -d  'username=zhangsan&password=123456'  
6 、使用multipart Form 传参(文件上传)
   curl   http://xxx.com   -X POST  -F "myFile=@/usr/local/aaa.txt" (文件参数)  -F "username=zhangsan" (普通文本参数)
7、cookie的回写与使用
    curl -c  ~/cookie.txt   http://xxx.com   #请求回写cookie到本地文件
    curl -b  ~/cookie.txt  http://xxx.com   #再次请求,携带上cookie (sesisonId)
8、使用http 代理服务器访问url

   curl   -x  proxyIP:proxyPort  http://xxx.com  

9 、使用-C  进行断点续传 



11 、restFull API 使用Post JSON

     curl  http://xxx.com   -X POST -H "Content-Type:application/json"  -d  '{"a":"abc","b":101}' 

    对应java服务端(spring4.x  + spingMVC)

@RequestMapping("/curl_test3")
public String curl_test3(HttpServletRequest request,HttpServletResponse response,
        //restFull  json 请求参数
        @RequestBody JsonParam jsonParam) throws Exception{
    System.out.println("参数a:"+jsonParam.getA());
    System.out.println("参数b:"+jsonParam.getB());
    System.out.println("参数c:"+jsonParam.getC());   
    response.getWriter().write(JSON.toJSONString(AjaxUtil.messageMap(200, "请求成功")));
    return null;
}

pojo 

import java.io.Serializable;
 
public class JsonParam implements Serializable{
     
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private String a;
    private Integer b;
    private Boolean c ;
    public String getA() {
        return a;
    }
    public Integer getB() {
        return b;
    }
    public Boolean getC() {
        return c;
    }
    public void setA(String a) {
        this.a = a;
    }
    public void setB(Integer b) {
        this.b = b;
    }
    public void setC(Boolean c) {
        this.c = c;
    }
 
}

pom.xml 

<dependency> 
         <groupid>com.fasterxml.jackson.core</groupid> 
         <artifactid>jackson-core</artifactid> 
         <version>2.7.9</version> 
     </dependency> 
     <dependency> 
         <groupid>com.fasterxml.jackson.core</groupid> 
         <artifactid>jackson-databind</artifactid> 
          
         <version>2.7.9</version> 
     </dependency> 
     <dependency> 
         <groupid>com.fasterxml.jackson.core</groupid> 
         <artifactid>jackson-annotations</artifactid> 
          <version>2.7.9</version> 
     </dependency>

ok ~~


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值