Azure Mobile Services Client utilizing proxy settings in Android Apps

Azure Mobile Services Client utilizing proxy settings in Android Apps

Getting your Azure Mobile Services Client to utilize proxy settings in Android Apps

A student of mine asked in interesting question during class the other night.  How do I get my Android app, built using Xamrin, to utilize the proxy settings I setup in my Android emulator? Essentially the question is how do I get Azure Mobile Services Client to support proxy settings?

He further explained that it seems that the proxy settings are working fine because he could browse the internet using Chrome but the Android app was not able to communicate out to his Azure Mobile Service?

It would seem like this should just work, set the proxy settings up in the emulator, and all of the apps should just use it right?  Well…. Unfortunately no…

It turns out that the Azure Mobile Service Client, Microsoft.Windows.Azure.MobileServices.MobileServiceClient, doesn’t use the proxy automatically. Fortunately it has a constructor does take, an oh so important,  HttpClientHandler instance.  With this it’s a rather simple matter of creating an HttpClientHandler that utilizes a proxy if one exists. I created a simple HttpAutoProxyHandler, as shown below, that determines if a proxy is used and then constructs a WebProxy instance with the proxy’s IP address and Port number.

Here is a rather simple HttpAutoProxyHandler that illustrates the foundation of a solution:

HttpAutoProxyHandler

using System;
using System.Net;
using System.Net.Http;
using Java.Lang;

namespace AndroidClient
{
 /// <summary>
 /// Initializes a new instance of the <see cref="HttpAutoProxyHandler"/> class.
 /// </summary>
 /// <param name="schemeName">Name of the scheme (http or https).</param>
 public class HttpAutoProxyHandler : HttpClientHandler
 {
  public HttpAutoProxyHandler(string schemeName = "http")
  {
   // Eqv: System.getProperty(http.ProxyHost)
   // Eqv: System.getProperty(http.ProxyPort)
   string hostIpAddress = JavaSystem.GetProperty(string.Format("{0}.proxyHost", schemeName));
   string hostPort = JavaSystem.GetProperty(string.Format("{0}.proxyPort",schemeName));

   // Setup to use proxy if one is found
   if (!string.IsNullOrWhiteSpace(hostIpAddress))
   {
    // Instruct to use proxy
    UseProxy = true;

    string uriString;

    if (string.IsNullOrWhiteSpace(hostPort))
    {
      uriString = string.Format("{0}://{1}", schemeName,hostIpAddress);
    }
    else
    {
      uriString = string.Format("{0}://{1}:{2}",schemeName, hostIpAddress,hostPort);
    }

    Proxy = new WebProxy(new Uri(uriString));
   }
  }
 }
}

Now all that is needed is to use an instance of the HttpAutoProxyHandler when creating an instance of the MobileServiceClient like this:

 

Enabling Azure Mobile Services Client to utilize proxy settings

MobileServiceClient mobileService = new MobileServiceClient("<your mobile service uri>", "<your api key>", new HttpAutoProxyHandler());

Note:

This won’t handle cases where the proxy is flipped on or off after the app is loaded unless the mobile service client is instantiated each time a new request is made or you detect the change in the proxy settings and instantiate a new mobile client. I may look into handling that case a bit later stay tuned…

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>