2

I create android app with WebView inside myActivity.

public void onCreate( Bundle savedInstanceState) { super.onCreate( savedInstanceState);

  // before setContentView
  CookieSyncManager.createInstance( this);
  CookieSyncManager.getInstance().startSync();
  CookieManager.getInstance().removeAllCookie();
  CookieManager.getInstance().setAcceptCookie( true);

  setContentView( R.layout.browser);

      webviewHTML = (WebView) findViewById( R.id.webviewHTML);
      WebSettings webSettings = webviewHTML.getSettings();
      webSettings.setSupportZoom( true);
      webSettings.setBuiltInZoomControls( true);
      webSettings.setJavaScriptEnabled( true);
      webSettings.setJavaScriptCanOpenWindowsAutomatically( true);
      webSettings.setAppCacheEnabled( false);
      webSettings.setSaveFormData( false);
      webSettings.setSavePassword( false);
      webSettings.setDefaultTextEncodingName( "utf-8");
      webSettings.setDomStorageEnabled( true);
      webSettings.setUserAgent( 0);
      webviewHTML.setWebViewClient( new myWebViewClient());
      webviewHTML.addJavascriptInterface( new myJavaScriptInterface(), getString( R.string.sAppName));
      webviewHTML.setOnLongClickListener( onWebViewLongClickListener);

      webviewHTML.loadUrl( "myaddress");
}

When create WebView I open page of my corporate Microsoft Exchange 2010 OWA 'https://mail.mydomain.com/owa/':

enter image description here

Then I manual enter username and password from my domain. But I get error message page inside WebView (response code 200):

<head><head></head><body>Bad Request</body></head>

Screenshot:

enter image description here

But when I open this page inside any browser (Chrome) I correct enter to OWA mail page: enter image description here

Why Exchange server return html-page with error inside body?

What I need add to WebView properties?

Add: Also when I open this page in desktop browser on PC window for username and password popup inside browser (not site):

enter image description here

Add solution I find my error. I check URL in WebView.myWebViewClient.onPageFinish and URL was different with my starting URL. I correct this URL and my page will open correct.

Tapa Save
  • 4,769
  • 5
  • 32
  • 54

1 Answers1

3

Change:

webSettings.setUserAgent( 0);

For

webSettings.setUserAgentString("User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko");

Explanation: you are purposefully setting identifying yourself (the webview) as an inexistent browser, and Exchange probably doesn't like that. With this change, you will indentify yourself as Firefox.

Answer extension

Also, in order to get rid of that authentication request, you should use add to the webview your own class which extends WebViewClient and implements onReceivedHttpAuthRequest().

For example:

    webviewHTML = (WebView) findViewById( R.id.webviewHTML);

    (...)

    MPascualViewClient myNewClient = new MPascualViewClient();
    webviewHTML.setWebViewClient(myNewClient);

    (...)

    webviewHTML.loadUrl( "myaddress");
}

private class MPascualViewClient extends WebViewClient {

    @Override
    public void onReceivedHttpAuthRequest(WebView view,
    HttpAuthHandler handler, String host, String realm) {
        Log.d(this.getClass().getName(), "onReceivedHttpAuthRequest: " + host);
        String user = "a username"; //get this string from wherever you want
        String password = "a password"; //get this string from wherever you want

        handler.proceed(user, password);

    }
}
Mikel Pascual
  • 2,202
  • 18
  • 27
  • thanks, but this not work for me. Also I add new info about open this page in desktop browser on PC. – Tapa Save Jul 14 '14 at 12:35
  • I added more code to answer you question's extension – Mikel Pascual Jul 19 '14 at 17:39
  • I try implement this code, but not work for me. You forward me to another direction and I find my error. I check URL in WebView onPageFinish and URL different from my start URL. I correct this URL and my page will open correct. Thank you! – Tapa Save Jul 22 '14 at 07:09
  • This fixed the same problem for me where I was using HttpGet to download attachments from Outlook Web Access, and OWA required both the Cookies and User-Agent header fields to be set. – Rupert Rawnsley Sep 22 '14 at 13:51