TidHTTP.Post - Replacement with SecureBridge

Discussion of open issues, suggestions and bugs regarding network security and data protection solution - SecureBridge
Post Reply
mmkw
Posts: 4
Joined: Tue 17 Jul 2018 10:34

TidHTTP.Post - Replacement with SecureBridge

Post by mmkw » Thu 19 Jul 2018 11:05

Hi,

I need to replace

TidHTTP.Post(AURL: string; const ASource: TStream; const AResponseContent: TStream);

with SecureBridge. ASource contains the data to be posted, AResponseContent contains the returned data. We need this often to communicate with webservices.

How can this be done? Can you give an example?

Thank you in advance

Kai

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: TidHTTP.Post - Replacement with SecureBridge

Post by ViktorV » Thu 19 Jul 2018 11:25

To work with the data via the HTTP (HTTPS) protocol, you can use the TScHttpWebRequest component: https://devart.com/sbridge/docs/index.h ... equest.htm You can specify settings for secure connection in the TScHttpWebRequest.SSLOptions property.
You can find the samples of using the TScHttpWebRequest component at our forum: viewtopic.php?f=27&t=36270

mmkw
Posts: 4
Joined: Tue 17 Jul 2018 10:34

Re: TidHTTP.Post - Replacement with SecureBridge

Post by mmkw » Thu 19 Jul 2018 14:10

Hi,

I came so far:

Code: Select all

TgboHTTP = class
  private
    FHTTP: TScHttpWebRequest;
  public
    constructor Create;
    destructor Destroy; override;

    procedure Post(const AURI: string; const AStream, AResultStream: TStream);
  end;
  
procedure TgboHTTP.Post(const AURI: string; const AStream, AResultStream: TStream);
var
   AResponse: TScHttpWebResponse;
   ABuf: TBytes;
begin
     SetLength(ABuf, 0);
     AStream.Position:= 0;
     AStream.ReadBuffer(ABuf, AStream.Size);

     FHTTP.Method := rmPost;
     FHTTP.RequestUri:= AURI;
     FHTTP.ContentType := 'application/x-www-form-urlencoded';
     FHTTP.ContentLength := Length(ABuf);
     FHTTP.WriteBuffer(ABuf);

     SetLength(ABuf, 0);
     AResponse:= FHTTP.GetResponse;
     try
        AResponse.ReadBuffer(ABuf);
        AResultStream.Size:= 0;
        AResultStream.Position:= 0;
        AResultStream.WriteBuffer(ABuf, Length(ABuf));
     finally
        AResponse.Free;
     end;
end;
But this does not work, because AResponse.ReadBuffer does not accept a TBytes parameter.

I want to read all the data, the remote side sends. So if you cannot give an example, the following questions occur:

1. Can I rely on the value in AResponse.ContentLength or is it a matter of the remote side, if they give the right value in it?
2. If not, do I have to read chunks unless less bytes are read than assumed?
3. How is it implemented in AResponse.ReadAsString? As I cannot see the code, I have no clue.
3. In the docs, ReadBuffer is declared as

Code: Select all

function ReadBuffer(const Buffer: TValueArr; Offset, Count: integer): integer;
Delphi says, it is

Code: Select all

 function ReadBuffer(Buffer: PAnsiChar; Offset: Integer; Count: Integer): Integer; 
So is TValueArr = PAnsiChar ?

Your help is appreciated.
best regards
Kai

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: TidHTTP.Post - Replacement with SecureBridge

Post by ViktorV » Fri 20 Jul 2018 08:19

Implemention of the ReadAsString method:

Code: Select all

function TScHttpWebResponse.ReadAsString: string;
var
  Offset, Count: integer;
begin
  Offset := 0;

  repeat
    if Length(FTmpBuf) - Offset < 1024 then
      SetLength(FTmpBuf, Length(FTmpBuf) + BUF_SIZE);

    Count := InternalRead(TValueArr(FTmpBuf), Offset, Length(FTmpBuf) - Offset);
    Inc(Offset, Count);
  until Count <= 0;

  Result := Encoding.UTF8.GetString(FTmpBuf, 0, Offset);
end;
You can use this code to implement data fetching using the ReadBuffer method.
Yes, you are right TValueArr = PAnsiChar.
TScHttpWebResponse.ContentLength returns the content length returned by the query.

Post Reply