Delphi Indy login and download a file -


problem cant download file (cause im not logged). can login, nothing more. delphi version: xe2, indy version: 10.5.8.0 (also tested on delphi xe4 newest indy). there 1 cookie 1970r date. help.

code: (login , password real)

var   idhttp: tidhttp;   request: tstringlist;   response: tmemorystream;   file: tmemorystream; begin   try     response := tmemorystream.create;     try       request := tstringlist.create;       try         request.add('user[login]=1212test1212');         request.add('user[password]=test1212');         idhttp := tidhttp.create;         file := tmemorystream.create;         try           idhttp.allowcookies := true;           idhttp.cookiemanager:=form1.idcookiemanager1;           idhttp.handleredirects := true;           idhttp.request.contenttype := 'application/x-www-form-urlencoded';           idhttp.post('http://www.twitch.tv/login', request, response);           idhttp.get('http://www.twitch.tv/broadcast/fmle3_config',file);           file.savetofile('c:\aaa.xml');                   idhttp.free;           ms.free;         end;               request.free;       end;           response.free;     end;   except     on e: exception       showmessage(e.message);   end; end; 

http analyzer logs:

(status-line):http/1.1 302 moved temporarily cache-control:no-cache, no-store, must-revalidate pragma:no-cache set-cookie:feature_bucket=83; domain=.twitch.tv; path=/ set-cookie:name=1212test1212; domain=.twitch.tv; path=/; expires=tue, 06-aug-2013 19:20:52 gmt set-cookie:_twitch_session_id=16d9fc8368fe22701324c7ce39c9f668; domain=.twitch.tv; path=/; expires=wed, 24-jul-2013 07:20:52 gmt; httponly set-cookie:unique_id=a5b16cf36861154f9dd118e673fd4aa0; domain=.twitch.tv; path=/ set-cookie:persistent=46516706_1212test1212_35ptd94szs8ysrpfcckrcx21t; domain=.twitch.tv; path=/; expires=tue, 06-aug-2013 19:20:52 gmt; httponly set-cookie:login=1212test1212; domain=.twitch.tv; path=/; expires=tue, 06-aug-2013 19:20:52 gmt set-cookie:last_login=tue+jul+23+19%3a20%3a52+utc+2013; domain=.twitch.tv; path=/; expires=tue, 06-aug-2013 19:20:52 gmt set-cookie:language=en; domain=.twitch.tv; path=/; expires=sat, 23-jul-2033 19:20:52 gmt set-cookie:api_token=daa32f51e6d015daf9c7a1949d1ef9e0; domain=.twitch.tv; path=/; expires=tue, 06-aug-2013 19:20:52 gmt content-length:144 content-type:text/html location:http://www.twitch.tv/ server:nginx front-end-https:off p3p:cp="cao psa our" status:302 found x-geo:pl x-rack-cache:invalidate, pass x-request-id:a829998aa94c51788ec9b504656639f3 x-runtime:0.272887 x-ua-compatible:ie=edge,chrome=1 date:tue, 23 jul 2013 19:20:52 gmt connection:close  (status-line):http/1.1 200 ok cache-control:no-cache, no-store, must-revalidate pragma:no-cache set-cookie:_twitch_session_id=860dc07ccd1dd937f1cb6adfc84a7f14; domain=.twitch.tv; path=/; expires=wed, 24-jul-2013 07:20:53 gmt; httponly set-cookie:persistent=; domain=.twitch.tv; path=/; expires=thu, 01-jan-1970 00:00:00 gmt content-length:59850 content-type:text/html; charset=utf-8 server:nginx front-end-https:off status:200 ok x-geo:pl x-rack-cache:invalidate, pass x-request-id:2557352c302a3eddfe9080882aaa3028 x-runtime:0.301446 x-ua-compatible:ie=edge,chrome=1 date:tue, 23 jul 2013 19:20:53 gmt connection:close  (status-line):http/1.1 302 moved temporarily cache-control:must-revalidate, no-cache, no-store, private pragma:no-cache set-cookie:_twitch_session_id=860dc07ccd1dd937f1cb6adfc84a7f14; domain=.twitch.tv; path=/; expires=wed, 24-jul-2013 07:20:54 gmt; httponly content-length:150 content-type:text/html location:http://www.twitch.tv/signup server:nginx front-end-https:off status:302 found x-geo:pl x-rack-cache:miss x-request-id:b35e5b998a633b80cfb1f53373e5627a x-runtime:0.029409 x-ua-compatible:ie=edge,chrome=1 date:tue, 23 jul 2013 19:20:54 gmt connection:keep-alive 

http://img197.imageshack.us/img197/7007/xgyf.jpg

you not logging in correct url. http://www.twitch.tv/login html login form users fill out in web browser , submit real login url, https://secure.twitch.tv/user/login instead. if @ html login form, see that, several hidden form fields need submit not submitting.

also, because actual login url using https, need attach ssl iohandler tidhttp, such tidssliohandlersocketopenssl.

and complicate things, required authenticity_token value dynamically generated when http://www.twitch.tv/login requested, need first download html login form, manually parse out token value, , finish login submission.

try this:

var   idhttp: tidhttp;   idssl: tidssliohandlersocketopenssl;   html, authtoken: string;   request: tstringlist;   file: tmemorystream; begin   try     idhttp := tidhttp.create;     try       idssl := tidssliohandlersocketopenssl.create(idhttp);       idhttp.iohandler := idssl;        idhttp.allowcookies := true;       idhttp.cookiemanager := form1.idcookiemanager1;       idhttp.handleredirects := true;        html := idhttp.get('http://www.twitch.tv/login');       authtoken := ...; // parse html extract authenticity_token value...        request := tstringlist.create;       try         request.add('utf8=✓');         request.add('authenticity_token=' + authtoken);         request.add('redirect_on_login=http://www.twitch.tv/');         request.add('embed_form=false');         request.add('user[login]=1212test1212');         request.add('user[password]=test1212');          idhttp.post('https://secure.twitch.tv/user/login', request, nil, indyutf8encoding);               request.free;       end;        file := tmemorystream.create;       try         idhttp.get('http://www.twitch.tv/broadcast/fmle3_config', file);         file.savetofile('c:\aaa.xml');               file.free;       end;           idhttp.free;     end;   except     on e: exception       showmessage(e.message);   end; end; 

with said, because login form performs redirect specified url when login successful, might try shorter approach avoid 1 roundtrip:

var   idhttp: tidhttp;   idssl: tidssliohandlersocketopenssl;   html, authtoken: string;   request: tstringlist;   file: tmemorystream; begin   try     idhttp := tidhttp.create;     try       idssl := tidssliohandlersocketopenssl.create(idhttp);       idhttp.iohandler := idssl;        idhttp.allowcookies := true;       idhttp.cookiemanager := form1.idcookiemanager1;       idhttp.handleredirects := true;        html := idhttp.get('http://www.twitch.tv/login');       authtoken := ...; // parse html extract authenticity_token value...        request := tstringlist.create;       try         request.add('utf8=✓');         request.add('authenticity_token=' + authtoken);         request.add('redirect_on_login=http://www.twitch.tv/broadcast/fmle3_config');         request.add('embed_form=false');         request.add('user[login]=1212test1212');         request.add('user[password]=test1212');          file := tmemorystream.create;         try           idhttp.post('https://secure.twitch.tv/user/login', request, file, indyutf8encoding);           file.savetofile('c:\aaa.xml');                   file.free;         end;               request.free;       end;           idhttp.free;     end;   except     on e: exception       showmessage(e.message);   end; end; 

Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -