2016-07-28 94 views
0

我正在嘗試在我的項目中實現HTTP客戶端,我無法登錄到我的帳戶,我得到了Forbidden !, IdHTTP工作正常,什麼是缺失或錯誤我的代碼?如何在Delphi中使用HTTP客戶端登錄到網站xe

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.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; 
+0

那麼,對於初學者來說,你不能真正開始使用'TIdHTTP'登錄,然後用'TNetHTTPClient'完成登錄,它們不會像Cookie一樣共享彼此的狀態信息。如果您要顯示完整的適用於您的登錄的「TIdHTTP」代碼,然後有人可以幫助您將該登錄名轉換爲「TNetHTTPClient」,那將會更加有用。 –

+0

@RemyLebeau酷,已添加 – cyberdude

回答