How to initiate a post request using webview2?

Post Reply
raychl
Posts: 4
Joined: Mon Nov 25, 2019 7:00 am

How to initiate a post request using webview2?

Post by raychl »

C# has an example:

Code: Select all

var postData = "thing1=hello";
    postData += "&thing2=world";
var data = Encoding.ASCII.GetBytes(postData);
MemoryStream stream = new MemoryStream();
stream.Write(data, 0, data.Length);
var request = webView.CoreWebView2.Environment.CreateWebResourceRequest(navigateData.Url, 
              "POST", stream, "Content-Type: application/x-www-form-urlencoded");
webView.CoreWebView2.NavigateWithWebResourceRequest(request);
stream.Close();
stream.Dispose();
ICoreWebView2Environment does not have CreateWebResourceRequest。。
raychl
Posts: 4
Joined: Mon Nov 25, 2019 7:00 am

Re: How to initiate a post request using webview2?

Post by raychl »

I use the following code, but sometimes it throw a pointer access exception.

Code: Select all

procedure TBrowserFrame.PostDataByWVB;
var
  ARequest:ICoreWebView2WebResourceRequest;
  AStream:TMemoryStream;
  AIStream : IStream;
  AResult:Boolean;
begin
   AStream:=TStringStream.Create('key1=111&key2=123');
   AIStream:=TStreamAdapter.Create(AStream,soReference);
   AResult:=False;
   try
     AResult:=WVBrowser1.CoreWebView2Environment.CreateWebResourceRequest(URLCbx.Text,'POST',AIStream,'Content-Type: application/x-www-form-urlencoded',ARequest);
     if (AResult) then
     begin
        AResult:=WVBrowser1.NavigateWithWebResourceRequest(ARequest);
     end;
   finally
      AStream.Free;
      AIStream._Release;
   end;
end;
User avatar
salvadordf
Posts: 4121
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: How to initiate a post request using webview2?

Post by salvadordf »

I'll try to include a NavigateWithWebResourceRequest example in the next webView4Delphi update.
User avatar
salvadordf
Posts: 4121
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: How to initiate a post request using webview2?

Post by salvadordf »

I just uploaded the new NavigateWithWebResourceRequestBrowser demo.

Download WebView4Delphi from GitHub to see it.
Post Reply