Xamarin Android - 使用 CookieContainer 和 javascript 加载 WebView

Xamarin Android - Load a WebView using CookieContainer and javascript

本文关键字:javascript 加载 WebView CookieContainer Android 使用 Xamarin      更新时间:2023-09-26

我正在尝试使用与应用程序中其他 HttpWebRequests 相同的 sessionId 加载 WebView,并且该页面具有需要运行的 javascript。

我正在设置 _webView.JaveScriptEnabled = true;

当我使用 LoadUrl 加载页面时,javascript 会运行,但发送的ASP.NET_SessionId与我的其他 HttpWebRequests 发送的会话 ID 不同,并且我无法设置 CookieContainer:

_webView.LoadUrl(apiBase + "/App/Index");

当我使用 HttpWebRequest 检索页面时,我可以设置 CookieContainer 并加载页面,但 javascript 无法运行:

var httpReq = (HttpWebRequest)HttpWebRequest.Create(new Uri(apiBase + "/App/Index"));
httpReq.CookieContainer = cookieContainer;
var response = (HttpWebResponse)httpReq.GetResponse();
using (StreamReader sr = new StreamReader(response.GetResponseStream()))

{ var viewResult = ;
_webView.LoadData (sr.ReadToEnd(), "text/html", "UTF-8");
}

如何在一次网络通话中同时获得这两种行为?

我找到了,我需要改变
_webView.LoadData (sr.ReadToEnd(), "text/html", "UTF-8");

_webView.LoadDataWithBaseURL (apiBase, sr.ReadToEnd(), "text/html", "UTF-8", ");