溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2020-08-21 04:49:38 來(lái)源:腳本之家 閱讀:356 作者:踏歌行666 欄目:服務(wù)器

前言

本文主要講解通過(guò)curl 實(shí)現(xiàn)表單提交登錄。單獨(dú)的表單提交與表單登錄都差不多,因此就不單獨(dú)說(shuō)了。

說(shuō)明:針對(duì)curl表單提交實(shí)現(xiàn)登錄,不是所有網(wǎng)站都適用,原因是有些網(wǎng)站后臺(tái)做了限制或有其他校驗(yàn)。我們不知道這些網(wǎng)站后臺(tái)的限制或校驗(yàn)機(jī)制具體是什么,因此直接curl表單登錄可能是不行的。

當(dāng)然,如下案例是可以用curl登錄的。

案例:LeanCloud登錄

要求和結(jié)果

要求:通過(guò)curl登錄后,能正常訪問(wèn)leancloud的應(yīng)用頁(yè)面。

登錄頁(yè)面鏈接如下:

1 https://leancloud.cn/dashboard/login.html#/signin

能正常訪問(wèn)如下頁(yè)面:

1 https://leancloud.cn/dashboard/applist.html#/apps

瀏覽器訪問(wèn)效果:

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

無(wú)登錄直接訪問(wèn)結(jié)果瀏覽器訪問(wèn)結(jié)果

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

上圖紅框 403 中的訪問(wèn)連接如下:

1 https://leancloud.cn/1.1/clients/self/apps

通過(guò)curl 驗(yàn)證是否登錄

[root@iZ28xbsfvc4Z ~]# curl -i https://leancloud.cn/1.1/clients/self/apps
HTTP/1.1 403 Forbidden
Server: openresty
Date: Sun, 14 Jul 2019 11:35:28 GMT
Content-Type: application/json;charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Cache-Control: no-cache,no-store
Pragma: no-cache

{"code":1,"error":"User doesn't sign in."}

獲取表單字段信息

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

獲取表單提交鏈接

通過(guò)下圖可得到表單提交的鏈接信息。具體如下:

1 https://leancloud.cn/1.1/signin

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

curl 表單登錄并保存cookie信息

1 curl -v -c leancloud1.info -X POST -F 'email=yourname' -F 'password=yourpassword' https://leancloud.cn/1.1/signin
2 # 或則
3 curl -v -c leancloud3.info -X POST -d 'email=yourname&password=yourpassword' https://leancloud.cn/1.1/signin

查看cookie信息

[root@iZ28xbsfvc4Z 20190714_02]# ll
total 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%2F6ipevuxy0OOg%3D%3D
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%3D%3D
leancloud.cn FALSE / TRUE 1563709591 XSRF-TOKEN 751e12827c7c046408541bc1bf962b5912ac35b0d07f88120XXXXXX40704704

每列字段說(shuō)明:

domain:創(chuàng)建并可以讀取變量的域名。
flag:一個(gè) TRUE/FALSE 值,表明給定域中的所有機(jī)器是否都可以訪問(wèn)該變量。此值由瀏覽器自動(dòng)設(shè)置,具體取決于你為域設(shè)置的值。
path:變量在域中有效的路徑。
secure:一個(gè) TRUE/FALSE 值,表明是否需要與域的安全連接來(lái)訪問(wèn)變量。
expiration:該變量將過(guò)期的UNIX時(shí)間。UNIX時(shí)間定義為自1970年1月1日00:00:00 GMT開(kāi)始的秒數(shù)。
name:變量名稱
value:變量值

校驗(yàn)是否登錄成功

直接訪問(wèn)和帶有cookie訪問(wèn),這兩種訪問(wèn)方式,請(qǐng)對(duì)比查看。

直接訪問(wèn)

[root@iZ28xbsfvc4Z 20190714_02]# curl -i https://leancloud.cn/1.1/clients/self/apps
HTTP/1.1 403 Forbidden
Server: openresty
Date: Sun, 14 Jul 2019 11:52:47 GMT
Content-Type: application/json;charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Cache-Control: no-cache,no-store
Pragma: no-cache

{"code":1,"error":"User doesn't sign in."}

帶有cookie文件的訪問(wèn)

# 使用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/apps
HTTP/1.1 200 OK
Server: openresty
Date: Sun, 14 Jul 2019 11:53:29 GMT
Content-Type: application/json;charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Cache-Control: no-cache,no-store
Pragma: no-cache
Strict-Transport-Security: max-age=31536000

[{"app_domain":null,"description":null,"archive_status":0,"biz_type":"dev","master_key": ………………

復(fù)制瀏覽器的cookie訪問(wèn)

[root@iZ28xbsfvc4Z 20190720]# curl -i -H 'cookie: _ga=GA1.2.2055706705.1560005524; …………' https://leancloud.cn/1.1/clients/self/apps
HTTP/1.1 200 OK
Server: openresty
Date: Sat, 20 Jul 2019 08:11:37 GMT
Content-Type: application/json;charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Cache-Control: no-cache,no-store
Pragma: no-cache
Strict-Transport-Security: max-age=31536000

[{"app_domain":null,"description":null,"archive_status":0,"biz_type":"dev","master_key": ………………

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

由上可知curl登錄成功。

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)億速云的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI