0

I have an url like http://username:password@ipaddress:port//test/test.mjepd.cgi. It actually streams a camera in app browser.

However it works in laptop and computer not in mobile web view. It simple does not load.

By going through some forums I came to know that passing basic authentication credentials is not supported in mobile in app browser webview.

Any help on this is highly appreciated.

HiDeoo
  • 10,353
  • 8
  • 47
  • 47
Naju
  • 1,541
  • 7
  • 27
  • 59

1 Answers1

0

Option 1: Add the Authorization header to the request:

    HashMap<String, String> headers = new HashMap<String, String>();
    String auth = "Basic " + Base64.encodeToString(password.getBytes("UTF-8"), Base64.NO_WRAP);
    headers.put("Authorization", auth);
    webview.loadUrl(url, headers);

Option 2: Handle the authentication through a callback:

    webview.setWebViewClient(new WebViewClient() {
        public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
            if (realm.equals(expectedRealm) && host.equals(ipaddress)) {
                handler.proceed(username, password);
            } else {
                handler.cancel();
            }
        }
    });

Also: if you must use basic auth, do yourself a favor and use https:; don't send passwords in clear text.

kris larson
  • 30,387
  • 5
  • 62
  • 74
  • This url calls up a camera that in turn starts streaming. It does not have OPTIONS enabled so I am unable to call this with headers. Also I am using ionic, the hybrid framework. it does not support onReceivedHttpAuthRequest with any of the available plugins. – Naju Jul 15 '16 at 09:30
  • Apologies, didn't notice the hybrid-mobile-app tag. I've never developed with Ionic, or Cordova, or Angular.js. I looked through a bunch of docs, and I am sure there is way to do this either through a custom plugin or a custom Angular module. Hopefully somebody with Ionic experience can answer. Another idea I had was to pass your credentials on query parameters in the URL and have the server/firewall rewrite the URL to move the credentials to the right place. Apache mod_write does rewrite the URLs before authentication, so this seems like it should work. – kris larson Jul 15 '16 at 14:35