Linux curl表單登錄或提交與cookie使用詳解

發(fā)布時間:2024-03-05
服務(wù)器
前言
本文主要講解通過curl 實現(xiàn)表單提交登錄。單獨的表單提交與表單登錄都差不多,因此就不單獨說了。
說明:針對curl表單提交實現(xiàn)登錄,不是所有網(wǎng)站都適用,原因是有些網(wǎng)站后臺做了限制或有其他校驗。我們不知道這些網(wǎng)站后臺的限制或校驗機制具體是什么,因此直接curl表單登錄可能是不行的。
當(dāng)然,如下案例是可以用curl登錄的。
案例:leancloud登錄
要求和結(jié)果
要求:通過curl登錄后,能正常訪問leancloud的應(yīng)用頁面。
登錄頁面鏈接如下:
1 https://leancloud.cn/dashboard/login.html#/signin
能正常訪問如下頁面:
1 https://leancloud.cn/dashboard/applist.html#/apps
瀏覽器訪問效果:
無登錄直接訪問結(jié)果瀏覽器訪問結(jié)果
上圖紅框 403 中的訪問連接如下:
1 https://leancloud.cn/1.1/clients/self/apps
通過curl 驗證是否登錄
[root@iz28xbsfvc4z ~]# curl -i https://leancloud.cn/1.1/clients/self/appshttp/1.1 403 forbiddenserver: openrestydate: sun, 14 jul 2019 11:35:28 gmtcontent-type: application/json;charset=utf-8transfer-encoding: chunkedconnection: keep-alivevary: accept-encodingcache-control: no-cache,no-storepragma: no-cache{code:1,error:user doesn\\\'t sign in.}獲取表單字段信息
獲取表單提交鏈接
通過下圖可得到表單提交的鏈接信息。具體如下:
1 https://leancloud.cn/1.1/signin
curl 表單登錄并保存cookie信息
1 curl -v -c leancloud1.info -x post -f \\\'email=yourname\\\' -f \\\'password=yourpassword\\\' https://leancloud.cn/1.1/signin2 # 或則3 curl -v -c leancloud3.info -x post -d \\\'email=yourname&password=yourpassword\\\' https://leancloud.cn/1.1/signin查看cookie信息
[root@iz28xbsfvc4z 20190714_02]# lltotal 32-rw-r--r-- 1 root root 337 jul 14 19:45 leancloud1.info-rw-r--r-- 1 root root 335 jul 14 19:46 leancloud3.info[root@iz28xbsfvc4z 20190714_02]# cat leancloud1.info # netscape http cookie file# http://curl.haxx.se/docs/http-cookies.html# this file was generated by libcurl! edit at your own risk.#httponly_leancloud.cn false / true 1563709522 uluru_user ff1ipoimx/6ipevuxy0oog==leancloud.cn false / true 1563709522 xsrf-token 5647dc84bd6eaea37eca2d07ae0e401cca4ba76803989c8559xxxxx7283da[root@iz28xbsfvc4z 20190714_02]# cat leancloud3.info # netscape http cookie file# http://curl.haxx.se/docs/http-cookies.html# this file was generated by libcurl! edit at your own risk.#httponly_leancloud.cn false / true 1563709591 uluru_user artwqm6jylzljbaqt7tpiq==leancloud.cn false / true 1563709591 xsrf-token 751e12827c7c046408541bc1bf962b5912ac35b0d07f88120xxxxxx40704704每列字段說明:
domain:創(chuàng)建并可以讀取變量的域名。
flag:一個 true/false 值,表明給定域中的所有機器是否都可以訪問該變量。此值由瀏覽器自動設(shè)置,具體取決于你為域設(shè)置的值。
path:變量在域中有效的路徑。
secure:一個 true/false 值,表明是否需要與域的安全連接來訪問變量。
expiration:該變量將過期的unix時間。unix時間定義為自1970年1月1日00:00:00 gmt開始的秒數(shù)。
name:變量名稱
value:變量值
校驗是否登錄成功
直接訪問和帶有cookie訪問,這兩種訪問方式,請對比查看。
直接訪問
[root@iz28xbsfvc4z 20190714_02]# curl -i https://leancloud.cn/1.1/clients/self/appshttp/1.1 403 forbiddenserver: openrestydate: sun, 14 jul 2019 11:52:47 gmtcontent-type: application/json;charset=utf-8transfer-encoding: chunkedconnection: keep-alivevary: accept-encodingcache-control: no-cache,no-storepragma: no-cache{code:1,error:user doesn\\\'t sign in.}帶有cookie文件的訪問
# 使用cookie[root@iz28xbsfvc4z 20190714_02]# curl -i -b leancloud1.info https://leancloud.cn/1.1/clients/self/apps ## 或者[root@iz28xbsfvc4z 20190714_02]# curl -i -b leancloud3.info https://leancloud.cn/1.1/clients/self/appshttp/1.1 200 okserver: openrestydate: sun, 14 jul 2019 11:53:29 gmtcontent-type: application/json;charset=utf-8transfer-encoding: chunkedconnection: keep-alivevary: accept-encodingcache-control: no-cache,no-storepragma: no-cachestrict-transport-security: max-age=31536000[{app_domain:null,description:null,archive_status:0,biz_type:dev,master_key: ………………復(fù)制瀏覽器的cookie訪問
[root@iz28xbsfvc4z 20190720]# curl -i -h \\\'cookie: _ga=ga1.2.2055706705.1560005524; …………\\\' https://leancloud.cn/1.1/clients/self/appshttp/1.1 200 okserver: openrestydate: sat, 20 jul 2019 08:11:37 gmtcontent-type: application/json;charset=utf-8transfer-encoding: chunkedconnection: keep-alivevary: accept-encodingcache-control: no-cache,no-storepragma: no-cachestrict-transport-security: max-age=31536000[{app_domain:null,description:null,archive_status:0,biz_type:dev,master_key: ………………
由上可知curl登錄成功。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對西部數(shù)碼的支持。
上一個:離婚房子還沒辦房產(chǎn)證如何做房變
下一個:鼎聲微GR0603F160RT5G00電阻

一般婚慶公司價格表 婚慶公司有哪些收費項目
園林植物病蟲害葉斑病的防治
飛盧小說網(wǎng)電腦端(飛盧小說網(wǎng),電腦版)
新手怎么泡出一杯好茶
二級域名怎么弄?二級域名的用途是什么?
360n4s 紅米note3(小米note3和360n6pro)
青少年模式在哪里設(shè)置微信(vx青少年模式)
幾種護(hù)膚美容中藥
wdbiue是什么硬盤,WD硬盤是什么牌子的
Canonical 宣布發(fā)布 Ubuntu 20.04 LTS
十八禁 网站在线观看免费视频_2020av天堂网_一 级 黄 色 片免费网站_绝顶高潮合集Videos