回复'错误请求'来自MS Exchange OWA的HTML-body

时间:2014-07-14 11:55:58

标签: java android webview exchange-server outlook-web-app

我在myActivity中创建了带有WebView的Android应用程序。

  

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

创建WebView时,我打开了我的企业Microsoft Exchange 2010 OWA页面https://mail.mydomain.com/owa/':

enter image description here

然后我手动输入我的域名的用户名和密码。 但是我在WebView中得到了错误消息页面(响应代码200):

  

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

截图:

enter image description here

但是当我在任何浏览器(Chrome)中打开此页面时,我更正输入OWA邮件页面: enter image description here

为什么Exchange服务器在正文中返回html-page错误?

我需要添加到WebView属性中吗?

添加: 此外,当我在PC窗口的桌面浏览器中打开此页面以在浏览器(而不是站点)内弹出用户名和密码时:

enter image description here

添加解决方案我发现了我的错误。我检查了WebView.myWebViewClient.onPageFinish中的URL,并且URL与我的起始URL不同。我更正了此网址,我的网页将正确打开。

1 个答案:

答案 0 :(得分:3)

变化:

webSettings.setUserAgent( 0);

有关

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

说明:您有意将自己(webview)标识为不存在的浏览器,而Exchange可能不喜欢这样。通过此更改,您将自己标识为Firefox。

解答扩展程序

此外,为了摆脱该身份验证请求,您应该使用自己的类添加到webview,该类扩展WebViewClient并实现onReceivedHttpAuthRequest()

例如:

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

    }
}