xamarin android webview不再检索使用httplistener在本地提供的页面

时间:2015-03-02 00:11:24

标签: android webview xamarin

我们有一个xamarin android应用程序,其中包含一个httplistener,用于向全屏幕webview中运行的单页面应用程序提供html,js,css和json调用。从Xamarin 3.5开始,webview无法检索我们在端口31316上运行的本地主机地址。在此版本之前,这是正常运行的。

从我所看到的,httplistener似乎很健康,因为我们可以正确地使用WebRequest库调用它,这导致我相信某些内容已经改变为webview。

非常感谢任何协助。

以下示例演示了行为:

using System;
using System.Net;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Webkit;
using Android.Widget;
using System.IO;
using System.Text;

namespace HttpListenerTest
{
    [Activity (Label = "HttpListenerTest", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.Main);
        StartService(new Intent(this, typeof(HttpListenerTestService)));

        var webView = (WebView)this.FindViewById<WebView>(Resource.Id.webview);
        webView.Settings.AllowFileAccess = true;
        webView.Settings.BlockNetworkLoads = false;
        webView.LoadUrl("http://localhost:31316/");
        //webView.LoadData (Activities.html, "text/html", "UTF-8");

        var button = FindViewById<Button>(Resource.Id.button1);
        button.Click += delegate
        {
            AlertDialog.Builder alert1;
            alert1 = new AlertDialog.Builder(this);
            try
            {

                if(!Activities.httpListener.IsListening)
                {
                    alert1.SetMessage("Listener has stopped listening");
                    alert1.Show();
                    return;
                }

                string url = "http://localhost:31316/" + DateTime.Now.ToString("O");

                var request = (HttpWebRequest)WebRequest.Create(new Uri(url));                 
                request.Method = "GET";

                string responseString = "";

                using (WebResponse response = request.GetResponse())
                {
                    StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    responseString = reader.ReadToEnd();
                    response.Close();
                    reader.Close();
                }

                alert1.SetMessage(responseString);

            }
            catch (Exception e)
            {
                alert1.SetMessage(e.Message + " " + e.StackTrace);
            }

            alert1.Show();
        };
    }
}

[Service(Label = "HttpListenerTest Service")]
public class HttpListenerTestService : Service
{
    public override IBinder OnBind(Intent intent)
    {
        return null;
    }

    public override void OnCreate()
    {
        base.OnCreate();
        Activities.InitRest();
    }

    public override void OnDestroy()
    {

    }

    public override void OnStart(Intent intent, int startId)
    {
        base.OnStart(intent, startId);
    }

    private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {

    }
}

public static class Activities
{
    public static HttpListener httpListener { get; set; }

    public static Thread RestThread { get; set; }

    public const string html = "<html>hello world</html>";

    public static void InitRest()
    {
        RestThread = new Thread(StartRest) { Name = "Rest Service" };
        RestThread.Start();
    }

    private static void StartRest()
    {
        httpListener = new HttpListener { IgnoreWriteExceptions = true };
        httpListener.Prefixes.Add(@"http://localhost:31316/");
        httpListener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
        httpListener.Start();
        httpListener.BeginGetContext(HandleRequest, httpListener);
    }

    public static void HandleRequest(IAsyncResult result)
    {
        Console.WriteLine ("==========================HandleRequest==========================");
        var context = httpListener.EndGetContext(result);
        var unescapedUrl = Uri.UnescapeDataString(context.Request.RawUrl);


        var bytes = new byte[html.Length * sizeof(char)];
        Buffer.BlockCopy(html.ToCharArray(), 0, bytes, 0, bytes.Length);

        context.Response.ContentLength64 = bytes.Length;
        context.Response.OutputStream.Write(bytes, 0, bytes.Length);
        context.Response.ContentType = "text/html";
        //context.Response.ContentEncoding = "UTF-8";
        context.Response.OutputStream.Close();
        context.Response.OutputStream.Dispose();
        context.Response.StatusCode = 200;
        httpListener.BeginGetContext(HandleRequest, httpListener);
    }
}

}

0 个答案:

没有答案