XMLHttpRequest Post Data not being sent on Android 10+ (Empty stream)

41 Views Asked by At

Delphi 11.3 or 10.4.1

AWB: TWebBrowser;

Try call like this:

    AWB.EvaluateJavaScript(
//      'alert( "var3=pmp ap pn q m" );' + #13 + #10 + // this work fine all time
    '{ let xhr = new XMLHttpRequest();' + #13 + #10 +     
    'xhr.open("POST", "http://127.0.0.1:50001", false);' + #13 + #10 +            
    'xhr.send("var3=pmp ap pn q m");' + #13 + #10 +
    ' }');

js Alert work fine!

How i catch (dfm) (work fine in all versions of Android):

object IdHTTPServer1: TIdHTTPServer
  Bindings = <
    item
      IP = '127.0.0.1'
      Port = 50001
    end>
  DefaultPort = 50001
  TerminateWaitTime = 1000
  AutoStartSession = True
  ParseParams = False
  OnCommandGet = IdHTTPServer1CommandGet
end

How i check, that IdHTTPServer1 work fine:

var JSon        : TStringStream;

Json   := TStringStream.Create('Hee ll p', TEncoding.UTF8);
IdHTTP1.Post('http://127.0.0.1:50001', Json);

So i can send POST to self via IdHTTP1, but have problem with js in TWebBrowser. Work fine on Android 8.1 (MIUI 9.6) and dont sent POST on Android 10 (MIUI 12) and Android 12 (MIUI 14).

I made sample Project based on default Demo.

C:\Users\Public\Documents\Embarcadero\Studio\22.0\Samples\Object Pascal\Mobile Snippets\WebBrowser

May be need some parameters in Request? Or some more permissions for app?

procedure TWebBrowserForm.IdHTTPServer1CommandGet(AContext: TIdContext;
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
  var
    Stream : TStream;
begin
  AResponseInfo.ResponseNo:= 200;
  AResponseInfo.ContentText:= 'OKOK';
  if ARequestInfo.Command = 'POST' then
  begin

    Stream := ARequestInfo.PostStream; // Here is nil, if come from Android 10+
    if assigned(Stream) then
    begin
      FAnswer_Content := ReadStringFromStream(Stream, -1, IndyTextEncoding_UTF8);
    end
  end;

end;
1

There are 1 best solutions below

0
Станислав 000 On

It`s was bug in new Delphi 11.3 in TIdHTTPServer. TIdHTTPServer set its Events properties to nil when run on Android.