Delphi – Indy(IDHTTP)保持会话
发布时间:2020-12-15 09:33:08 所属栏目:大数据 来源:网络整理
导读:好吧,我有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 o
好吧,我有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,0); End; end; 我有一个网站,要求我保持会话活着.我该怎么做?与上面的代码相似. 如果我为所有东西创建一个可视化组件,并且使用它一切都很棒,但是当我动态创建组件时(我真的想以这种方式离开它)它无法使会话保持活跃状态??. 任何帮助表示赞赏. 解决方法
我没有看到你在哪里实例化CookieManager,但那是你应该跟踪会话的地方.服务器将发送一些表示当前会话的cookie,并且您发送到服务器的所有其他请求应包含该cookie,以便服务器知道要使用哪个会话.
您必须在会话期间保留cookie-manager对象,或者您必须将其数据保存在其他位置,然后在每次创建新的cookie-manager对象时重新加载它.我更喜欢前者.实际上,您可能会考虑保留整个HTTP对象. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |