shell中的curl網(wǎng)絡請求

curl 是利用URL語法在命令行下工作的文件傳輸工具,1997年首次發(fā)行,支持文件上傳和下載,結合shell腳本體驗更棒。但按照傳統(tǒng)習慣稱 curl 為下載工具。

curl 支持的通信協(xié)議有 有FTPFTPS、HTTPHTTPS、TFTP、SFTP 等等,支持的平臺有 Linux、MacOSX、Darwin、Windows、DOS、FreeBSD等等。

一、curl的作用:

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

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

<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新聞</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地圖</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>視頻</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>貼吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登錄</a> </noscript> <script>document.write('<a + encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登錄</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多產(chǎn)品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>關于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>?2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必讀</a>  <a href=http://jianyi.baidu.com/ class=cp-feedback>意見反饋</a> 京ICP證030173號  <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

// 保存整個網(wǎng)頁,使用 -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)絡請求信息

GET方式請求:

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

POST方式請求:

//數(shù)據(jù)和網(wǎng)址分開,需要使用 '--data' 或者 '-d' 參數(shù); curl默認使用GET,使用 '-X' 參數(shù)可以支持其他動詞, 更多的參數(shù)使用 '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' 參數(shù),可以讓curl發(fā)送cookie
$ curl --cookie "name=xxx" www.example.com

//添加頭信息,自行增加一個頭信息。'--header' 或者 '-H' 參數(shù)就可以起到這個作用
$ 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:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.webservices.com"><soapenv:Header/><soapenv:Body><ser:addEmpIdCardrecord><ser:empId>11622695,D58C6A25-C683-47D6-A18C-B7741284F632</ser:empId></ser:addEmpIdCardrecord></soapenv:Body></soapenv:Envelope>

二、實例

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

當然,既然這些請求是在命令行中執(zhí)行,完全可以寫成shell腳本,來處理一系列的工作,比如多個請求,而shell腳本在Mac中,可以使用定時任務觸發(fā),進而完成一系列的自動化工作。

三、相關鏈接

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

How do I POST XML data with curl

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 一、什么是CURL? cURL 是一個利用URL語法規(guī)定來傳輸文件和數(shù)據(jù)的工具,支持很多協(xié)議,如HTTP、FTP、...
    茶藝瑤閱讀 4,826評論 0 6
  • 一、什么是CURL? cURL 是一個利用URL語法規(guī)定來傳輸文件和數(shù)據(jù)的工具,支持很多協(xié)議,如HTTP、FTP、...
    伊Summer閱讀 1,398評論 0 4
  • 【php中的curl】php中curl的詳細解說 轉載2014年03月19日 14:38:05 124700 這幾...
    雨黎_95c0閱讀 3,054評論 0 0
  • cURL是一個利用URL語法規(guī)定來傳輸文件和數(shù)據(jù)的工具,支持很多協(xié)議和選項,如HTTP、FTP、TELNET等,能...
    司馬東陽閱讀 1,519評論 0 6
  • 今早一到辦公室,顧問告訴我十點會有一個應聘顧問助理的年輕人過來面試??戳艘幌潞啔v,大專畢業(yè),幾份零零雜雜的工作都不...
    了無風閱讀 1,075評論 4 3

友情鏈接更多精彩內(nèi)容