delphi - Login OK to site with TwebBrowser , but not with TidHTTP -
may ask little using indy login website please?
firstly, 'proof of concept' used twebbrowser test credentials in following manner ...
procedure tfrmmain.cxbutton1click(sender: tobject); begin webbrow.navigate('http://assurance.redtractor.org.uk/rtassurance/services.eb'); end; procedure tfrmmain.webbrowdocumentcomplete(asender: tobject; const pdisp: idispatch; var url: olevariant); var currentbrowser: iwebbrowser2; topbrowser: iwebbrowser2; document: olevariant; doc3 : ihtmldocument3; frm : ihtmlformelement; begin currentbrowser := pdisp iwebbrowser2; topbrowser := (asender twebbrowser).defaultinterface; if assigned(currentbrowser) , assigned(topbrowser) begin if currentbrowser = topbrowser begin doc3 := currentbrowser.document ihtmldocument3; webbrow.ondocumentcomplete := nil; // remove handler avoid reentrance doc3.getelementbyid('el9m9aqxil51ji3_loginpnl_username').setattribute('value', 'avalidusername', 0); doc3.getelementbyid('el9m9aqxil51ji3_loginpnl_password').setattribute('value', 'avalidpassword', 0); //frm := doc3.getelementbyid('ct100') ihtmlformelement; doc3.getelementbyid('el9m9aqxil51ji3_loginpnl_button').click(); end; end; end;
i got above whosrdaddy answer here automated log in (webbrowser)
that logs me site , takes me search page ... need.
however, i'd avoid using twebbrowser thought searches slow due fact page need rendered. in mind tried use indy 10 login same address, passing parameters ...
idredtractor.post(login_url, request, response);
but returns 'server error, unauthenticated username' response.
my full code trying login ...
procedure tfrmmain.btnlogonclick(sender: tobject); var response : tmemorystream; searchresp : tmemorystream; request : tstringlist; searchreq : tstringlist; resultstr : tstringlist; begin idredtractor begin allowcookies := true; cookiemanager := cookieredtractor; iohandler := idsslredtractor; request.accept := 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'; request.contenttype := 'text/html'; request.useragent := 'mozilla/3.0 (compatible; indy library)'; end; idsslredtractor begin // ssloptions not make difference. still server error message ssloptions.mode := sslmunassigned; //ssloptions.mode := sslmboth; //ssloptions.mode := sslmclient; //ssloptions.mode := sslmserver; end; try try response := tmemorystream.create; searchresp := tmemorystream.create; try request := tstringlist.create; searchreq := tstringlist.create; resultstr := tstringlist.create; // individual params via firebug request.add('__eventargument=login'); request.add('__eventtarget=el9m9aqxil51ji3$loginpnl'); request.add('__viewstate=/wepdwulltezmjc3nzq0odepzbyeagepzbyczg9kfgjmdxychgruzxh0brndagvja2vycyamifnlcnzpy2vzzaidd2qwbaicdxychgdwaxnpymxlagqccq9kfgicag9kfgicba8wah8bzxycagepfgifawhkzd3t1ydwd12+6szzogvhrnka9lkb'); request.add('__viewstategenerator=9d5bca8c'); request.add('ebabpwd=' + edtuserpass.text); request.add('ebabpwd='); request.add('ebabuser=' + edtusername.text); request.add('ebabuser='); request.add('el9m9aqxil51ji3$loginpnl_...=' + edtusername.text); request.add('el9m9aqxil51ji3$loginpnl_...=' + edtuserpass.text); request.add('el9ok3xx11wqs60_email=');{} idredtractor.request.referer := 'http://assurance.redtractor.org.uk/rtassurance/schemes.eb';//initial_url; idredtractor.post('http://assurance.redtractor.org.uk/rtassurance/services.eb', request, response); if idredtractor.responsecode = 200 begin resultstr.clear; response.position := 0; resultstr.loadfromstream(response); mmoresponse.lines.addstrings(resultstr); end; request.free; searchreq.free; resultstr.free; end; response.free; searchresp.free; end; except on e: exception showmessage(e.message); end; end;
just case there value in versions of ssl dll's, 'libeay32.dll' v1.0.1.3 , 'ssleay32.dll', v1.0.1.3.
may ask please in understanding have missed or done wrong prevents me logging site tidhttp?
ok, found problem. site doing redirect same page after post login request. key solution setting handleredirects
true , change vmethod
variable in onhandleredirect
event. cleaned code bit:
unit so35263785test; interface uses idhttp, sysutils, strutils, stdctrls, classes, controls, forms; type tform1 = class(tform) memo1: tmemo; button1: tbutton; procedure button1click(sender: tobject); private { private declarations } client : tidhttp; procedure handleredirect(sender: tobject; var dest: string; var numredirect: integer; var handled: boolean; var vmethod: tidhttpmethod); procedure logintoredtractor(const username, password : string); public { public declarations } end; var form1: tform1; implementation {$r *.dfm} procedure tform1.handleredirect(sender: tobject; var dest: string; var numredirect: integer; var handled: boolean; var vmethod: tidhttpmethod); begin vmethod := id_httpmethodget; handled := true; end; procedure extractviewstateandgenerator(const html : string; var viewstate : string; var viewstategenerator: string); var ps : integer; begin viewstate := ''; viewstategenerator := ''; // assume __viewstate , __viewstategenerator inputs there, no error checking ps := pos('__viewstate', html); ps := posex('value', html, ps); ps := posex('"', html, ps); viewstate := copy(html, ps+1, posex('"', html, ps+1)-ps-1); ps := pos('__viewstategenerator', html); ps := posex('value', html, ps); ps := posex('"', html, ps); viewstategenerator := copy(html, ps+1, posex('"', html, ps+1)-ps-1); end; procedure tform1.logintoredtractor(const username, password : string); var getresponse : string; request : tstringlist; viewstate : string; viewstategenerator : string; begin client := tidhttp.create; try client.protocolversion := pv1_1; client.httpoptions := [hoforceencodeparams, hokeeporigprotocol]; client.allowcookies := true; client.handleredirects := true; client.request.useragent := 'mozilla/5.0 (windows nt 6.3; wow64) applewebkit/537.36 (khtml, gecko) chrome/48.0.2564.103 safari/537.36'; client.onredirect := handleredirect; getresponse := client.get('http://assurance.redtractor.org.uk/rtassurance/schemes.eb'); extractviewstateandgenerator(getresponse, viewstate, viewstategenerator); request := tstringlist.create; try request.add('__viewstate='+viewstate); request.add('__viewstategenerator='+viewstategenerator); request.add('__eventtarget=el9m9aqxil51ji3$loginpnl'); request.add('el9m9aqxil51ji3$loginpnl_username='+username); request.add('el9m9aqxil51ji3$loginpnl_password='+password); client.request.referer := client.url.uri; memo1.text := client.post('http://assurance.redtractor.org.uk/rtassurance/services.eb', request); request.free; end; client.free; end; end; procedure tform1.button1click(sender: tobject); begin logintoredtractor('myusername', 'mypassword'); end; end
this code has been verified , works in delphi xe.
Comments
Post a Comment