新聞中心
前言

創(chuàng)新互聯(lián)主營(yíng)興業(yè)網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,成都app軟件開(kāi)發(fā)公司,興業(yè)h5重慶小程序開(kāi)發(fā)搭建,興業(yè)網(wǎng)站營(yíng)銷(xiāo)推廣歡迎興業(yè)等地區(qū)企業(yè)咨詢
本文主要講解通過(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)效果:
無(wú)登錄直接訪問(wèn)結(jié)果瀏覽器訪問(wèn)結(jié)果
上圖紅框 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."}獲取表單字段信息
獲取表單提交鏈接
通過(guò)下圖可得到表單提交的鏈接信息。具體如下:
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/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:變量名稱(chēng)
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": ………………
由上可知curl登錄成功。
總結(jié)
本篇文章到此結(jié)束,如果您有相關(guān)技術(shù)方面疑問(wèn)可以聯(lián)系我們技術(shù)人員遠(yuǎn)程解決,感謝大家支持本站!
創(chuàng)新互聯(lián)成都網(wǎng)站建設(shè)公司提供專(zhuān)業(yè)的建站服務(wù),為您量身定制,歡迎來(lái)電(028-86922220)為您打造專(zhuān)屬于企業(yè)本身的網(wǎng)絡(luò)品牌形象。
成都創(chuàng)新互聯(lián)品牌官網(wǎng)提供專(zhuān)業(yè)的網(wǎng)站建設(shè)、設(shè)計(jì)、制作等服務(wù),是一家以網(wǎng)站建設(shè)為主要業(yè)務(wù)的公司,在網(wǎng)站建設(shè)、設(shè)計(jì)和制作領(lǐng)域具有豐富的經(jīng)驗(yàn)。
網(wǎng)頁(yè)名稱(chēng):Linuxcurl表單登錄或提交與cookie使用詳解
新聞來(lái)源:http://www.dlmjj.cn/article/ccicggp.html


咨詢
建站咨詢
