Page 1 of 1

Payload request

Posted: Thu Feb 20, 2025 8:08 am
by sodlf159
function StreamToString(MemStream: TMemoryStream; AEncoding: TEncoding = nil): string;
var
Bytes: TBytes;
begin
if (MemStream = nil) or (MemStream.Size = 0) then
Exit('');
if AEncoding = nil then AEncoding := TEncoding.UTF8;
SetLength(Bytes, MemStream.Size);
MemStream.Position := 0;
MemStream.ReadBuffer(Bytes[0], MemStream.Size);
Result := AEncoding.GetString(Bytes, 0, Length(Bytes));
end;

function IStreamToString(const AStream: IStream; AEncoding: TEncoding = nil): string;
var
OleStream: TOleStream;
MemStream: TMemoryStream;
begin
Result := '';
if not Assigned(AStream) then Exit;
OleStream := TOleStream.Create(AStream);
try
MemStream := TMemoryStream.Create;
try
MemStream.LoadFromStream(OleStream);
Result := StreamToString(MemStream, AEncoding);
finally
MemStream.Free;
end;
finally
OleStream.Free;
end;
end;

////////////
procedure TMainForm.WVBrowser1WebResourceRequested(Sender: TObject;
const aWebView: ICoreWebView2;
const aArgs: ICoreWebView2WebResourceRequestedEventArgs);
var
Request: ICoreWebView2WebResourceRequest;
pUri, pMethod: PWideChar;
LUri, LMethod: string;
Stream: IStream;
RequestBody: string;
JSONVal: TJSONValue;
JSONObj: TJSONObject;
SToken: string;
begin
aArgs.Get_Request(Request);
if not Assigned(Request) then Exit;
Request.Get_uri(pUri);
Request.Get_Method(pMethod);
try
LUri := pUri;
LMethod := pMethod; //check_token_state.json > you URL
if (Pos('check_token_state.json', LowerCase(LUri)) > 0) and (SameText(LMethod, 'POST')) then
begin
// 1) body open
Request.Get_Content(Stream);
if Assigned(Stream) then
begin
RequestBody := IStreamToString(Stream, TEncoding.UTF8);

// LOG
Memo1.Lines.Add('---- [check_token_state.json] Request Body ----');
Memo1.Lines.Add(RequestBody);
JSONVal := TJSONObject.ParseJSONValue(RequestBody);
try
if (JSONVal <> nil) and (JSONVal is TJSONObject) then
begin
JSONObj := TJSONObject(JSONVal);
if JSONObj.TryGetValue<string>('token', SToken) then
begin
Memo1.Lines.Add('Token = ' + SToken);
end
else
Memo1.Lines.Add('token ERROR');
end;
finally
JSONVal.Free;
end;
end;
end;
finally
if Assigned(pUri) then CoTaskMemFree(pUri);
if Assigned(pMethod) then CoTaskMemFree(pMethod);
end;
end;