阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

简单介绍shell中的curl网络请求的实现

33次阅读
没有评论

共计 2882 个字符,预计需要花费 8 分钟才能阅读完成。

导读 本文主要介绍了 shell 中的 curl 网络请求的实现,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

shell 中的 curl 网络请求的实现

curl 是利用 URL 语法在命令行下工作的文件传输工具,1997 年首次发行,支持文件上传和下载,结合 shell 脚本体验更棒。但按照传统习惯称 curl 为下载工具。

curl 支持的通信协议有 有 FTP、FTPS、HTTP、HTTPS、TFTP、SFTP 等等,支持的平台有 Linux、MacOSX、Darwin、Windows、DOS、FreeBSD 等等。

一、curl 的作用:
1、查看网页源码
denglibingdeMacBook-Pro-4: curl www.baidu.com
 

  百度一下,你就知道                    
新闻 hao123 地图 视频 贴吧 登录 document.write('登录'); 更多产品

关于百度 About Baidu

©2017 Baidu 使用百度前必读  意见反馈 京 ICP 证 030173 号  // 保存整个网页,使用 -o 处理 denglibingdeMacBook-Pro-4: curl -o baidu www.baidu.com % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 2381 100 2381 0 0 77899 0 --:--:-- --:--:-- --:--:-- 79366
2、查看头信息
denglibingdeMacBook-Pro-4: denglibing$ curl -i www.baidu.com
HTTP/1.1 200 OK
Server: bfe/1.0.8.18
Date: Mon, 03 Jul 2017 09:12:17 GMT
Content-Type: text/html
Content-Length: 2381
Last-Modified: Mon, 23 Jan 2017 13:28:11 GMT
Connection: Keep-Alive
ETag: "588604eb-94d"
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Pragma: no-cache
Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
Accept-Ranges: bytes
...
...
...
3、发送网络请求信息

GET 方式请求:

curl example.com/form.cgi?data=xxx  如果这里的 URL 指向的是一个文件或者一幅图都可以直接下载到本地 

POST 方式请求:

// 数据和网址分开,需要使用 '--data' 或者 '-d' 参数; curl 默认使用 GET,使用 '-X' 参数可以支持其他动词,更多的参数使用 'man curl' 查看
$ curl -X POST --data "data=xxx" example.com/form.cgi
 
// '--user-agent' 字段,表表面客户端的设备信息:$ curl --user-agent "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_2 like Mac OS X) AppleWebKit/603.2.4 (KHTML, like Gecko) Mobile/14F89/mideaConnect MissonWebKit/4021/zh-Hans (AppStore) (4347701760)" http://www.example.com
 
// 使用 '--cookie' 参数,可以让 curl 发送 cookie
$ curl --cookie "name=xxx" www.example.com
 
// 添加头信息, 自行增加一个头信息。'--header' 或者 '-H' 参数就可以起到这个作用
$ curl --header "Content-Type:application/json" http://example.com
 
 
// 提交文件作为请求信息 使用 '@文件名' 请求
$ curl -X POST -H "Content-Type: text/xml" -d @denglibing.txt http://example.com
 
//denglibing.txt:
11622695,D58C6A25-C683-47D6-A18C-B7741284F632
二、实例
denglibingdeMacBook-Pro-4:~ denglibing$ curl https://api.github.com/users
[
  {
    "login": "mojombo",
    "id": 1,
    "avatar_url": "https://avatars3.githubusercontent.com/u/1?v=3",
    "gravatar_id": "","url":"https://api.github.com/users/mojombo","html_url":"https://github.com/mojombo","followers_url":"https://api.github.com/users/mojombo/followers","following_url":"https://api.github.com/users/mojombo/following{/other_user}","gists_url":"https://api.github.com/users/mojombo/gists{/gist_id}","starred_url":"https://api.github.com/users/mojombo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mojombo/subscriptions","organizations_url":"https://api.github.com/users/mojombo/orgs","repos_url":"https://api.github.com/users/mojombo/repos","events_url":"https://api.github.com/users/mojombo/events{/privacy}","received_events_url":"https://api.github.com/users/mojombo/received_events","type":"User","site_admin": false
  }
]

当然,既然这些请求是在命令行中执行,完全可以写成 shell 脚本,来处理一系列的工作,比如多个请求,而 shell 脚本在 Mac 中,可以使用定时任务触发,进而完成一系列的自动化工作。

阿里云 2 核 2G 服务器 3M 带宽 61 元 1 年,有高配

腾讯云新客低至 82 元 / 年,老客户 99 元 / 年

代金券:在阿里云专用满减优惠券

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2024-07-25发表,共计2882字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中