博客详情

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

作者: 朝如青丝暮成雪
发布时间:2018-04-09 01:37:00  文章分类:java编程   阅读(1736)  评论(0)

 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  进行断点续传 

    


10、连接服务器时指定登录用户(密码)

curl -O -u 用户名:密码 ftp:/xxx.com/aa.zip   #从远程ftp服务中下载资源。


11 、restFull API 使用Post JSON

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

对应java服务端(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;
	}


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;
	}

} 


      
             com.fasterxml.jackson.core  
             jackson-core  
             2.7.9  
           
           
             com.fasterxml.jackson.core  
             jackson-databind  
             
             2.7.9  
           
           
             com.fasterxml.jackson.core  
             jackson-annotations  
              2.7.9  
	     



ok ~~~





关键字:  curl
评论信息
暂无评论
发表评论

亲,您还没有登陆,暂不能评论哦! 去 登陆 | 注册

博主信息
   
数据加载中,请稍候...
文章分类
   
数据加载中,请稍候...
阅读排行
 
数据加载中,请稍候...
评论排行
 
数据加载中,请稍候...

Copyright © 叮叮声的奶酪 版权所有
备案号:鄂ICP备17018671号-1

鄂公网安备 42011102000739号