5

わかりました、次のように Idhttp を動的に作成しました


procedure TForm1.Button1Click(Sender: TObject);
Var
   Resp : String;
begin
     Resp := webSession('https://www.website.com'); // HTTPS site requires session to keep alive
     if Length(Resp)>0 then
        MessageDlg('Got the body ok',mtInformation,[mbOk],0);
end;

function TForm1.webSession(sURL : ansistring) : ansistring;
var
   SStream    : Tstringstream;
   HTTPCon    : TIdHTTP;
   AntiFreeze : TIdAntiFreeze;
   CompressorZLib: TIdCompressorZLib;
   ConnectionIntercept: TIdConnectionIntercept;
   SSLIOHandlerSocketOpenSSL: TIdSSLIOHandlerSocketOpenSSL;
   CookieManager: TIdCookieManager;
begin
    CompressorZLib :=  TIdCompressorZLib.Create;
    ConnectionIntercept :=TIdConnectionIntercept.Create;
    SSLIOHandlerSocketOpenSSL :=  TIdSSLIOHandlerSocketOpenSSL.Create;
     Result := '';
     if Length(SettingsForm.edtProxyServer.text) >= 7 then  // 0.0.0.0
     Try
        SStream := NIL;
        AntiFreeze := NIL;
        HTTPCon := NIL;
        Try
           SStream := tstringstream.Create('');
           { Create & Set IdHTTP properties }
           HTTPCon := TIdHTTP.create;
           HTTPCon.AllowCookies:=true;
           HTTPCon.CookieManager :=CookieManager;
           HTTPCon.Compressor := CompressorZLib;
           HTTPCon.Intercept := ConnectionIntercept;
           HTTPCon.IOHandler := SSLIOHandlerSocketOpenSSL;
           HTTPCon.HandleRedirects := true;
           { Check Proxy }
           if checkproxy('http://www.google.com') then
           Begin
                HTTPCon.ProxyParams.ProxyServer := SettingsForm.edtProxyServer.text;
                HTTPCon.ProxyParams.ProxyPort := StrToInt(SettingsForm.edtProxyPort.Text);
                HTTPCon.ProxyParams.BasicAuthentication := True;
                HTTPCon.ProxyParams.ProxyUsername := SettingsForm.edtProxyServer.Text;
                HTTPCon.ProxyParams.ProxyPassword := SettingsForm.edtProxyUserName.Text;
           End;
           { Create another AntiFreeze - only 1/app }
           AntiFreeze := TIdAntiFreeze.Create(nil);
           AntiFreeze.Active := true;
           HTTPCon.Get(sURL,SStream);
           Result := UTF8ToWideString(SStream.DataString);
        Finally
           If Assigned(HTTPCon) then FreeAndNil(HTTPCon);
           If Assigned(AntiFreeze) then FreeAndNil(AntiFreeze);
           If Assigned(SStream) then FreeAndNil(SStream);
           If Assigned(CookieManager) then FreeAndNil (CookieManager );
           If Assigned(CompressorZLib) then FreeAndNil (CompressorZLib );
           If Assigned(ConnectionIntercept) then FreeAndNil (ConnectionIntercept );
           If Assigned(SSLIOHandlerSocketOpenSSL) then FreeAndNil (SSLIOHandlerSocketOpenSSL);

        End;
     Except
        { Handle exceptions }
        On E:Exception do
           MessageDlg('Exception: '+E.Message,mtError, [mbOK], 0);
     End;
end;

function TForm1.checkproxy(sURL : ansistring) : boolean;
var
   HTTPCon : TIdHTTP;
   AntiFreeze : TIdAntiFreeze;
begin
     Result := False;
     Try
        { Inti vars }
        AntiFreeze := NIL;
        HTTPCon := NIL;
        Try
           { AntiFreeze }
           AntiFreeze := TIdAntiFreeze.Create(NIL);
           AntiFreeze.Active := true;
           { Create & Set IdHTTP properties }
           HTTPCon := TIdHTTP.Create(NIL);
           HTTPCon.ProxyParams.ProxyServer := SettingsForm.edtProxyServer.text;
           HTTPCon.ProxyParams.ProxyPort := StrToInt(SettingsForm.edtProxyPort.Text);
           HTTPCon.ProxyParams.BasicAuthentication := True;
           HTTPCon.ProxyParams.ProxyUsername := SettingsForm.edtProxyServer.Text;
           HTTPCon.ProxyParams.ProxyPassword := SettingsForm.edtProxyUserName.Text;
           HTTPCon.HandleRedirects := true;
           HTTPCon.ConnectTimeout := 1000;
           HTTPCon.Request.Connection := 'close';
           HTTPCon.Head(sURL);
        Finally
           { Cleanup }
           if Assigned(HTTPCon) then
           Begin
                { Return Success/Failure }
                Result := HTTPCon.ResponseCode = 200;
                If HTTPCon.Connected then HTTPCon.Disconnect;
                FreeAndNil(HTTPCon);
           End;
           if Assigned(AntiFreeze) then FreeAndNil(AntiFreeze);
        End;
     Except
        On E:EIdException do ;
        { Handle exceptions }
        On E:Exception do
           MessageDlg('Exception: '+E.Message,mtError, [mbOK], 0);
     End;
end;

セッションを維持する必要がある Web サイトがあります。どうすればいいですか?上記と同様のコードで。

すべてのビジュアルコンポーネントを作成して使用すると、すべてがうまくいきますが、コンポーネントを動的に作成すると (本当にこのままにしておきたい)、セッションを維持できません。

どんな助けでも大歓迎です。

4

3 に答える 3

5

インスタンス化する場所はわかりませんCookieManagerが、セッションを追跡する必要がある場所です。サーバーは現在のセッションを表す Cookie を送信します。サーバーに送信する以降のすべての要求には、その Cookie を含めて、サーバーが使用するセッションを認識できるようにする必要があります。

セッションの間、cookie-manager オブジェクトを保持するか、そのデータを別の場所に保存してから、新しい cookie-manager オブジェクトを作成するたびに再ロードする必要があります。私は前者の方がいいです。実際、HTTP オブジェクト全体を保持することを検討することもできます。

于 2010-02-14T02:58:52.403 に答える
2

コメントで述べたように、OnCreateイベントハンドラーでCookieManagerを作成しているため、TForm1.webSessionが呼び出されると、CookieManagerが使用可能になりますが、TForm1.webSessionのfinallyブロックでは、CookieManagerが解放されるため、TForm1を終了します。 webSessionメソッド、CookieManagerのメモリが不足しています。したがって、次にTForm1.webSessionが呼び出されたとき、CookieManagerはNilであり、Cookieは保存されません。

あなたの質問には関係ありませんが、あなたのソースコードに関係している他の2つのメモがあります:

1-メソッドはAnsiStringを返していますが、結果変数に値を割り当てるためにUtf8ToWideStringを使用しています。Utf8ToWideStringはWideStringを返すため、コンパイラはWideStringをAnsiStringに変換する必要があります。これにより、パフォーマンスが低下するだけでなく、返される文字列のUnicode文字が失われます。String(D2009&D2010)またはWideString(Delphiの古いバージョン)のいずれかを返すようにメソッドシグネチャを変更する必要があります。

2-FinallyブロックでSStream、AntiFreeze、またはHTTPConが割り当てられているかどうかを確認する必要はありません。Freeメソッドを呼び出すか、FreeAndNilプロシージャを使用できます。

よろしく

于 2010-02-14T13:28:33.560 に答える
1

Rob が言ったように、あなたの TIdCookieManager は Cookie ベースのセッションを維持するための鍵です。TIdCookieManager は、データモジュールの作成イベントまたはメインフォームの OnCreate() イベントで作成され、IdHTTP コンポーネントを作成するたびに設定されます。

于 2010-02-14T07:21:48.237 に答える