Authorization Basic HTTRIO SPC / Delphi

2

Good morning everyone ,

I wonder if anyone could help me in the following situation:

I'm doing an integration with the SPC / CDL web service, which requires a basic authentication (HTTP header).

I need to do this authentication using HTTRIO because I used the WSDL Importer, I did not build the XML on the nail, so to speak.

I was able to authenticate to the beforePost event as shown below:

  auth := 'Authorization: Basic ' + EncodeString('login:senha');
  HttpAddRequestHeaders(Data, PChar(auth), Length(auth), HTTP_ADDREQ_FLAG_ADD);

The problem is this, the first time I run the application and consume the web service, it works perfectly, it does the authentication and it gives me a return, everything ok.

However, without closing the application if you make one more query, it returns the authentication error (WS Authentication Error).

If you close the application and open the first query again, it works.

Could anyone help?

Here is the source for a project I did here for testing:

procedure TForm3.BitBtn1Click(Sender: TObject);
var C:ConsultaSpcScWSService;
    F:filtroConsultaSpcPlusMasterProtestoSCWS2;
    R:RespostaConsultaSpcPlusMasterProtestoSC2;
    H:THTTPRIO;
begin
  H := THTTPRIO.Create(self);
  H.HTTPWebNode.OnBeforePost := HTTPRIO1HTTPWebNode1BeforePost;
  try 
    C := GetConsultaSpcScWSService(false,'',H);
    F := filtroConsultaSpcPlusMasterProtestoSCWS2.Create;
    try
      F.cpfCnpj := 'xxxx';
      R := C.SPCPlusMasterProtestoSC_65(f);
      Memo1.Text :=  R.consumidor.nome;
      R.free;
    finally
      C := nil;
      F.free;     
    end;
  except on E:exception do
    Memo1.Text := 'Erro :'+E.Message;
  end;
end;

procedure TForm3.HTTPRIO1HTTPWebNode1BeforePost(
  const HTTPReqResp: THTTPReqResp; Data: Pointer);
var auth:string;
begin
  auth := 'Authorization: Basic ' + EncodeString('user:pass');
  HttpAddRequestHeaders(Data, PChar(auth), Length(auth), HTTP_ADDREQ_FLAG_ADD);
end;

Thank you in advance.

    
asked by anonymous 09.11.2016 / 12:38

1 answer

0

Problem resolved . After many tests I discovered that the problem was in the storage of cookies. To fix the problem I simply cleaned the cookies before authentication in the OnBeforePost event of HTTPReqResp:

procedure TConsulta.HTTPRIO1HTTPWebNode1BeforePost(
  const HTTPReqResp: THTTPReqResp; Data: Pointer);
var
  auth:string;
  S:string;
begin
  {Clear Cookies}
  InternetSetOption(0, INTERNET_OPTION_END_BROWSER_SESSION, nil, 0);

  {autenticação, (Monta base64 e seta no Header HTTP)}
  auth := 'Authorization: Basic ' + EncodeString(GEntidade+':'+GUser + ':'+ GPassWord);
  HttpAddRequestHeaders(Data, PChar(auth), Length(auth), HTTP_ADDREQ_FLAG_ADD);
end;

Maybe it helps somebody too.

    
10.11.2016 / 13:07