0

我正在开发一个移动应用程序来访问 android 中的 Intranet 站点。这个网站的 SSL 证书很好。我可以这样说,因为当我在 android 浏览器中打开网站时,它并没有说证书有问题。现在我正在使用 webviewclient 访问我的应用程序中的同一站点,并且该应用程序引发 401.2 错误 - 您无权查看此页面。请帮助我。

    package com.example.kms;

import android.app.Activity;
import android.graphics.Bitmap;
import android.net.http.SslCertificate;
import android.net.http.SslError;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.CookieManager;

/*
 * Demo of creating an application to open any URL inside the application and clicking on any link from that URl
should not open Native browser but  that URL should open in the same screen.
 */
public class MainActivity extends Activity {
    /** Called when the activity is first created. */

    WebView web;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        web = (WebView) findViewById(R.id.webview01);
        web.setWebViewClient(new myWebClient());
        web.getSettings().setJavaScriptEnabled(true);
        web.loadUrl("https://kms.techmahindra.com");
    }

    public class myWebClient extends WebViewClient
    {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub


        view.loadUrl("https://kms.techmahindra.com");

            return true;

        }
        @Override 
        public void onReceivedLoginRequest(WebView view, String realm, 
                        String account, String args) { 

                super.onReceivedLoginRequest(view, realm, account, args); 
                System.err.println(realm); 
                System.err.println(account); 
                System.out.println(args); 
        } 
        @Override 
        public void onReceivedSslError(WebView view, SslErrorHandler handler, 
                        SslError error) { 
                System.err.println("SSL ERROR"); 
                SslCertificate certificate = error.getCertificate(); 
                System.err.println(certificate.getIssuedBy().getCName()); 
                System.err.println(certificate.getIssuedBy().getDName()); 
                System.err.println(certificate.getValidNotAfterDate()); 
                //handler.handleMessage(handler.obtainMessage()); 
                //handler.proceed(); 
        } 
        @Override 
        public void onReceivedError(WebView view, int errorCode, 
                        String description, String failingUrl) { 
                // TODO Auto-generated method stub 
                super.onReceivedError(view, errorCode, description, failingUrl); 
                System.err.println(errorCode + " - " + description + "-" + failingUrl); 
        } 
    }

    // To handle "Back" key press event for WebView to go back to previous screen.
   @Override
   public boolean onKeyDown(int keyCode, KeyEvent event)
  {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
        web.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
   }
}

这是我完整的logcat:

08-10 14:43:23.495: D/kernel(130): [17865.949615] max17040 0-0036: batt:100%, 4186 mV
08-10 14:43:26.735: D/AndroidRuntime(9740): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
08-10 14:43:26.735: D/AndroidRuntime(9740): CheckJNI is OFF
08-10 14:43:27.075: D/AndroidRuntime(9740): Calling main entry com.android.commands.pm.Pm
08-10 14:43:27.105: D/AndroidRuntime(9740): Shutting down VM
08-10 14:43:27.105: D/dalvikvm(9740): GC_CONCURRENT freed 101K, 82% free 466K/2560K, paused 0ms+0ms
08-10 14:43:27.105: D/jdwp(9740): Got wake-up signal, bailing out of select
08-10 14:43:27.105: D/dalvikvm(9740): Debugger has detached; object registry had 1 entries
08-10 14:43:27.555: D/AndroidRuntime(9752): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
08-10 14:43:27.565: D/AndroidRuntime(9752): CheckJNI is OFF
08-10 14:43:27.916: D/AndroidRuntime(9752): Calling main entry com.android.commands.am.Am
08-10 14:43:27.925: I/ActivityManager(251): START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.kms/.MainActivity} from pid 9752
08-10 14:43:27.955: D/AndroidRuntime(9752): Shutting down VM
08-10 14:43:27.955: D/dalvikvm(9752): GC_CONCURRENT freed 101K, 81% free 488K/2560K, paused 1ms+1ms
08-10 14:43:27.955: D/jdwp(9752): Got wake-up signal, bailing out of select
08-10 14:43:27.955: D/dalvikvm(9752): Debugger has detached; object registry had 1 entries
08-10 14:43:28.205: I/ActivityManager(251): Displayed com.example.kms/.MainActivity: +242ms
08-10 14:43:28.345: D/SurfaceFlinger(122): Release buffer at 0xb3180
08-10 14:43:28.395: D/SurfaceFlinger(122): Release buffer at 0x747e8
08-10 14:43:40.555: D/kernel(130): [17883.013092] gpio_keys_scan_keys: key 1-66, 0 (200) continue debounce
08-10 14:43:40.565: D/kernel(130): [17883.023223] gpio_keys_scan_keys: key 1-66, 0 (200) continue debounce
08-10 14:43:40.575: D/kernel(130): [17883.033325] gpio_keys_scan_keys: key 1-66, 0 (200) continue debounce
08-10 14:43:40.715: D/kernel(130): [17883.173370] gpio_keys_scan_keys: key 1-66, 0 (200) continue debounce
08-10 14:43:40.725: D/kernel(130): [17883.183502] gpio_keys_scan_keys: key 1-66, 0 (200) continue debounce
08-10 14:43:40.735: D/kernel(130): [17883.193634] gpio_keys_scan_keys: key 1-66, 0 (200) continue debounce
08-10 14:43:40.755: I/ActivityManager(251): START {act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10200000 cmp=com.sonyericsson.home/.HomeActivity} from pid 251
08-10 14:43:40.855: E/Storage(548): I/O error when reading: slowloading.zip
08-10 14:43:40.895: D/OpenGLRenderer(9465): Flushing caches (mode 1)
08-10 14:43:40.895: D/OpenGLRenderer(9465): Flushing caches (mode 0)
08-10 14:43:41.115: D/SurfaceFlinger(122): Release buffer at 0xba0c0
08-10 14:43:41.135: D/dalvikvm(548): GC_FOR_ALLOC freed 6623K, 41% free 15630K/26119K, paused 40ms
08-10 14:43:41.205: W/InputManagerService(251): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@2c28f418 (uid=10152 pid=9465)
08-10 14:43:41.305: D/dalvikvm(548): GC_CONCURRENT freed 450K, 35% free 17218K/26119K, paused 2ms+6ms
08-10 14:43:41.385: D/dalvikvm(548): GC_CONCURRENT freed 1947K, 34% free 17283K/26119K, paused 2ms+5ms
08-10 14:43:41.405: I/ActivityManager(251): Displayed com.sonyericsson.home/.HomeActivity: +624ms
08-10 14:43:41.595: D/dalvikvm(548): GC_CONCURRENT freed 138K, 27% free 19192K/26119K, paused 3ms+5ms
08-10 14:43:41.615: D/SurfaceFlinger(122): Release buffer at 0x747e8
08-10 14:43:42.595: D/dalvikvm(548): GC_CONCURRENT freed 711K, 22% free 20487K/26119K, paused 2ms+5ms
4

1 回答 1

1

尝试在您的 WebViewClient 中覆盖 onReceivedHttpAuthRequest() ,如下所示:

 import android.webkit.HttpAuthHandler;

HttpAuthHandler

...

 @Override
    public void onReceivedHttpAuthRequest(WebView view,HttpAuthHandler handler, String host, String realm) {

        super.onReceivedHttpAuthRequest(view, handler, host, realm);

        view.setHttpAuthUsernamePassword(host, realm, username, password);

    }

资源链接:链接

看看这是否适合你。

如果没有,你也可以试试这个:)

于 2013-08-10T09:55:11.410 回答