Revisión | 12 (tree) |
---|---|
Tiempo | 2020-02-15 03:03:07 |
Autor | ![]() |
Removida toda codificação que considerava que os Cookies precisavam ser gerenciados. O WinInet gerencia os Cookies automaticamente quando o flag "INTERNET_FLAG_NO_COOKIES" não é usado
@@ -89,7 +89,6 @@ | ||
89 | 89 | <AppEnableRuntimeThemes>true</AppEnableRuntimeThemes> |
90 | 90 | <VerInfo_IncludeVerInfo>true</VerInfo_IncludeVerInfo> |
91 | 91 | <VerInfo_Locale>1033</VerInfo_Locale> |
92 | - <Debugger_RunParams>u 02322719412 ******</Debugger_RunParams> | |
93 | 92 | <AppDPIAwarenessMode>PerMonitor</AppDPIAwarenessMode> |
94 | 93 | </PropertyGroup> |
95 | 94 | <PropertyGroup Condition="'$(Cfg_2)'!=''"> |
@@ -210,6 +210,7 @@ | ||
210 | 210 | |
211 | 211 | // Inicializa um record para guardar informações estendidas a serem |
212 | 212 | // associadas ao item que está sendo adicionado a LIVIMensagens |
213 | + { TODO : EU não sei porque eu precisei fazer isso. Mas vou descobrir depois } | |
213 | 214 | New(MCF); |
214 | 215 | MCF.Id := RMR.Id; |
215 | 216 | MCF.Mensagem := '??? não sei o que colocar aqui'; |
@@ -22,7 +22,13 @@ | ||
22 | 22 | History: THistory; |
23 | 23 | end; |
24 | 24 | |
25 | -{ TODO : Descobri que após a primeira utilização o headers não são mais retornados e mesmo assim tudo continua funcionando. Será que eles são desnecessários? } | |
25 | +// O WinInet automaticamente gerencia os Cookies, ou seja, você não precisa se | |
26 | +// preocupar em salvar cookies retornados para posterior envio. Outros | |
27 | +// cabeçalhos precisam de gerenciamento de forma usual. Ao utilizar o flag | |
28 | +// INTERNET_FLAG_NO_COOKIES (o qual está comentado em vários locais abaixo) os | |
29 | +// Cookies não são mais automaticamente gerenciados, obrigando o programador a | |
30 | +// construir toda uma lógica para mantê-los, excluí-los e reenviá-los | |
31 | + | |
26 | 32 | function CurrentStatus(AHandle: THandle; AProducao: Boolean; ACPF: String; APassaword: String; AMatricula: String): TCheckInChecOutInfo; |
27 | 33 | function CheckIn(AHandle: THandle; AProducao: Boolean; ACPF: String; APassword: String; AMatricula: String): Boolean; |
28 | 34 | function CheckOut(AHandle: THandle; AProducao: Boolean; ACPF: String; APassword: String; AMatricula: String): Boolean; |
@@ -35,17 +41,22 @@ | ||
35 | 41 | StrUtils; |
36 | 42 | |
37 | 43 | const |
38 | - URLBASEH = 'https://homologacao.tjpe.gov.br/controlefrequencia/'; | |
44 | + URLBASEH = 'https://homologacao-controlefrequencia.app.tjpe.gov.br/'; | |
39 | 45 | URLBASEP = 'https://www.tjpe.jus.br/controlefrequencia/'; |
40 | 46 | SEMVERIFICATIONCOOKIE = 'Não foi possível obter o cookie de verificação. Revise suas informações de login e tente novamente'; |
41 | 47 | |
42 | 48 | type |
43 | 49 | TAdditionalInfo = record |
44 | - VerificationCookie: String; | |
45 | 50 | IdBotaoRegistroEntradaSaida: String; |
46 | 51 | end; |
47 | 52 | |
48 | -function GetBaseCookie(AHandle: THandle; AProducao: Boolean): String; | |
53 | +//: Acessa, via método GET, a página inicial (página de login) para obter os | |
54 | +//: Cookies que serão requeridos por todas as outras requisições subsequentes. | |
55 | +//: A página de login é ponto de entrada para o sistema e configura alguns | |
56 | +//: cookies básicos que outras requisições usarão. Outras requisições, | |
57 | +//: realizadas por outras subrotinas contidas aqui podem obter Cookies | |
58 | +//: adicionais, os quais serão gerenciados automaticamente pelo WinInet | |
59 | +procedure GetInitialCookies(AHandle: THandle; AProducao: Boolean); | |
49 | 60 | var |
50 | 61 | RO: TRequestOptions; |
51 | 62 | RE: TResponse; |
@@ -80,7 +91,7 @@ | ||
80 | 91 | RO.HttpOpenRequestParams.ReceiveTimeout := 30000; |
81 | 92 | RO.HttpOpenRequestParams.AcceptTypes := TStringList.Create; |
82 | 93 | RO.HttpOpenRequestParams.AcceptTypes.Text := Trim('*/*'); |
83 | - RO.HttpOpenRequestParams.Flags := INTERNET_FLAG_NO_CACHE_WRITE or INTERNET_FLAG_NO_COOKIES or INTERNET_FLAG_PRAGMA_NOCACHE;// 0; | |
94 | + RO.HttpOpenRequestParams.Flags := INTERNET_FLAG_NO_CACHE_WRITE {or INTERNET_FLAG_NO_COOKIES} or INTERNET_FLAG_PRAGMA_NOCACHE;// 0; | |
84 | 95 | RO.HttpOpenRequestParams.Context := AHandle; |
85 | 96 | RO.HttpOpenRequestParams.AutoDetectHTTPS := True; |
86 | 97 | RO.HttpOpenRequestParams.IgnoreInvalidCertificates := False; |
@@ -98,8 +109,7 @@ | ||
98 | 109 | RE.Content := TStringStream.Create(''); |
99 | 110 | try |
100 | 111 | Request(RO, RE); |
101 | - TStringStream(RE.Content).SaveToFile('d:\GetBaseCookie.txt'); | |
102 | - RegExMatch(RE.Headers,'(JSESSIONID=.*);',1,0,True,[preUnGreedy],Result); | |
112 | + TStringStream(RE.Content).SaveToFile('d:\CiCo_GetInitialCookies.txt'); | |
103 | 113 | finally |
104 | 114 | RE.Content.Free; |
105 | 115 | RO.HttpSendRequestParams.Headers.Free; |
@@ -109,7 +119,7 @@ | ||
109 | 119 | end; |
110 | 120 | end; |
111 | 121 | |
112 | -procedure SelectDirection(AHandle: THandle; AProducao: Boolean; AIn: Boolean; AVerificationCookie: String; ABaseCookie: String); | |
122 | +procedure SelectDirection(AHandle: THandle; AProducao: Boolean; AIn: Boolean); | |
113 | 123 | var |
114 | 124 | RO: TRequestOptions; |
115 | 125 | RE: TResponse; |
@@ -159,13 +169,13 @@ | ||
159 | 169 | RO.HttpOpenRequestParams.ReceiveTimeout := 30000; |
160 | 170 | RO.HttpOpenRequestParams.AcceptTypes := TStringList.Create; |
161 | 171 | RO.HttpOpenRequestParams.AcceptTypes.Text := Trim('*/*'); |
162 | - RO.HttpOpenRequestParams.Flags := INTERNET_FLAG_NO_CACHE_WRITE or INTERNET_FLAG_NO_COOKIES or INTERNET_FLAG_PRAGMA_NOCACHE; | |
172 | + RO.HttpOpenRequestParams.Flags := INTERNET_FLAG_NO_CACHE_WRITE {or INTERNET_FLAG_NO_COOKIES} or INTERNET_FLAG_PRAGMA_NOCACHE; | |
163 | 173 | RO.HttpOpenRequestParams.Context := AHandle; |
164 | 174 | RO.HttpOpenRequestParams.AutoDetectHTTPS := True; |
165 | 175 | RO.HttpOpenRequestParams.IgnoreInvalidCertificates := False; |
166 | 176 | RO.HttpOpenRequestParams.Headers := TStringList.Create; |
167 | 177 | RO.HttpOpenRequestParams.Headers.Add('Content-Type: application/x-www-form-urlencoded'); |
168 | - RO.HttpOpenRequestParams.Headers.Add('Cookie: ' + AVerificationCookie + '; ' + ABaseCookie + '; ROUTEID=.1'); | |
178 | +// RO.HttpOpenRequestParams.Headers.Add('Cookie: ' + AVerificationCookie + '; ' + ABaseCookie + '; ROUTEID=.1'); | |
169 | 179 | RO.HttpSendRequestParams.IgnoreInvalidCertificateCA := True; |
170 | 180 | |
171 | 181 | RE.Content := TStringStream.Create(''); |
@@ -180,7 +190,7 @@ | ||
180 | 190 | end; |
181 | 191 | end; |
182 | 192 | |
183 | -function OpenCheckInCheckOutPage(AHandle: THandle; AProducao: Boolean; AVerificationCookie: String; ABaseCookie: String; AIdBotao: String): TCheckInChecOutInfo; | |
193 | +function OpenCheckInCheckOutPage(AHandle: THandle; AProducao: Boolean; AIdBotao: String): TCheckInChecOutInfo; | |
184 | 194 | var |
185 | 195 | RO: TRequestOptions; |
186 | 196 | RE: TResponse; |
@@ -224,19 +234,19 @@ | ||
224 | 234 | RO.HttpOpenRequestParams.ReceiveTimeout := 30000; |
225 | 235 | RO.HttpOpenRequestParams.AcceptTypes := TStringList.Create; |
226 | 236 | RO.HttpOpenRequestParams.AcceptTypes.Text := Trim('*/*'); |
227 | - RO.HttpOpenRequestParams.Flags := INTERNET_FLAG_NO_CACHE_WRITE or INTERNET_FLAG_NO_COOKIES or INTERNET_FLAG_PRAGMA_NOCACHE; | |
237 | + RO.HttpOpenRequestParams.Flags := INTERNET_FLAG_NO_CACHE_WRITE {or INTERNET_FLAG_NO_COOKIES} or INTERNET_FLAG_PRAGMA_NOCACHE; | |
228 | 238 | RO.HttpOpenRequestParams.Context := AHandle; |
229 | 239 | RO.HttpOpenRequestParams.AutoDetectHTTPS := True; |
230 | 240 | RO.HttpOpenRequestParams.IgnoreInvalidCertificates := False; |
231 | 241 | RO.HttpOpenRequestParams.Headers := TStringList.Create; |
232 | 242 | RO.HttpOpenRequestParams.Headers.Add('Content-Type: application/x-www-form-urlencoded'); |
233 | - RO.HttpOpenRequestParams.Headers.Add('Cookie: ' + AVerificationCookie + '; ' + ABaseCookie + '; ROUTEID=.1'); | |
243 | +// RO.HttpOpenRequestParams.Headers.Add('Cookie: ' + AVerificationCookie + '; ' + ABaseCookie + '; ROUTEID=.1'); | |
234 | 244 | RO.HttpSendRequestParams.IgnoreInvalidCertificateCA := True; |
235 | 245 | |
236 | 246 | RE.Content := TStringStream.Create(''); |
237 | 247 | try |
238 | 248 | Request(RO, RE); |
239 | - TStringStream(RE.Content).SaveToFile('d:\OpenCheckInCheckOutPage.txt'); | |
249 | + TStringStream(RE.Content).SaveToFile('d:\CiCo_OpenCheckInCheckOutPage.txt'); | |
240 | 250 | |
241 | 251 | // Realiza um parser na resposta buscando várias informações. Se não |
242 | 252 | // houver movimentação no dia significa que o usuário não fez checkin |
@@ -267,7 +277,9 @@ | ||
267 | 277 | end; |
268 | 278 | end; |
269 | 279 | |
270 | -function GetAdditionalInfo(AHandle: THandle; AProducao: Boolean; ABaseCookie, ACPF, APassword: String; AMatricula: String): TAdditionalInfo; | |
280 | +//: Efetivamente executa o login no sistema de controle de frequência. A função | |
281 | +//: retornará informações adicionais que podem ser usadas por outras subrotinas | |
282 | +function ExecuteLogin(AHandle: THandle; AProducao: Boolean; ACPF, APassword: String; AMatricula: String): TAdditionalInfo; | |
271 | 283 | var |
272 | 284 | RO: TRequestOptions; |
273 | 285 | RE: TResponse; |
@@ -321,13 +333,12 @@ | ||
321 | 333 | RO.HttpOpenRequestParams.ReceiveTimeout := 30000; |
322 | 334 | RO.HttpOpenRequestParams.AcceptTypes := TStringList.Create; |
323 | 335 | RO.HttpOpenRequestParams.AcceptTypes.Text := Trim('*/*'); |
324 | - RO.HttpOpenRequestParams.Flags := INTERNET_FLAG_NO_CACHE_WRITE or INTERNET_FLAG_NO_COOKIES or INTERNET_FLAG_PRAGMA_NOCACHE;// 0; | |
336 | + RO.HttpOpenRequestParams.Flags := INTERNET_FLAG_NO_CACHE_WRITE {or INTERNET_FLAG_NO_COOKIES} or INTERNET_FLAG_PRAGMA_NOCACHE;// 0; | |
325 | 337 | RO.HttpOpenRequestParams.Context := AHandle; |
326 | 338 | RO.HttpOpenRequestParams.AutoDetectHTTPS := True; |
327 | 339 | RO.HttpOpenRequestParams.IgnoreInvalidCertificates := False; |
328 | 340 | RO.HttpOpenRequestParams.Headers := TStringList.Create; |
329 | 341 | RO.HttpOpenRequestParams.Headers.Add('Content-Type: application/x-www-form-urlencoded'); |
330 | - RO.HttpOpenRequestParams.Headers.Add('Cookie: ' + ABaseCookie + '; ROUTEID=.1'); | |
331 | 342 | RO.HttpSendRequestParams.Headers := nil; |
332 | 343 | RO.HttpSendRequestParams.Optional := nil; |
333 | 344 | RO.HttpSendRequestParams.OptionalLength := 0; |
@@ -341,9 +352,8 @@ | ||
341 | 352 | RE.Content := TStringStream.Create(''); |
342 | 353 | try |
343 | 354 | Request(RO, RE); |
344 | - RegExMatch(RE.Headers,'(ControleFrequenciaVerificationCookie=.*);',1,0,True,[preUnGreedy],Result.VerificationCookie); | |
345 | 355 | |
346 | - TStringStream(RE.Content).SaveToFile('d:\GetVerificationCookie.txt'); | |
356 | + TStringStream(RE.Content).SaveToFile('d:\CiCo_ExecuteLogin.txt'); | |
347 | 357 | |
348 | 358 | HD := coHTMLDocument.Create as IHTMLDocument2; |
349 | 359 | HD.Write(TStringStream(RE.Content).DataString); |
@@ -402,7 +412,7 @@ | ||
402 | 412 | end; |
403 | 413 | end; |
404 | 414 | |
405 | -function ConfirmDirection(AHandle: THandle; AProducao: Boolean; AIn: Boolean; AVerificationCookie: String; ABaseCookie: String): Boolean; | |
415 | +function ConfirmDirection(AHandle: THandle; AProducao: Boolean; AIn: Boolean): Boolean; | |
406 | 416 | var |
407 | 417 | RO: TRequestOptions; |
408 | 418 | RE: TResponse; |
@@ -453,12 +463,11 @@ | ||
453 | 463 | RO.HttpOpenRequestParams.ReceiveTimeout := 30000; |
454 | 464 | RO.HttpOpenRequestParams.AcceptTypes := TStringList.Create; |
455 | 465 | RO.HttpOpenRequestParams.AcceptTypes.Text := Trim('*/*'); |
456 | - RO.HttpOpenRequestParams.Flags := INTERNET_FLAG_NO_CACHE_WRITE or INTERNET_FLAG_NO_COOKIES or INTERNET_FLAG_PRAGMA_NOCACHE; | |
466 | + RO.HttpOpenRequestParams.Flags := INTERNET_FLAG_NO_CACHE_WRITE {or INTERNET_FLAG_NO_COOKIES} or INTERNET_FLAG_PRAGMA_NOCACHE; | |
457 | 467 | RO.HttpOpenRequestParams.Context := AHandle; |
458 | 468 | RO.HttpOpenRequestParams.AutoDetectHTTPS := True; |
459 | 469 | RO.HttpOpenRequestParams.Headers := TStringList.Create; |
460 | 470 | RO.HttpOpenRequestParams.Headers.Add('Content-Type: application/x-www-form-urlencoded'); |
461 | - RO.HttpOpenRequestParams.Headers.Add('Cookie: ' + AVerificationCookie + '; ' + ABaseCookie + '; ROUTEID=.1'); | |
462 | 471 | RO.HttpSendRequestParams.IgnoreInvalidCertificateCA := True; |
463 | 472 | |
464 | 473 | RE.Content := TStringStream.Create(''); |
@@ -493,18 +502,17 @@ | ||
493 | 502 | |
494 | 503 | function CurrentStatus(AHandle: THandle; AProducao: Boolean; ACPF: String; APassaword: String; AMatricula: String): TCheckInChecOutInfo; |
495 | 504 | var |
496 | - BaseCookie: String; | |
497 | 505 | AdditionalInfo: TAdditionalInfo; |
498 | 506 | begin |
499 | 507 | try |
500 | - // Fase 1: Obter o cookie-base | |
501 | - BaseCookie := GetBaseCookie(AHandle,AProducao); | |
508 | + // Fase 1: Obter os Cookies iniciais | |
509 | + GetInitialCookies(AHandle,AProducao); | |
502 | 510 | |
503 | - // Fase 2: Realizar o login e obter o cookie de verificação | |
504 | - AdditionalInfo := GetAdditionalInfo(AHandle,AProducao,BaseCookie,ACPF,APassaword,AMatricula); | |
511 | + // Fase 2: Realizar o login e obter informações adicionais | |
512 | + AdditionalInfo := ExecuteLogin(AHandle,AProducao,ACPF,APassaword,AMatricula); | |
505 | 513 | |
506 | 514 | // Fase 3: Abrir a página de registro de entrada / saída |
507 | - Result := OpenCheckInCheckOutPage(AHandle,AProducao,AdditionalInfo.VerificationCookie,BaseCookie,AdditionalInfo.IdBotaoRegistroEntradaSaida); | |
515 | + Result := OpenCheckInCheckOutPage(AHandle,AProducao,AdditionalInfo.IdBotaoRegistroEntradaSaida); | |
508 | 516 | except |
509 | 517 | on E: Exception do |
510 | 518 | raise ECurrentStatus.Create(E.Message); |
@@ -513,24 +521,23 @@ | ||
513 | 521 | |
514 | 522 | function CheckIn(AHandle: THandle; AProducao: Boolean; ACPF: String; APassword: String; AMatricula: String): Boolean; |
515 | 523 | var |
516 | - BaseCookie: String; | |
517 | 524 | AdditionalInfo: TAdditionalInfo; |
518 | 525 | begin |
519 | 526 | try |
520 | - // Fase 1: Obter o cookie-base | |
521 | - BaseCookie := GetBaseCookie(AHandle,AProducao); | |
527 | + // Fase 1: Obter os Cookies iniciais | |
528 | + GetInitialCookies(AHandle,AProducao); | |
522 | 529 | |
523 | - // Fase 2: Realizar o login e obter o cookie de verificação | |
524 | - AdditionalInfo := GetAdditionalInfo(AHandle,AProducao,BaseCookie,ACPF,APassword,AMatricula); | |
530 | + // Fase 2: Realizar o login e obter informações adicionais | |
531 | + AdditionalInfo := ExecuteLogin(AHandle,AProducao,ACPF,APassword,AMatricula); | |
525 | 532 | |
526 | 533 | // Fase 3: Abrir a página de registro de entrada / saída |
527 | - OpenCheckInCheckOutPage(AHandle,AProducao,AdditionalInfo.VerificationCookie,BaseCookie,AdditionalInfo.IdBotaoRegistroEntradaSaida); | |
534 | + OpenCheckInCheckOutPage(AHandle,AProducao,AdditionalInfo.IdBotaoRegistroEntradaSaida); | |
528 | 535 | |
529 | 536 | // Fase 4: Seleciona "ENTRADA" |
530 | - SelectDirection(AHandle,AProducao,True,AdditionalInfo.VerificationCookie,BaseCookie); | |
537 | + SelectDirection(AHandle,AProducao,True); | |
531 | 538 | |
532 | 539 | // Fase 5: Confirma a entrada |
533 | - Result := ConfirmDirection(AHandle,AProducao,True,AdditionalInfo.VerificationCookie,BaseCookie); | |
540 | + Result := ConfirmDirection(AHandle,AProducao,True); | |
534 | 541 | except |
535 | 542 | on E: Exception do |
536 | 543 | raise ECheckIn.Create(E.Message); |
@@ -539,24 +546,23 @@ | ||
539 | 546 | |
540 | 547 | function CheckOut(AHandle: THandle; AProducao: Boolean; ACPF: String; APassword: String; AMatricula: String): Boolean; |
541 | 548 | var |
542 | - BaseCookie: String; | |
543 | 549 | AdditionalInfo: TAdditionalInfo; |
544 | 550 | begin |
545 | 551 | try |
546 | - // Fase 1: Obter o cookie-base | |
547 | - BaseCookie := GetBaseCookie(AHandle,AProducao); | |
552 | + // Fase 1: Obter os Cookies iniciais | |
553 | + GetInitialCookies(AHandle,AProducao); | |
548 | 554 | |
549 | - // Fase 2: Realizar o login e obter o cookie de verificação | |
550 | - AdditionalInfo := GetAdditionalInfo(AHandle,AProducao,BaseCookie,ACPF,APassword,AMatricula); | |
555 | + // Fase 2: Realizar o login e obter informações adicionais | |
556 | + AdditionalInfo := ExecuteLogin(AHandle,AProducao,ACPF,APassword,AMatricula); | |
551 | 557 | |
552 | 558 | // Fase 3: Abrir a página de registro de entrada / saída |
553 | - OpenCheckInCheckOutPage(AHandle,AProducao,AdditionalInfo.VerificationCookie,BaseCookie,AdditionalInfo.IdBotaoRegistroEntradaSaida); | |
559 | + OpenCheckInCheckOutPage(AHandle,AProducao,AdditionalInfo.IdBotaoRegistroEntradaSaida); | |
554 | 560 | |
555 | 561 | // Fase 4: Seleciona "SAÍDA" |
556 | - SelectDirection(AHandle,AProducao,False,AdditionalInfo.VerificationCookie,BaseCookie); | |
562 | + SelectDirection(AHandle,AProducao,False); | |
557 | 563 | |
558 | 564 | // Fase 5: Confirma a saída |
559 | - Result := ConfirmDirection(AHandle,AProducao,False,AdditionalInfo.VerificationCookie,BaseCookie); | |
565 | + Result := ConfirmDirection(AHandle,AProducao,False); | |
560 | 566 | except |
561 | 567 | on E: Exception do |
562 | 568 | raise ECheckOut.Create(E.Message); |