linux Curl命令使用

Curl基本命令

  1. 获取页面内容
    默认会发送 GET 请求来获取链接内容到标准输出。
    实例:curl http://localhost:8080/simple-service-webapp/test/hello
    在这里插入图片描述
  2. 显示 HTTP 头
    显示 HTTP 头,而不显示文件内容,使用 -I 选项
    实例:curl -I http://localhost:8080/simple-service-webapp/test/hello
    在这里插入图片描述
    同时显示 HTTP 头和文件内容,使用 -i 选项
    实例:curl -i http://localhost:8080/simple-service-webapp/test/hello
    在这里插入图片描述
  3. 将返回结果保存到文件
    使用-o参数
    实例:curl -o save.txt http://localhost:8080/simple-service-webapp/test/hello
    在这里插入图片描述
    或者使用重定向命令 >
    实例:curl http://localhost:8080/simple-service-webapp/test/hello > save.txt

同时下载多个文件
curl -o save.txt http://localhost:8080/simple-service-webapp/test/hello
-o save2.txt http://localhost:8080/simple-service-webapp/test/hello2

  1. 链接重定向
    使用 -L 跟随链接重定向,逻辑为http://codebelief.com -> http://www.codebelief.com
    实例:curl -L http://codebelief.com
  2. 使用 -H 自定义 header
    实例1:curl -H “Referer: www.example.com” -H “User-Agent: Custom-User-Agent” http://www.baidu.com
    实例2:curl -H “Cookie: JSESSIONID=D0112A5063D938586B659EF8F939BE24” http://www.example.com
  3. 使用 -c 保存 Cookie
    当我们使用 cURL 访问页面的时候,默认是不会保存 Cookie 的。有些情况下我们希望保存 Cookie 以便下次访问时使用。例如登陆了某个网站,我们希望再次访问该网站时保持登陆的状态,这时就可以现将登陆时的 Cookie 保存起来,下次访问时再读取。-c 后面跟上要保存的文件名。
    实例:curl -c “cookie-example” http://www.example.com
  4. 使用 -b 读取 Cookie
    前面讲到了使用 -H 来发送 Cookie 的方法,这种方式是直接将 Cookie 字符串写在命令中。如果使用 -b 来自定义 Cookie,命令如下:
    实例1:curl -b “JSESSIONID=D0112A5063D938586B659EF8F939BE24” http://www.example.com
    实例2:curl -b "cookie-example " http://www.example.com
  5. 使用 -d 发送 POST 请求
    我们以登陆网页为例来进行说明使用 cURL 发送 POST 请求的方法。假设有一个登录页面 www.example.com/login,只需要提交用户名和密码便可登录。我们可以使用 CURL 来完成这一 POST 请求,-d 用于指定发送的数据,-X 用于指定发送数据的方式:
    实例1:curl -d “userName=tom&passwd=123456” -X POST http://www.example.com/login
    实例2:curl -d " {“name”:“1234”,“id”:“c62f7bff-d8d7-402a-935f-e33b539a9ed7”}" -X POST http://www.example.com/login
    参数以文件方式
    实例3:curl -d “@data.txt” -X POST http://www.example.com/login
  6. 文件上传
    使用了-F参数,curl会以multipart/form-data的方式发送POST请求。-F以key=value的形式指定要上传的参数,如果是文件,则需要使用key=@file的形式。
    实例1:curl -F “key=value” -F “filename=@file.tar.gz” http://localhost/upload
  7. 其他命令
    其他命令参考:http://man.linuxde.net/curl

实际项目例子

  1. GET方式
    后台逻辑:
	@GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/doGet")
    public Response doGet(@QueryParam("name") String name, @Context HttpHeaders httpHeader, @Context UriInfo uriInfo) {
		// 请求头
		String contentType = httpHeader.getHeaderString("Content-Type");
		String token = httpHeader.getHeaderString("X-Auth-Token");
		JSONObject headerObject = new JSONObject();
		headerObject.put("contentType", contentType);
		headerObject.put("token", token);
		
		JSONObject dataObject = new JSONObject();
		dataObject.put("id", UUID.randomUUID());
		dataObject.put("name", name);
		
		JSONObject resultObject = new JSONObject();
		resultObject.put("datas", dataObject);
		resultObject.put("header", headerObject);
		
		return Response.ok(resultObject, MediaType.APPLICATION_JSON).status(Response.Status.OK).build();
	}

Curl命令:

curl -i -H “Content-Type:application/json;charset=UTF-8” -H “X-Auth-Token:1234567876543” http://localhost:8080/simple-service-we bapp/demo/doGet?name=1234

返回结果

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: application/json
Content-Length: 151
Date: Sun, 26 May 2019 13:26:50 GMT

{"datas":{"name":"1234","id":"e899d723-e140-4d32-a59a-23fff09a82cf"},"header":{"contentType":"application/json;charset=UTF-8","token":"1234567876543"}}
  1. POST方式
    后台逻辑:
    @POST
	@Produces(MediaType.APPLICATION_JSON)
	@Path("/doPost")
	public Response doPost(String param, @Context HttpHeaders httpHeader, @Context UriInfo uriInfo) {
		// 请求头
		String contentType = httpHeader.getHeaderString("Content-Type");
		String token = httpHeader.getHeaderString("X-Auth-Token");
		JSONObject headerObject = new JSONObject();
		headerObject.put("contentType", contentType);
		headerObject.put("token", token);
		
		JSONObject dataObject = JSONObject.parseObject(param);
		
		JSONObject resultObject = new JSONObject();
		resultObject.put("datas", dataObject);
		resultObject.put("header", headerObject);
		
		return Response.ok(resultObject, MediaType.APPLICATION_JSON).status(Response.Status.OK).build();
	}

Curl命令:

curl -i -H “Content-Type:application/json;charset=UTF-8” -H “X-Auth-Token:1234567876543” -d “{“vimId”:“vim3”,“regionId”:“Reg ionOne”,“vnfrId”:“vnf1”,“operationId”:“8c500087-81bd-42fe-b227-28a3094f9f7e”,“changeType”:“scale-in”,“serverList”:[“b44a03f8-ec02-48a9-91da-cbe064b8d99b”,“bee3803b-509c-45e6-b278-43cc3614886b”,“cc3ea4de-65a4-434a-b7da-6ceb26cea1dc”],“callType”:“0”,“realEnd”:“false”}” http://localhost:8080/simple-service-webapp/demo/doPost

返回结果

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: application/json
Content-Length: 390
Date: Sun, 26 May 2019 13:30:49 GMT

{"datas":{"vimId":"vim3","regionId":"Reg ionOne","vnfrId":"vnf1","realEnd":"false","changeType":"scale-in","serverList":["b44a03f8-ec02-48a9-91da-cbe064b8d99b","bee3803b-509c-45e6-b278-43cc3614886b","cc3ea4de-65a4-434a-b7da-6ceb26cea1dc"],"operationId":"8c500087-81bd-42fe-b227-28a3094f9f7e","callType":"0"},"header":{"contentType":"application/json;charset=UTF-8","token":"1234567876543"}}
  • 7
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值