Xamarin WebView 在特殊页面后加载 URL
Xamarin WebView load URL after special page
您好,我尝试对某些网站进行身份验证。登录页面后,该站点将我的 webview 重定向到另一个站点,新的 url 对我来说有重要的参数。本页return我的错误
"Web page is not available net::ERR_CONNECTION_REFUSED"
现在,在传递到登录页面后,我从新 url 获取参数,并尝试将 webview 重定向到我的页面。
public class MainActivity : AppCompatActivity
{
WebView app_view = null;
WebSettings app_web_settings = null;
WebChromeClient web_client;
MyWebViewClient my_web_client;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
try
{
app_view = FindViewById(Resource.Id.webViewApp) as WebView;
app_web_settings = app_view.Settings;
app_web_settings.JavaScriptEnabled = true;
app_web_settings.AllowUniversalAccessFromFileURLs = true;
app_web_settings.DomStorageEnabled = true;
app_web_settings.DatabaseEnabled = true;
app_web_settings.SetRenderPriority(WebSettings.RenderPriority.High);
app_view.SetLayerType(LayerType.Hardware, null);
my_web_client = new MyWebViewClient(this);
web_client = new WebChromeClient();
app_view.SetWebViewClient(my_web_client);
app_view.SetWebChromeClient(web_client);
string app_url = "http://examplesite.com";
app_view.LoadUrl(app_url);
app_view.AddJavascriptInterface(new Foo(this), "foo");
}
catch (Exception ex)
{
Console.WriteLine("Error -> " + ex.ToString());
}
}
}
public class MyWebViewClient : WebViewClient
{
MainActivity act;
public MyWebViewClient(MainActivity activity)
{
this.act = activity;
}
public override void OnPageStarted(WebView view, string url, Android.Graphics.Bitmap favicon)
{
base.OnPageStarted(view, url, favicon);
if (url.Length > 7)
{
int index = url.IndexOf("?code=");
if (index > -1)
{
//------------------------------
// In this line, I want to redirect my page
view.LoadUrl("file:///android_asset/app_pages/home.html");
}
}
}
public override void OnReceivedError(WebView view, IWebResourceRequest request, WebResourceError error)
{
base.OnReceivedError(view, request, error);
//------------------------------
// In this line, I want to redirect my page
view.LoadUrl("file:///android_asset/app_pages/home.html");
}
public override void OnPageFinished(WebView view, string url)
{
base.OnPageFinished(view, url);
}
}
我尝试在 OnPageStarted 或 OnReceivedError 事件中加载URL。但是 webview 无法加载 this URL。
我该如何解决这个问题?
尝试覆盖 MyWebViewClient 中的 ShouldOverrideUrlLoading
方法:
public override bool ShouldOverrideUrlLoading(WebView view, IWebResourceRequest request)
{
string url = request.Url.ToString();
if (url.Length > 7)
{
int index = url.IndexOf("?code=");
if (index > -1)
{
//------------------------------
// In this line, I want to redirect my page
view.LoadUrl("file:///android_asset/app_pages/home.html");
}
}
return true;
}
您好,我尝试对某些网站进行身份验证。登录页面后,该站点将我的 webview 重定向到另一个站点,新的 url 对我来说有重要的参数。本页return我的错误
"Web page is not available net::ERR_CONNECTION_REFUSED"
现在,在传递到登录页面后,我从新 url 获取参数,并尝试将 webview 重定向到我的页面。
public class MainActivity : AppCompatActivity
{
WebView app_view = null;
WebSettings app_web_settings = null;
WebChromeClient web_client;
MyWebViewClient my_web_client;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
try
{
app_view = FindViewById(Resource.Id.webViewApp) as WebView;
app_web_settings = app_view.Settings;
app_web_settings.JavaScriptEnabled = true;
app_web_settings.AllowUniversalAccessFromFileURLs = true;
app_web_settings.DomStorageEnabled = true;
app_web_settings.DatabaseEnabled = true;
app_web_settings.SetRenderPriority(WebSettings.RenderPriority.High);
app_view.SetLayerType(LayerType.Hardware, null);
my_web_client = new MyWebViewClient(this);
web_client = new WebChromeClient();
app_view.SetWebViewClient(my_web_client);
app_view.SetWebChromeClient(web_client);
string app_url = "http://examplesite.com";
app_view.LoadUrl(app_url);
app_view.AddJavascriptInterface(new Foo(this), "foo");
}
catch (Exception ex)
{
Console.WriteLine("Error -> " + ex.ToString());
}
}
}
public class MyWebViewClient : WebViewClient
{
MainActivity act;
public MyWebViewClient(MainActivity activity)
{
this.act = activity;
}
public override void OnPageStarted(WebView view, string url, Android.Graphics.Bitmap favicon)
{
base.OnPageStarted(view, url, favicon);
if (url.Length > 7)
{
int index = url.IndexOf("?code=");
if (index > -1)
{
//------------------------------
// In this line, I want to redirect my page
view.LoadUrl("file:///android_asset/app_pages/home.html");
}
}
}
public override void OnReceivedError(WebView view, IWebResourceRequest request, WebResourceError error)
{
base.OnReceivedError(view, request, error);
//------------------------------
// In this line, I want to redirect my page
view.LoadUrl("file:///android_asset/app_pages/home.html");
}
public override void OnPageFinished(WebView view, string url)
{
base.OnPageFinished(view, url);
}
}
我尝试在 OnPageStarted 或 OnReceivedError 事件中加载URL。但是 webview 无法加载 this URL。 我该如何解决这个问题?
尝试覆盖 MyWebViewClient 中的 ShouldOverrideUrlLoading
方法:
public override bool ShouldOverrideUrlLoading(WebView view, IWebResourceRequest request)
{
string url = request.Url.ToString();
if (url.Length > 7)
{
int index = url.IndexOf("?code=");
if (index > -1)
{
//------------------------------
// In this line, I want to redirect my page
view.LoadUrl("file:///android_asset/app_pages/home.html");
}
}
return true;
}