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

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

shell中的curl網(wǎng)絡(luò)請(qǐng)求的實(shí)現(xiàn)_linux shell

作者:兵臨城下也 ? 更新時(shí)間: 2022-04-28 編程語(yǔ)言

shell中的curl網(wǎng)絡(luò)請(qǐng)求的實(shí)現(xiàn)
curl 是利用URL語(yǔ)法在命令行下工作的文件傳輸工具,1997年首次發(fā)行,支持文件上傳和下載,結(jié)合shell腳本體驗(yàn)更棒。但按照傳統(tǒng)習(xí)慣稱 curl 為下載工具。

curl 支持的通信協(xié)議有 有FTP、FTPS、HTTP、HTTPS、TFTP、SFTP 等等,支持的平臺(tái)有 Linux、MacOSX、Darwin、Windows、DOS、FreeBSD等等。

一、curl的作用:

1、查看網(wǎng)頁(yè)源碼

denglibingdeMacBook-Pro-4: curl www.baidu.com


 百度一下,你就知道  

關(guān)于百度 About Baidu

©2017 Baidu 使用百度前必讀  意見(jiàn)反饋 京ICP證030173號(hào) 

// 保存整個(gè)網(wǎng)頁(yè),使用 -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、發(fā)送網(wǎng)絡(luò)請(qǐng)求信息

GET方式請(qǐng)求:

curl example.com/form.cgi?data=xxx  如果這里的URL指向的是一個(gè)文件或者一幅圖都可以直接下載到本地

POST方式請(qǐng)求:

//數(shù)據(jù)和網(wǎng)址分開(kāi),需要使用 '--data' 或者 '-d' 參數(shù); curl默認(rèn)使用GET,使用 '-X' 參數(shù)可以支持其他動(dòng)詞, 更多的參數(shù)使用 'man curl' 查看
$ curl -X POST --data "data=xxx" example.com/form.cgi

// '--user-agent' 字段,表表面客戶端的設(shè)備信息:
$ 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' 參數(shù),可以讓curl發(fā)送cookie
$ curl --cookie "name=xxx" www.example.com

//添加頭信息,自行增加一個(gè)頭信息。'--header' 或者 '-H' 參數(shù)就可以起到這個(gè)作用
$ curl --header "Content-Type:application/json" http://example.com


//提交文件作為請(qǐng)求信息 使用 '@文件名' 請(qǐng)求
$ curl -X POST -H "Content-Type: text/xml" -d @denglibing.txt http://example.com

//denglibing.txt:
11622695,D58C6A25-C683-47D6-A18C-B7741284F632

二、實(shí)例

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

當(dāng)然,既然這些請(qǐng)求是在命令行中執(zhí)行,完全可以寫成shell腳本,來(lái)處理一系列的工作,比如多個(gè)請(qǐng)求,而shell腳本在Mac中,可以使用定時(shí)任務(wù)觸發(fā),進(jìn)而完成一系列的自動(dòng)化工作。

三、相關(guān)鏈接

curl網(wǎng)站開(kāi)發(fā)指南

How do I POST XML data with curl

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

欄目分類
最近更新