登录命令行

时间:2016-07-19 13:21:32

标签: http unix curl wget

我希望在特定网址上执行GET请求,但在能够做到这一点之前,我必须建立连接。 从逻辑上讲,我必须连接到网站。

我尝试使用curl和wget,类似的东西:

curl -d server_name=www.starsquest.co.uk&uni=1&username=MYUSERNAME&password=MYPASSWORD&x=0&y=0" --dump-header headers http://s2.starsquest.co.uk/?page=login

连接似乎有效,我得到类似的东西:

HTTP/1.1 302 Found
X-Powered-By: PHP/5.4.36-0+deb7u3
Content-Type: text/html; charset=UTF-8
P3P: CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"
Set-Cookie: StarsQuest=1asb9t24g9l26lpdu207h8rms2; expires=Sun, 24-Jul-2016 07:20:17 GMT; path=/; HttpOnly
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Location: http://s2.starsquest.co.uk/game.php?mode=login
Content-Length: 0
Date: Tue, 19 Jul 2016 07:20:17 GMT
Server: lighttpd/1.4.31

下一个请求是:

curl -H "Accept: application/xml" -H "Content-Type: application/json" -X GET http://s2.starsquest.co.uk/game.php?page=showfleets&ajax=1

但是如何使用预先建立的连接?有可能吗?

感谢您的帮助

3 个答案:

答案 0 :(得分:1)

在两个请求中使用cookie-jar选项:

curl --cookie-jar cookies.txt -d "server_name=www.starsquest.co.uk&uni=1&username=MYUSERNAME&password=MYPASSWORD&x=0&y=0" --dump-header headers "http://s2.starsquest.co.uk/?page=login"

curl --cookie-jar cookies.txt -H "Accept: application/xml" -H "Content-Type: application/json" -X GET "http://s2.starsquest.co.uk/game.php?page=showfleets&ajax=1"

这将在发出请求之前读取给定文件中的cookie,并在完成后将新文件存储在其中,因此它将允许您存储会话数据。

答案 1 :(得分:0)

您需要遵循重定向。

curl -L -d“server_name = www.starsquest.co.uk& uni = 1& username = MYUSERNAME& password = MYPASSWORD& x = 0& y = 0”--dump-header headers http://s2.starsquest.co.uk/?page=login

在您下一个请求中添加从上一个请求转储到文件的标头。它应该是这个。

curl -L --dump-header headers -H“日期:星期二,2016年7月19日13:46:50 GMT”-H“内容类型:text / html; charset = ISO-8859-1”-H “Transfer-Encoding:chunked”-H“Connection:keep-alive”-H“Set-Cookie:__ cffidid = daf5017ceaf3bd2369687ad910be2a3cf1468936009; expires = Wed,19-Jul-17 13:46:49 GMT; path = /; domain =。 starsquest.co.uk; HttpO $“-H”X-Powered-By:PHP / 5.4.4-14 + deb7u14“-H”Set-Cookie:proxy_check = 1; expires = Tue,26-Jul-2016 13: 53:33 GMT“-H”Set-Cookie:starsquestsession_id = 4f565c147ac949fa451020ae4c6b5ed0; path = /; domain = .starsquest.co.uk; httponly“-H”Set-Cookie:universe_var = 2; expires = Wed,19-Jul- 2017 13:53:33 GMT“-H”缓存控制:无缓存,必须重新验证,max-age = 0“-H”到期:星期一,2016年7月18日13:53:33 GMT“-H”Pragma :no-cache“-H”变化:接受编码“-H”服务器:cloudflare-nginx“-H”CF-RAY:2c4e9f2d680b28d8-DXB“-X GET http://s2.starsquest.co.uk/game.php?page=showfleets&ajax=1

请注意,在没有明确许可的情况下刮取某些网站是不道德/非法的。

答案 2 :(得分:0)

像@kryzk解决方案一样使用它,但添加“-L”

curl --cookie-jar cookies.txt -L -d“server_name = www.starsquest.co.uk& uni = 1& username = MYUSERNAME& password = MYPASSWORD& x = 0& y = 0”--dump -header标题“http://s2.starsquest.co.uk/?page=login

curl --cookie-jar cookies.txt -L -H“接受:application / xml”-H“Content-Type:application / json”-X GET“http://s2.starsquest.co.uk/game.php?page=showfleets&ajax=1