Issue
I've developed a cross platform mobile app with Xamarin.Forms. I have local services that my app hits. My Xamarin app defines the HTTPClient as shown below. When testing on BrowserStack's App Live product I can not hit my local services from my app using the BrowserStack Local app (I receive a "No Such Host Is Known" response) . I can hit my services using the device's default browser.
HTTPClient setup in Xamarin App:
public static readonly HttpClient client = new HttpClient()
My local services have internal domain names (it's not simply 'localhost:443' but it actually has a domain name like 'customservice.com')
Is it possible for a Xamarin App to use the BrowserStack App Live product while using Local Testing? If so, how?
Solution
I found a solution that works for Android and should work for iOS (but doesn't). Maybe with some tinkering it could work for iOS too, but I thought I'd share what I found:
Step 1: Create a Dependency Service
This step is loosely based on this article. The article is incomplete because it does not create a dependency service. So that's fun
- In the Shared Project create a
IProxyInfoProvider
interface as shown:
public interface IProxyInfoProvider
{
WebProxy GetProxySettings();
}
- Create the platform implementations: (Don't forget to add the assembly tag above your namespace to export the Dependency Service!)
iOS
public class ProxyInfoProvider : IProxyInfoProvider
{
public WebProxy GetProxySettings()
{
var systemProxySettings = CFNetwork.GetSystemProxySettings();
var proxyPort = systemProxySettings.HTTPPort;
var proxyHost = systemProxySettings.HTTPProxy;
Console.WriteLine("Proxy Port: " + proxyPort.ToString());
Console.WriteLine("Proxy Host: " + Convert.ToInt64(proxyHost));
return !string.IsNullOrEmpty(proxyHost) && proxyPort != 0
? new WebProxy(proxyHost, proxyPort)
: null;
}
}
Android
public class ProxyInfoProvider : IProxyInfoProvider
{
public WebProxy GetProxySettings()
{
var proxyHost = JavaSystem.GetProperty("http.proxyHost");
var proxyPort = JavaSystem.GetProperty("http.proxyPort");
Console.WriteLine("Proxy Host: " + proxyHost);
Console.WriteLine("Proxy Port: " + proxyPort);
return !string.IsNullOrEmpty(proxyHost) && !string.IsNullOrEmpty(proxyPort)
? new WebProxy($"{proxyHost}:{proxyPort}")
: null;
}
}
Step 2: Update your HttpClientHandler
- We want to consume the WebProxy that is now being returned from the dependency service. Update your HttpClient handler so it looks something like this:
var _handler = new HttpClientHandler();
_handler.Proxy = DependencyService.Get<IProxyInfoProvider>().GetProxySettings();
Ensure that your HttpClient
is consuming this Handler in it's constructor like: new HttpClient(_handler.Value)
Step 3: Upload your app to BrowserStack
- Boom. Then Android works with BrowserStack local testing! Why doesn't it work on iOS you ask? Good question. I'm still trying to figure that part out...
- What is also weird is that if you use a proxy to debug your app in your local environment, this solution works! But it stops working when you put your app in BrowserStack.
Answered By - RangerRick
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.