我正在使用帶有 Webview2 的 winform 并嘗試使用 POST xml 有效負載訪問網站。我對 Webview2 很陌生,似乎無處可去。我可以像往常一樣瀏覽 Webview2,但似乎找不到發布 xml 有效負載的方法。任何樣品將不勝感激!!我嘗試了以下方法,但沒有運氣。
using (var stream = CreateStream(data))
{
webView21.CoreWebView2.AddWebResourceRequestedFilter("*", CoreWebView2WebResourceContext.All);
CoreWebView2WebResourceRequest webResourceRequest = webView21.CoreWebView2.Environment.CreateWebResourceRequest(uri, "POST", stream, "Content-Type: application/x-www-form-urlencoded");
webView21.CoreWebView2.NavigateWithWebResourceRequest(webResourceRequest);
}
}
public static Stream CreateStream(string s)
{
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}
uj5u.com熱心網友回復:
洗掉該using陳述句,您的代碼將起作用(也不需要 AddWebResourceRequestedFilter)。這些CoreWebView2.Navigate*方法啟動導航,但直到引發各種導航事件才完成。導航需要您的流,該導航將在NavigateWithWebResourceRequest方法完成后繼續。
var stream = CreateStream(data);
CoreWebView2WebResourceRequest webResourceRequest = webView21.CoreWebView2.Environment.CreateWebResourceRequest(uri, "POST", stream, "Content-Type: application/x-www-form-urlencoded");
webView21.CoreWebView2.NavigateWithWebResourceRequest(webResourceRequest);
}
public static Stream CreateStream(string s)
{
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}
WebView2 應該在 CoreWebView2.ContentLoading 事件引發后處理流,此時可以釋放流。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/491377.html
