本文主要讲解通过curl 实现表单提交登录。单独的表单提交与表单登录都差不多,因此就不单独说了。
说明:针对curl表单提交实现登录,不是所有网站都适用,原因是有些网站后台做了限制或有其他校验。我们不知道这些网站后台的限制或校验机制具体是什么,因此直接curl表单登录可能是不行的。
当然,如下案例是可以用curl登录的。
要求:通过curl登录后,能正常访问leancloud的应用页面。
登录页面链接如下:
1 https://leancloud.cn/dashboard/login.html#/signin
能正常访问如下页面:
1 https://leancloud.cn/dashboard/applist.html#/apps
浏览器访问效果:
上图红框 403 中的访问连接如下:
1 https://leancloud.cn/1.1/clients/self/apps
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."}
通过下图可得到表单提交的链接信息。具体如下:
1 https://leancloud.cn/1.1/signin
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
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:创建并可以读取变量的域名。
flag:一个 true/false 值,表明给定域中的所有机器是否都可以访问该变量。此值由浏览器自动设置,具体取决于你为域设置的值。
path:变量在域中有效的路径。
secure:一个 true/false 值,表明是否需要与域的安全连接来访问变量。
expiration:该变量将过期的unix时间。unix时间定义为自1970年1月1日00:00:00 gmt开始的秒数。
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."}
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": ………………
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": ………………
由上可知curl登录成功。
如果觉得不错就点个赞呗 (-^o^-) !
———end———-
如对本文有疑问, 点击进行留言回复!!
linux下文本编辑器vim的使用方法(复制、粘贴、替换、行号、撤销、多文件操作)
网友评论