Xamarin android webview no longer gets page served locally with httplistener

We have an xamarin Android application that contains an httplistener that handles html, js, css and json calls to a single-page application running in full-screen web browsing. Starting with Xamarin 3.5, web browsing cannot get this local host address, which we run on port 31316. Prior to this release, this worked correctly.

From what I see, httplistener seems healthy, since we can correctly call it the WebRequest library, which makes me think that something has changed to web browsing.

Any help would be greatly appreciated.

The following is an example application demonstrating behavior:

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);
    }
}

}

+4

All Articles