Authorization Basic HTTRIO SPC / Delphi

Asked

Viewed 1,818 times

2

Good morning to all,

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

I am 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 Importer WSDL, I did not build XML in "nail", let’s say so.

I was able to perform the authentication in the beforePost event as an example below:

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

the problem is the following, the first time I run the application and consume the web service, works perfectly, makes the authentication and gives me a return, everything ok.

However, without closing the application if performing another query, returns the authentication error (WS Authentication Error).

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

Could someone help?

Following is the source of 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;

Grateful from now on.

  • Hello everyone, one more piece of information: I used a third component in which the Httprio component was changed, when performing some tests with this component I disabled an option called "Allowcookies", after disabling this option worked perfectly. I came to the conclusion then that the problem is with the storage of cookies in the navigation. The problem now is how to clear cookies, and the native HTTPRIO component does not have this property (Allowcookies); Any suggestions? Thank you!

  • where does Httpaddrequestheaders come from? and HTTP_ADDREQ_FLAG_ADD

  • Both come from the windows API. Uses: Winapi.Wininet

1 answer

0


Problem solved. After many tests I discovered that the problem was in the storage of cookies. To solve the problem simply clear cookies before authentication at the Httpreqresp Onbeforepost event:

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 help someone too.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.