亚洲全黄无码一级在线看_国产剧情久久久性色_无码av一区二区三区无码_亚洲成a×人片在线观看

當(dāng)前位置: 首頁 > 科技新聞 >

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

時間:2020-06-30 16:58來源:網(wǎng)絡(luò)整理 瀏覽:
本文主要講解通過curl 實現(xiàn)表單提交登錄。單獨的表單提交與表單登錄都差不多,因此就不單獨說了。說明:針對curl表單提交實現(xiàn)登錄,不是所有

本文主要講解通過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

瀏覽器訪問效果:

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

無登錄直接訪問結(jié)果瀏覽器訪問結(jié)果Linux curl 表單登錄或提交與cookie使用

上圖紅框 403 中的訪問連接如下:

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

通過curl 驗證是否登錄
 1 [root@iZ28xbsfvc4Z ~]# curl -i https://leancloud.cn/1.1/clients/self/apps
2 HTTP/1.1 403 Forbidden
3 Server: openresty
4 Date: Sun, 14 Jul 2019 11:35:28 GMT
5 Content-Type: application/json;charset=utf-8
6 Transfer-Encoding: chunked
7 Connection: keep-alive
8 Vary: Accept-Encoding
9 Cache-Control: no-cache,no-store
10 Pragma: no-cache
11
12 {"code":1,"error":"User doesn't sign in."}

獲取表單字段信息Linux curl 表單登錄或提交與cookie使用

獲取表單提交鏈接

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

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信息
 1 [root@iZ28xbsfvc4Z 20190714_02]# ll
2 total 32
3 -rw-r--r-- 1 root root 337 Jul 14 19:45 leancloud1.info
4 -rw-r--r-- 1 root root 335 Jul 14 19:46 leancloud3.info
5 [root@iZ28xbsfvc4Z 20190714_02]# cat leancloud1.info
6 # Netscape HTTP Cookie File
7 # http://curl.haxx.se/docs/http-cookies.html
8 # This file was generated by libcurl! Edit at your own risk.
9
10 #HttpOnly_leancloud.cn FALSE / TRUE 1563709522 uluru_user Ff1IPOiMX%2F6ipevuxy0OOg%3D%3D
11 leancloud.cn FALSE / TRUE 1563709522 XSRF-TOKEN 5647dc84bd6eaea37eca2d07ae0e401cca4ba76803989c8559XXXXX7283da
12 [root@iZ28xbsfvc4Z 20190714_02]# cat leancloud3.info
13 # Netscape HTTP Cookie File
14 # http://curl.haxx.se/docs/http-cookies.html
15 # This file was generated by libcurl! Edit at your own risk.
16
17 #HttpOnly_leancloud.cn FALSE / TRUE 1563709591 uluru_user arTwQm6JylzLjBaQt7TpiQ%3D%3D
18 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訪問,這兩種訪問方式,請對比查看。

直接訪問
 1 [root@iZ28xbsfvc4Z 20190714_02]# curl -i https://leancloud.cn/1.1/clients/self/apps
2 HTTP/1.1 403 Forbidden
3 Server: openresty
4 Date: Sun, 14 Jul 2019 11:52:47 GMT
5 Content-Type: application/json;charset=utf-8
6 Transfer-Encoding: chunked
7 Connection: keep-alive
8 Vary: Accept-Encoding
9 Cache-Control: no-cache,no-store
10 Pragma: no-cache
11
12 {"code":1,"error":"User doesn't sign in."}

帶有cookie文件的訪問
 1 # 使用cookie
2 [root@iZ28xbsfvc4Z 20190714_02]# curl -i -b leancloud1.info https://leancloud.cn/1.1/clients/self/apps
3 ## 或者
4 [root@iZ28xbsfvc4Z 20190714_02]# curl -i -b leancloud3.info https://leancloud.cn/1.1/clients/self/apps
5 HTTP/1.1 200 OK
6 Server: openresty
7 Date: Sun, 14 Jul 2019 11:53:29 GMT
8 Content-Type: application/json;charset=utf-8
9 Transfer-Encoding: chunked
10 Connection: keep-alive
11 Vary: Accept-Encoding
12 Cache-Control: no-cache,no-store
13 Pragma: no-cache
14 Strict-Transport-Security: max-age=31536000
15
16 [{"app_domain":null,"description":null,"archive_status":0,"biz_type":"dev","master_key": ………………

復(fù)制瀏覽器的cookie訪問
 1 [root@iZ28xbsfvc4Z 20190720]# curl -i -H 'cookie: _ga=GA1.2.2055706705.1560005524; …………' https://leancloud.cn/1.1/clients/self/apps
2 HTTP/1.1 200 OK
3 Server: openresty
4 Date: Sat, 20 Jul 2019 08:11:37 GMT
5 Content-Type: application/json;charset=utf-8
6 Transfer-Encoding: chunked
7 Connection: keep-alive
8 Vary: Accept-Encoding
9 Cache-Control: no-cache,no-store
10 Pragma: no-cache
11 Strict-Transport-Security: max-age=31536000
12
13 [{"app_domain":null,"description":null,"archive_status":0,"biz_type":"dev","master_key": ………………
Linux curl 表單登錄或提交與cookie使用

由上可知curl登錄成功。

推薦內(nèi)容