gpt4 book ai didi

Delphi - Indy (IDHTTP) 保持 session

转载 作者:行者123 更新时间:2023-12-03 15:08:44 25 4
gpt4 key购买 nike

好的,我已经动态创建了 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;

我有一个网站,要求我保持 session 事件。我该怎么做?使用与上面类似的代码。

如果我为所有内容创建一个可视组件,并使用它,一切都很棒,但是当我动态创建该组件时(我真的想以这种方式保留它),它无法使 session 保持事件状态。

感谢任何帮助。

最佳答案

我没有看到你在哪里实例化 CookieManager,但那是你应该跟踪 session 的地方。服务器将发送一些代表当前 session 的 cookie,并且您发送到服务器的所有进一步请求都应包含该 cookie,以便服务器知道要使用哪个 session 。

您必须在 session 期间保留 cookie 管理器对象,或者必须将其数据保存在其他位置,然后在每次创建新的 cookie 管理器对象时重新加载它。我更喜欢前者。事实上,您可能会考虑保留整个 HTTP 对象。

关于Delphi - Indy (IDHTTP) 保持 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2259753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com