日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

shell中的curl網絡請求的實現_linux shell

作者:兵臨城下也 ? 更新時間: 2022-04-28 編程語言

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


 百度一下,你就知道  

關于百度 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中,可以使用定時任務觸發,進而完成一系列的自動化工作。

三、相關鏈接

curl網站開發指南

How do I POST XML data with curl

原文鏈接:https://blog.csdn.net/u012390519/article/details/74231606

欄目分類
最近更新