如何在delphixe中使用HTTP客户端登录网站
我正在尝试在我的项目中实现HTTP客户端,我无法登录到我的帐户,我被禁止!,有了如何在delphixe中使用HTTP客户端登录网站,http,delphi,delphi-xe7,Http,Delphi,Delphi Xe7,我正在尝试在我的项目中实现HTTP客户端,我无法登录到我的帐户,我被禁止!,有了IdHTTP它工作得很好,我的代码中有什么遗漏或错误 NetHTTPClient1属性: Connectiontimeout = 30000 AllowCookies = True HandleRedirects = True UserAgent = Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHT
IdHTTP
它工作得很好,我的代码中有什么遗漏或错误
NetHTTPClient1属性:
Connectiontimeout = 30000
AllowCookies = True
HandleRedirects = True
UserAgent = Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36
NetHTTPRequest1属性:
Method String = POST
URL=https://www.instagram.com/accounts/web_create_ajax/attempt/
代码:
procedure TForm2.按钮1单击(发送方:TObject);
变量
参数:t字符串;
lHTTP:TIdHTTP;
IdSSL:TIdSSLIOHandlerSocketOpenSSL;
N:整数;
令牌,电子邮件,S:string;
答复:TStringList,;
曲奇:小甜饼;
开始
lHTTP:=TIdHTTP.Create(nil);
尝试
IdSSL:=TIdSSLIOHandlerSocketOpenSSL.Create(lHTTP);
IdSSL.SSLOptions.Method:=sslvTLSv1;
IdSSL.SSLOptions.Mode:=sslmClient;
lHTTP.IOHandler:=IdSSL;
lHTTP.ReadTimeout:=30000;
lHTTP.HandleRedirects:=True;
lHTTP.Request.UserAgent:=“Mozilla/5.0(Linux;Android 6.0;Nexus 5 Build/MRA58N)AppleWebKit/537.36(KHTML,像Gecko)Chrome/46.0.2490.76 Mobile Safari/537.36”;
lHTTP.Get('https://www.instagram.com",湾仔(无);;
Cookie:=lHTTP.CookieManager.CookieCollection.Cookie['csrftoken','www.instagram.com'];
如果是零,那么
令牌:=Cookie.Value;
最后
结束;
尝试
Params:=TStringList.Create;
Add('username='+'myusername');
Add('password='+'mypassword');
NetHTTPClient1.CustomHeaders['X-CSRFToken']:=Token;
NetHTTPClient1.CustomHeaders['X-Instagram-AJAX']:='1';
NetHTTPClient1.CustomHeaders['X-request-With']:=“XMLHttpRequest';
NetHTTPClient1.CustomHeaders['Referer']:='https://www.instagram.com/';
Memo1.Lines.Add(NetHTTPRequest1.Post)https://www.instagram.com/accounts/login/ajax/,参数)。状态文本);
最后
结束;
///使用IdHTTP///Wroks登录//
尝试
lHTTP.Request.CustomHeaders.Values['X-CSRFToken']:=Token;
lHTTP.Request.CustomHeaders.Values['X-Instagram-AJAX']:=“1”;
lHTTP.Request.CustomHeaders.Values['X-Requested-With']:='XMLHttpRequest';
lHTTP.Request.Referer:='https://www.instagram.com/';
lHTTP.Request.ContentType:=“application/x-www-form-urlencoded”;
lHTTP.Request.UserAgent:=“Mozilla/5.0(Linux;Android 6.0;Nexus 5 Build/MRA58N)AppleWebKit/537.36(KHTML,像Gecko)Chrome/46.0.2490.76 Mobile Safari/537.36”;
回复:=lHTTP.Post('https://www.instagram.com/accounts/login/ajax/,参数);
备忘录1.行。添加(回复);
结束;
TNetHTTPClient与handleRedirect和post之间存在错误
登录后,您将收到cookie(某种方式的密钥),并且您必须在所有未来连接中使用这些cookie。TNetHTTPClient与handleRedirect和post存在漏洞 登录后,您将收到cookie(某种方式的密钥),并且您必须在所有未来连接中使用这些cookie。“TNetHTTPClient与handleRedirect和post存在漏洞。” 这已经在版本10.2 Tokyo Release 2中得到了修复,“TNetHTTPClient在handleRedirect和post中存在错误。”
这在版本10.2 Tokyo Release 2中已经得到了修复。首先,您不能使用
TIdHTTP
启动登录,然后使用TNetHTTPClient
完成登录,它们之间不共享状态信息,就像cookies一样。如果你能展示你登录时使用的完整的TIdHTTP
代码,然后有人能帮你将登录翻译成TNetHTTPClient
@RemyLebeau cool,AddedWell,那就更有用了。首先,你不能用TIdHTTP
开始登录,然后用TNetHTTPClient
完成登录,他们彼此不共享状态信息,就像cookies一样。他补充道:“如果你能展示你的完整的TIdHTTP
登录代码,然后有人能帮你将该登录翻译成TNetHTTPClient
@RemyLebeau-cool,那就更有用了。”
procedure TForm2.Button1Click(Sender: TObject);
var
Params : TStrings;
lHTTP: TIdHTTP;
IdSSL : TIdSSLIOHandlerSocketOpenSSL;
N: Integer;
Token,email,S: string;
Reply: TStringList;
Cookie: TIdCookie;
begin
lHTTP := TIdHTTP.Create(nil);
try
IdSSL := TIdSSLIOHandlerSocketOpenSSL.Create(lHTTP);
IdSSL.SSLOptions.Method := sslvTLSv1;
IdSSL.SSLOptions.Mode := sslmClient;
lHTTP.IOHandler := IdSSL;
lHTTP.ReadTimeout := 30000;
lHTTP.HandleRedirects := True;
lHTTP.Request.UserAgent := 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36';
lHTTP.Get('https://www.instagram.com', TStream(nil));
Cookie := lHTTP.CookieManager.CookieCollection.Cookie['csrftoken', 'www.instagram.com'];
if Cookie <> nil then
Token := Cookie.Value;
finally
end;
try
Params := TStringList.Create;
Params.Add('username=' +'myusername');
Params.Add('password=' + 'mypassword');
NetHTTPClient1.CustomHeaders['X-CSRFToken'] := Token;
NetHTTPClient1.CustomHeaders['X-Instagram-AJAX'] := '1';
NetHTTPClient1.CustomHeaders['X-Requested-With'] := 'XMLHttpRequest';
NetHTTPClient1.CustomHeaders['Referer'] := 'https://www.instagram.com/';
Memo1.Lines.Add(NetHTTPRequest1.Post('https://www.instagram.com/accounts/login/ajax/', Params).StatusText);
finally
end;
///login with IdHTTP///Wroks//
try
lHTTP.Request.CustomHeaders.Values['X-CSRFToken'] := Token;
lHTTP.Request.CustomHeaders.Values['X-Instagram-AJAX'] := '1';
lHTTP.Request.CustomHeaders.Values['X-Requested-With'] := 'XMLHttpRequest';
lHTTP.Request.Referer := 'https://www.instagram.com/';
lHTTP.Request.ContentType := 'application/x-www-form-urlencoded';
lHTTP.Request.UserAgent := 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36';
Reply := lHTTP.Post('https://www.instagram.com/accounts/login/ajax/', Params);
Memo1.Lines.Add(Reply);
end;