Показать сообщение отдельно
  #10  
Старый 28.12.2010, 10:06
Аватар для NumLock
NumLock NumLock вне форума
Let Me Show You
 
Регистрация: 30.04.2010
Адрес: Северодвинск
Сообщения: 5,426
Версия Delphi: 7, XE5
Репутация: 59586
По умолчанию

ну если так хочется на половину все делать руками, то:
Код:
unit Unit1;

interface

uses
  IdHTTP,
  Dialogs,
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Memo1: TMemo;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses IdCookieManager, IdCookie;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  IdHTTP: TIdHTTP;
  stringstream: TStringStream;
  i: Integer;
begin
  IdHTTP:=TIdHTTP.Create(nil);
  IdHTTP.AllowCookies:=True;
{
TIdCookieManager создавать не будем, ибо (Delphi 7):

unit IdHTTP

procedure TIdCustomHTTP.ProcessCookies(ARequest: TIdHTTPRequest; AResponse: TIdHTTPResponse);
...
begin
    ...
    if not Assigned(FCookieManager) and AllowCookies then
    begin
      CookieManager := TIdCookieManager.Create(Self);
      FFreeOnDestroy := true;
    end;
    ...
}
  IdHTTP.HandleRedirects:=True;
  // POST request
  stringstream:=TStringStream.Create('vb_login_username=имя&vb_login_password=пароль&cookieuser=1&s=&securitytoken=guest&do=login&vb_login_md5password=&vb_login_md5password_utf=');
  try
    IdHTTP.Request.ContentType:='application/x-www-form-urlencoded';
    IdHTTP.Request.ContentLength:=stringstream.Size;
    IdHTTP.Post('http://forum29.ru/login.php?do=login', stringstream);
    // не дождетесь raise
    if IdHTTP.CookieManager=nil then raise Exception.Create('CookieManager=nil');
    // в Edit1 соберем cookie
    Edit1.Text:='';
    for i:=0 to IdHTTP.CookieManager.CookieCollection.Count-1 do
      Edit1.Text:=Edit1.Text+IdHTTP.CookieManager.CookieCollection[i].CookieName+'='+IdHTTP.CookieManager.CookieCollection[i].Value+';';
  finally
    stringstream.Free;
    IdHTTP.Free;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  IdHTTP: TIdHTTP;
begin
  IdHTTP:=TIdHTTP.Create(nil);
  // руками добавим cookie
  IdHTTP.AllowCookies:=False;
  IdHTTP.HandleRedirects:=True;
  try
    // в Edit1 наши cookie
    IdHTTP.Request.CustomHeaders.Add('Cookie: '+Edit1.Text);
    Memo1.Text:=IdHTTP.Get('http://forum29.ru/');
    Memo1.Lines.SaveToFile('c:\Downloads\forum29.htm');
  finally
    IdHTTP.Free;
  end;
end;

end.

по кнопке 1 авторизуемся и сохраняем куки в едите. по кнопке 2 получаем авторизованную страничку.

а вот если использовать TIdCookieManager, то никакой канители вообще не нужно для cookie. в случае использования нескольких TIdHTTP естественно.
__________________
Пишу программы за еду.
__________________
Ответить с цитированием