Тема: Delphi
Показать сообщение отдельно

  #3  
Старый 12.05.2010, 22:36
heretic1990
Постоянный
Регистрация: 02.07.2008
Сообщений: 472
Провел на форуме:
3728999

Репутация: 444
По умолчанию

Пример реализации авторизации на сервисе VKontakte.ru с использованием компонента Indy 10

после правильной авторизации должен вывести id страницы и имя
Код:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes,
  Graphics, Controls, Forms, Dialogs, StdCtrls,
  idHTTP, idCookieManager, IdCookie;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Button1: TButton;
    Label3: TLabel;
    Label4: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  http:TidHttp;
  CooM:TidCookieManager;
  data:TStringList;
  page, id, name:String; 
begin
  http:=TIdHTTP.Create(Self);
  data :=TStringList.Create;
  CooM:=TidCookieManager.Create(http);
  http.AllowCookies:=true;
  http.CookieManager:=CooM;
  http.HandleRedirects:=true;

 {****Включить по желанию****}

  //  http.Request.Host:='vkontakte.ru';
  //  http.Request.UserAgent:='Opera/9.51 (Windows NT 5.1; U; ru)';
  //  http.Request.Accept:='text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5';
  //  http.Request.AcceptLanguage:='ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3';
  //  http.Request.AcceptEncoding:='gzip,deflate';
  //  http.Request.AcceptCharSet:='windows-1251,utf-8;q=0.7,*;q=0.7';
  //  http.Request.Referer:='http://vkontakte.ru/u=1';

  data.Add('u=1');
  data.Add('success_url=');
  data.Add('fail_url=');
  data.Add('try_to_login=1');
  data.Add('email='+Edit1.Text);
  data.Add('pass='+Edit2.Text);
  Page:= http.Post('http://vkontakte.ru/login.php?', data);
  data.Free;
  Coom.Free;
  http.Free;
  name:='';
  name:=copy(page,Pos('<h1> <b>Online</b>',page)+length('<h1> <b>Online</b>'),Pos('<span>',page)-(Pos('<h1> <b>Online</b>',page)+length('<h1> <b>Online</b>')));
  if Pos('<h1> <b>Online</b>',page)<>0 then Label4.Caption:='Name :: '+name;
  id:='';
  if Pos('<input type="hidden" id="mid" value="',page)<>0 then delete(page,1,Pos('<input type="hidden" id="mid" value="',page)-1);
  id:=copy(page,Pos('<input type="hidden" id="mid" value="',page)+length('<input type="hidden" id="mid" value="'),Pos('">',page)-(Pos('<input type="hidden" id="mid" value="',page)+length('<input type="hidden" id="mid" value="')));
  if Pos('<input type="hidden" id="mid" value="',page)<>0 then Label3.Caption:='ID :: '+id;

end;
end.
 
Ответить с цитированием