使用 WebView/EdgeHTML 发送 POST 请求

Sending POST request with WebView/EdgeHTML

我目前正在尝试让 EdgeHTML/WebView 根据这个答案工作:

复制代码 运行 它工作得很好。

但现在我正在尝试添加 "NavigateWithHttpRequestMessage" 程序,以便我可以发送 POST 请求,并且必须意识到我不知道我应该怎么做为其参数创建对象。

这是程序的描述: https://docs.microsoft.com/en-us/uwp/api/windows.web.ui.iwebviewcontrol.navigatewithhttprequestmessage

它告诉我参数的类型是"HttpRequestMessage"。

我已经下载了 Windows 10 套件,并在里面找到了 Windows.Web.Http.idl 和 "HttpRequestMessage" 的界面:

[exclusiveto(Windows.Web.Http.HttpRequestMessage)]
[uuid(F5762B3C-74D4-4811-B5DC-9F8B4E2F9ABF)]
interface IHttpRequestMessage : IInspectable
{
[propget] HRESULT Content([out] [retval] Windows.Web.Http.IHttpContent** value);
[propput] HRESULT Content([in] Windows.Web.Http.IHttpContent* value);
[propget] HRESULT Headers([out] [retval] Windows.Web.Http.Headers.HttpRequestHeaderCollection** value);
[propget] HRESULT Method([out] [retval] Windows.Web.Http.HttpMethod** value);
[propput] HRESULT Method([in] Windows.Web.Http.HttpMethod* value);
[propget] HRESULT Properties([out] [retval] Windows.Foundation.Collections.IMap<HSTRING, IInspectable*>** value);
[propget] HRESULT RequestUri([out] [retval] Windows.Foundation.Uri** value);
[propput] HRESULT RequestUri([in] Windows.Foundation.Uri* value);
[propget] HRESULT TransportInformation([out] [retval] Windows.Web.Http.HttpTransportInformation** value);
}

我可以将其转换为 Delphi 中的接口,就像 NineBerry 对上述 link 中的其他接口所做的那样。 (或者至少使用虚拟程序。还不确定参数的类型。)

但是我如何从中创建一个对象,以便我可以将它用于 "NavigateWithHttpRequestMessage" 过程?

我们将不胜感激任何帮助甚至指向正确方向的指示。

根据official docs,我们可以在C#中使用这样的方法:

var httprequest = new HttpRequestMessage(HttpMethod.Post, new Uri(url))   
webView.NavigateWithHttpRequestMessage(httprequest);

我找不到 Delphi 示例。您可以参考 C# examples 并尝试将其转换为 Delphi.

如果您已经对接口有所了解,答案就很简单了。但我最终还是弄明白了:

根据 header 或 .idl 所说的内容创建您的界面:

[WinRTClassNameAttribute('Windows.Web.Http.HttpRequestMessage')]
IHttpRequestMessage = interface(IInspectable)
['{F5762B3C-74D4-4811-B5DC-9F8B4E2F9ABF}']
  procedure Placeholder_ContentGet; safecall;
  procedure Placeholder_ContentPut; safecall;
  procedure Placeholder_HeadersGet; safecall;
  procedure Placeholder_MethodGet; safecall;
  procedure put_Method(value:IHttpMethod); safecall;
  procedure Placeholder_PropertiesGet; safecall;
  procedure Placeholder_RequestUriGet; safecall;
  procedure put_RequestUri(source: IUriRuntimeClass); safecall;
  procedure Placeholder_TransportInformationGet; safecall;
end;

然后在它下面添加一个 CoClass:

THttpRequestMessage = class(TWinRTGenericImportI<IHttpRequestMessage>)
end;

然后可以这样使用:

procedure TForm1.Button1Click(Sender: TObject);
var
  req: IHttpRequestMessage;
begin
  req := THttpRequestMessage.Create;
end;