2017-06-28 7 views
0

idhttp 10.5.7を使用してセッションをサポートするアプリケーションをDelphi XEで作成しようとしていますが、リクエストを呼び出すたびにセッションが常に変更されます。セッションがindyを使用してメンテナンスされていない10 idhttpとDelphi XE

しかし、Delphi 2006ではindy 10.1.5を使用して同じコードを使用していますが、リクエストを複数回呼び出しても1セッションしか作成しなくても、セッションは正常に維持されます。

どのようにこの問題をDelphi XEで解決できますか?私はボタンをクリックした

unit Unit1; 

interface 

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

type 
    TForm1 = class(TForm) 
    HTTP: TIdHTTP; 
    Button1: TButton; 
    Memo1: TMemo; 
    procedure Button1Click(Sender: TObject); 
    procedure FormCreate(Sender: TObject); 
    private 
    function get_dataweb(url: string): string; 
    procedure OnNewCookie(ASender: TObject; ACookie: TIdCookieRFC2109; var VAccept: Boolean); 
    { Private declarations } 
    public 
     Stream : TStringStream; 
     cookie : TIdCookieManager; 
    { Public declarations } 
    end; 

var 
    Form1: TForm1; 

implementation 

{$R *.dfm} 

procedure TForm1.Button1Click(Sender: TObject); 
var hasil : string; 
begin 
    hasil := get_dataweb('http://localhost/web/tes.php'); 
end; 

procedure TForm1.FormCreate(Sender: TObject); 
begin 
    Stream := TStringStream.Create; 
    cookie := TIdCookieManager.Create; 
    cookie.OnNewCookie := OnNewCookie; 
    HTTP.CookieManager := cookie; 

end; 

procedure TForm1.OnNewCookie(ASender: TObject; ACookie: TIdCookieRFC2109; var VAccept: Boolean); 
begin 
    memo1.Lines.Add(ACookie.CookieText); 
end; 

function TForm1.get_dataweb(url:string):string; 
var hasil : string; 
begin 

    stream.Position  := 0; 
    stream.Size   := 0; 

    http.AllowCookies  := True; 
    http.HandleRedirects := True; 
    HTTP.ReadTimeout  := 0; 
    HTTP.ConnectTimeout := 0; 

    HTTP.Request.CustomHeaders.clear; 
    HTTP.Request.CustomHeaders.Add('user:user'); 
    HTTP.Request.CustomHeaders.Add('password:password'); 
    HTTP.request.useragent := 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0'; 

    try 
    http.get(url,Stream); 
    hasil := Stream.DataString; 
    except 

    on E: EIdHTTPProtocolException do 
    begin 
     hasil := E.ErrorMessage; 
     result := hasil; 
     exit; 
    end; 
    on E: Exception do 
    begin 
     ShowMessage('Failed, ' + E.Message + #13#10 + ' ' + hasil); 
    end; 
    end; 

    result := hasil; 
end; 


end. 

3回、3回、新しいセッションの提案レミーのため

3 times i clicked the button, 3 times new session created

+0

10.5.7は非常に古い、現在のバージョンは10.6.2です。いずれにせよ、これは明らかにクッキーの問題です。 Cookie管理は、最近のCookieプラクティスに従って、10.1.5から10.5.7まで多く変更されています。あなたの古い 'TIdHTTP'はクッキーを破棄しているか、実際にリクエストに合っていないと思っているので、サーバに戻すことはありません。これは、なぜ新しいセッションが各リクエストで作成されているのかを説明します。 10.6.2でこの問題を再現できません。セッションは期待どおりに再利用されます。 –

答えて

0

ありがとうを作成し、私は10.6.2に私のインディをアップグレードしているし、問題が解決しました。私はthisチュートリアルに成功しました

関連する問題