|
03.02.2015, 05:48
#38869994
Ссылка:
Ссылка на сообщение:
Ссылка с названием темы:
|
|
|
|
Всем здравствуйте.
Пытаюсь залогиниться на joyreactor.cc
Опыта в таких вещах нет, поэтому как делать не знаю. После некоторого гугления родилось что-то такое (решено было использовать Apache HttpClient 4.3.6 и JerichoParser):
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94.
private static CloseableHttpClient httpclient;
private static void printCookies(BasicCookieStore cookieStore) {
List<Cookie> cookies = cookieStore.getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i).toString());
}
}
}
private static CloseableHttpResponse executeRequest(HttpUriRequest request,
String origin, String referer) throws IOException,
ClientProtocolException {
request.setHeader(
"User-Agent",
"Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.94 Safari/537.36");
request.setHeader("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
request.setHeader("Accept-Language", "en-US,en;q=0.8,ru;q=0.6");
if (origin != null) {
request.setHeader("Origin", origin);
}
if (referer != null) {
request.setHeader("Referer", referer);
}
CloseableHttpResponse response = httpclient.execute(request);
System.out.println(response.getStatusLine());
return response;
}
public static void main(String[] args) throws Exception {
PropertyConfigurator.configure("log4j.properties");
BasicCookieStore cookieStore = new BasicCookieStore();
httpclient = HttpClients.custom()
.setConnectionManager(new BasicHttpClientConnectionManager())
.setDefaultCookieStore(cookieStore).build();
String token = null;
{
HttpGet httpget = new HttpGet("http://joyreactor.cc/login");
CloseableHttpResponse response = executeRequest(httpget, null, null);
try {
InputStream in = response.getEntity().getContent();
Source source = new Source(in);
List<Element> inputs = source.getAllElements("input");
for (Element input : inputs) {
String name = input.getAttributeValue("name");
if (name != null && name.equals("signin[_csrf_token]")) {
token = input.getAttributeValue("value");
break;
}
}
System.out.println("Initial cookies:");
printCookies(cookieStore);
} finally {
response.close();
}
System.out.println("Token: " + token);
}
{
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("signin[username]",
"someuser"));
formparams.add(new BasicNameValuePair("signin[password]",
"somepass"));
formparams
.add(new BasicNameValuePair("signin[_csrf_token]", token));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams,
Consts.UTF_8);
HttpPost httppost = new HttpPost("http://joyreactor.cc/login");
httppost.setEntity(entity);
CloseableHttpResponse response = executeRequest(httppost,
"http://joyreactor.cc", "http://joyreactor.cc/login");
try {
InputStream in = response.getEntity().getContent();
Source source = new Source(in);
System.out.println("Post logon cookies:");
printCookies(cookieStore);
} finally {
response.close();
}
}
httpclient.close();
}
Второй запрос вместо HTTP 302 отдает 200 и в контенте страница для логина. Initial cookies и Post logon cookies одинаковые.
Вот то, что в логах:
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116.
DEBUG [org.apache.http.client.protocol.RequestAddCookies] CookieSpec selected: best-match
DEBUG [org.apache.http.client.protocol.RequestAuthCache] Auth cache not set in the
context
DEBUG [org.apache.http.impl.conn.BasicHttpClientConnectionManager] Get connection for
route {}->http://joyreactor.cc:80
DEBUG [org.apache.http.impl.execchain.MainClientExec] Opening connection {}->
http://joyreactor.cc:80
DEBUG [org.apache.http.impl.conn.HttpClientConnectionOperator] Connecting to
joyreactor.cc/50.7.136.178:80
DEBUG [org.apache.http.impl.conn.HttpClientConnectionOperator] Connection established
192.168.0.10:4744<->50.7.136.178:80
DEBUG [org.apache.http.impl.execchain.MainClientExec] Executing request GET /login
HTTP/1.1
DEBUG [org.apache.http.impl.execchain.MainClientExec] Target auth state: UNCHALLENGED
DEBUG [org.apache.http.impl.execchain.MainClientExec] Proxy auth state: UNCHALLENGED
DEBUG [org.apache.http.headers] http-outgoing-0 >> GET /login HTTP/1.1
DEBUG [org.apache.http.headers] http-outgoing-0 >> User-Agent: Mozilla/5.0 (Windows NT
5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.94 Safari/537.36
DEBUG [org.apache.http.headers] http-outgoing-0 >> Accept:
text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
DEBUG [org.apache.http.headers] http-outgoing-0 >> Accept-Language:
en-US,en;q=0.8,ru;q=0.6
DEBUG [org.apache.http.headers] http-outgoing-0 >> Host: joyreactor.cc
DEBUG [org.apache.http.headers] http-outgoing-0 >> Connection: Keep-Alive
DEBUG [org.apache.http.headers] http-outgoing-0 >> Accept-Encoding: gzip,deflate
DEBUG [org.apache.http.headers] http-outgoing-0 << HTTP/1.1 401 Unauthorized
DEBUG [org.apache.http.headers] http-outgoing-0 << Server: nginx/1.6.2
DEBUG [org.apache.http.headers] http-outgoing-0 << Date: Mon, 02 Feb 2015 16:32:43 GMT
DEBUG [org.apache.http.headers] http-outgoing-0 << Content-Type: text/html; charset=utf-8
DEBUG [org.apache.http.headers] http-outgoing-0 << Transfer-Encoding: chunked
DEBUG [org.apache.http.headers] http-outgoing-0 << Connection: keep-alive
DEBUG [org.apache.http.headers] http-outgoing-0 << X-Powered-By: PHP/5.4.37
DEBUG [org.apache.http.headers] http-outgoing-0 << Cache-Control: private
DEBUG [org.apache.http.headers] http-outgoing-0 << Set-Cookie:
joyreactor=ver3fe15a757690ddab4ef41baffb8916e77:2cc2d86401ae1c54f559991f87beefd3bc32bd76;
expires=Thu, 05-Feb-2015 16:32:43 GMT; path=/; domain=.joyreactor.cc; httponly
DEBUG [org.apache.http.headers] http-outgoing-0 << Content-Encoding: gzip
DEBUG [org.apache.http.headers] http-outgoing-0 << Vary: Accept-Encoding
DEBUG [org.apache.http.impl.execchain.MainClientExec] Connection can be kept alive
indefinitely
DEBUG [org.apache.http.impl.auth.HttpAuthenticator] Authentication required
DEBUG [org.apache.http.impl.auth.HttpAuthenticator] joyreactor.cc:80 requested
authentication
DEBUG [org.apache.http.impl.auth.HttpAuthenticator] Response contains no authentication
challenges
DEBUG [org.apache.http.client.protocol.ResponseProcessCookies] Cookie accepted
[joyreactor="ver3fe15a757690ddab4ef41baffb8916e77:2cc2d86401ae1c54f559991f87beefd3bc32bd7
6", version:0, domain:.joyreactor.cc, path:/, expiry:Fri Feb 06 00:32:43 CST 2015]
HTTP/1.1 401 Unauthorized
DEBUG [org.apache.http.impl.conn.BasicHttpClientConnectionManager] Releasing connection
192.168.0.10:4744<->50.7.136.178:80
DEBUG [org.apache.http.impl.conn.BasicHttpClientConnectionManager] Connection can be
kept alive indefinitely
Initial cookies:
- [version: 0][name: joyreactor][value:
ver3fe15a757690ddab4ef41baffb8916e77:2cc2d86401ae1c54f559991f87beefd3bc32bd76][domain:
.joyreactor.cc][path: /][expiry: Fri Feb 06 00:32:43 CST 2015]
Token: 1e2cc498f94bcef46655cee4bc451053
DEBUG [org.apache.http.client.protocol.RequestAddCookies] CookieSpec selected: best-match
DEBUG [org.apache.http.client.protocol.RequestAddCookies] Cookie [version: 0][name:
joyreactor][value:
ver3fe15a757690ddab4ef41baffb8916e77:2cc2d86401ae1c54f559991f87beefd3bc32bd76][domain:
.joyreactor.cc][path: /][expiry: Fri Feb 06 00:32:43 CST 2015] match
[joyreactor.cc:80/login]
DEBUG [org.apache.http.client.protocol.RequestAuthCache] Auth cache not set in the
context
DEBUG [org.apache.http.impl.conn.BasicHttpClientConnectionManager] Get connection for
route {}->http://joyreactor.cc:80
DEBUG [org.apache.http.impl.execchain.MainClientExec] Stale connection check
DEBUG [org.apache.http.impl.execchain.MainClientExec] Executing request POST /login
HTTP/1.1
DEBUG [org.apache.http.impl.execchain.MainClientExec] Target auth state: UNCHALLENGED
DEBUG [org.apache.http.impl.execchain.MainClientExec] Proxy auth state: UNCHALLENGED
DEBUG [org.apache.http.headers] http-outgoing-0 >> POST /login HTTP/1.1
DEBUG [org.apache.http.headers] http-outgoing-0 >> User-Agent: Mozilla/5.0 (Windows NT
5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.94 Safari/537.36
DEBUG [org.apache.http.headers] http-outgoing-0 >> Accept:
text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
DEBUG [org.apache.http.headers] http-outgoing-0 >> Accept-Language:
en-US,en;q=0.8,ru;q=0.6
DEBUG [org.apache.http.headers] http-outgoing-0 >> Origin: http://joyreactor.cc
DEBUG [org.apache.http.headers] http-outgoing-0 >> Referer: http://joyreactor.cc/login
DEBUG [org.apache.http.headers] http-outgoing-0 >> Content-Length: 117
DEBUG [org.apache.http.headers] http-outgoing-0 >> Content-Type:
application/x-www-form-urlencoded; charset=UTF-8
DEBUG [org.apache.http.headers] http-outgoing-0 >> Host: joyreactor.cc
DEBUG [org.apache.http.headers] http-outgoing-0 >> Connection: Keep-Alive
DEBUG [org.apache.http.headers] http-outgoing-0 >> Cookie:
joyreactor=ver3fe15a757690ddab4ef41baffb8916e77:2cc2d86401ae1c54f559991f87beefd3bc32bd76
DEBUG [org.apache.http.headers] http-outgoing-0 >> Cookie2: $Version=1
DEBUG [org.apache.http.headers] http-outgoing-0 >> Accept-Encoding: gzip,deflate
DEBUG [org.apache.http.headers] http-outgoing-0 << HTTP/1.1 200 OK
DEBUG [org.apache.http.headers] http-outgoing-0 << Server: nginx/1.6.2
DEBUG [org.apache.http.headers] http-outgoing-0 << Date: Mon, 02 Feb 2015 16:32:45 GMT
DEBUG [org.apache.http.headers] http-outgoing-0 << Content-Type: text/html; charset=utf-8
DEBUG [org.apache.http.headers] http-outgoing-0 << Transfer-Encoding: chunked
DEBUG [org.apache.http.headers] http-outgoing-0 << Connection: keep-alive
DEBUG [org.apache.http.headers] http-outgoing-0 << X-Powered-By: PHP/5.4.37
DEBUG [org.apache.http.headers] http-outgoing-0 << Cache-Control: private
DEBUG [org.apache.http.headers] http-outgoing-0 << Content-Encoding: gzip
DEBUG [org.apache.http.headers] http-outgoing-0 << Vary: Accept-Encoding
DEBUG [org.apache.http.impl.execchain.MainClientExec] Connection can be kept alive
indefinitely
HTTP/1.1 200 OK
DEBUG [org.apache.http.impl.conn.BasicHttpClientConnectionManager] Releasing connection
192.168.0.10:4744<->50.7.136.178:80
DEBUG [org.apache.http.impl.conn.BasicHttpClientConnectionManager] Connection can be
kept alive indefinitely
Post logon cookies:
- [version: 0][name: joyreactor][value:
ver3fe15a757690ddab4ef41baffb8916e77:2cc2d86401ae1c54f559991f87beefd3bc32bd76][domain:
.joyreactor.cc][path: /][expiry: Fri Feb 06 00:32:43 CST 2015]
DEBUG [org.apache.http.impl.conn.BasicHttpClientConnectionManager] Shutting down
connection
DEBUG [org.apache.http.impl.conn.DefaultManagedHttpClientConnection] http-outgoing-0:
Shutdown connection
Для сравнения то, что видно в дебаггере chrom'а, когда логинюсь через браузер:
Первый запрос:
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27.
Remote Address:50.7.136.178:80
Request URL:http://joyreactor.cc/login
Request Method:GET
Status Code:401 Unauthorized
Request Headers
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8,ru;q=0.6
Connection:keep-alive
Host:joyreactor.cc
User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/40.0.2214.94 Safari/537.36
Response Headers
Cache-Control:private
Connection:keep-alive
Content-Encoding:gzip
Content-Type:text/html; charset=utf-8
Date:Mon, 02 Feb 2015 16:30:39 GMT
Server:nginx/1.6.2
Set-Cookie:joyreactor=ver31d60931282ac395f75ed2d67188f3b2d:55e29a350eac674c18d32849dd1b07
c842bd31df; expires=Thu, 05-Feb-2015 16:30:39 GMT; path=/; domain=.joyreactor.cc;
httponly
Transfer-Encoding:chunked
Vary:Accept-Encoding
X-Powered-By:PHP/5.4.37
Второй запрос:
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46.
Remote Address:50.7.136.178:80
Request URL:http://joyreactor.cc/login
Request Method:POST
Status Code:302 Found
Request Headers
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8,ru;q=0.6
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:117
Content-Type:application/x-www-form-urlencoded
Cookie:joyreactor=ver31d60931282ac395f75ed2d67188f3b2d:55e29a350eac674c18d32849dd1b07c842
bd31df; __utmt=1; __utma=181923772.218050553.1422894703.1422894703.1422894703.1;
__utmb=181923772.1.10.1422894703; __utmc=181923772;
__utmz=181923772.1422894703.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none);
__lx181488_load_cnt=1; __lx181488_load_tmr=0; __lx181488_load_tmr_pre=1422894703364;
___ws_ses=742A97B0F7AFD4EB.1; ___ws_vis=742A97B0F7AFD4EB.1422894644619;
___ws_ses_sec=unknown:1422894644619; ___ws_vis_sec=unknown:1422894644619;
__qca=P0-428451837-1422894705122; ws-refr=http://joyreactor.cc/login;
ortcsession-R60tcQ-s=5a9a44799aebe5d0; ortcsession-R60tcQ=5a9a44799aebe5d0
Host:joyreactor.cc
Origin:http://joyreactor.cc
Referer:http://joyreactor.cc/login
User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/40.0.2214.94 Safari/537.36
Form Data
signin[username]:someuser
signin[password]:somepass
signin[_csrf_token]:b484e9f918d191d5d050ff99ea517f51
Response Headers
Connection:keep-alive
Content-Encoding:gzip
Content-Type:text/html; charset=utf-8
Date:Mon, 02 Feb 2015 16:31:23 GMT
Location:http://joyreactor.cc/
Server:nginx/1.6.2
Set-Cookie:joyreactor=ver3e7ff5754f9ec6ef6411ac66daecf38c3:cf1dbe90d67cebc82284fe39de554b
7ae8f45828; expires=Thu, 05-Feb-2015 16:31:23 GMT; path=/; domain=.joyreactor.cc;
httponly
Transfer-Encoding:chunked
Vary:Accept-Encoding
X-Powered-By:PHP/5.4.37
Вроде бы все совпадает, но во втором запросе в Cookies похоже передается что-то еще, помимо того, что получается в первом запросе:
1. 2. 3. 4. 5. 6. 7. 8.
__utmt=1; __utma=181923772.218050553.1422894703.1422894703.1422894703.1;
__utmb=181923772.1.10.1422894703; __utmc=181923772;
__utmz=181923772.1422894703.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none);
__lx181488_load_cnt=1; __lx181488_load_tmr=0; __lx181488_load_tmr_pre=1422894703364;
___ws_ses=742A97B0F7AFD4EB.1; ___ws_vis=742A97B0F7AFD4EB.1422894644619;
___ws_ses_sec=unknown:1422894644619; ___ws_vis_sec=unknown:1422894644619;
__qca=P0-428451837-1422894705122; ws-refr=http://joyreactor.cc/login;
ortcsession-R60tcQ-s=5a9a44799aebe5d0; ortcsession-R60tcQ=5a9a44799aebe5d0
Не знаю что это такое и откуда берется. Может из-за этого не работать?
Или причина в чем-то вообще другом? Может как-то совсем по другому надо делать?
|
|
|