返回顶部

收藏

android中webview/webviewclient截获401响应

更多

http://blog.sina.com.cn/s/blog_444ee46f0100p7ee.html自定义了一种http认证方式,结果发现android 上使用webview访问相关页面时,在返回的401中,如果没有basic或者digest认证,就不会触发onReceivedHttpAuthRequest( )事件。开始设想用onReceivedError(),结果发现该事件只有在没有网络返回的情况下才会触发,而http的header并不会触发该事件。于是又回到 onReceivedHttpAuthRequest(),试验分别如下:

a.jsp (结果可以触发onReceivedHttpAuthRequest事件)

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <% response.setStatus(401,"authen required"); %>
    <% response.addHeader("WWW-Authenticate","Basic realm="Secure Area a""); %>
<html>
<body>
test.
</body>
</html>

b.jsp 结果不能触发事件

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <% response.setStatus(401,"authen required"); %>
    <% response.addHeader("WWW-Authenticate","MyAuth realm="Secure Area b""); %>
<html>
<body>
test
</body>
</html>

c.jsp 结果可以触发该事件!!

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <% response.setStatus(401,"authen required"); %>
    <% response.addHeader("WWW-Authenticate","TestBasic realm="Secure Area c",a=b"); %>
<html>
<body>
test.
</body>
</html>

d.jsp 结果可以触发该事件。(说明:http规范容许多种认证方式同时支持。)

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <% response.setStatus(401,"authen required"); %>
    <% response.addHeader("WWW-Authenticate","MyAuth realm="Secure Area d""); %>
    <% response.addHeader("WWW-Authenticate","Basic realm="Secure Area d""); %>
<html>
<body>
test
</body>
</html>

Main.java

package com.my.example;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import android.webkit.HttpAuthHandler;

public class Main extends Activity {
    private WebView webview;
    private static final String TAG = "Main";
    private ProgressDialog progressBar; 

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

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.main);

        this.webview = (WebView)findViewById(R.string.webview);

        WebSettings settings = webview.getSettings();
        settings.setJavaScriptEnabled(true);
        webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

        final AlertDialog alertDialog = new AlertDialog.Builder(this).create();

        progressBar = ProgressDialog.show(Main.this, "Example", "Loading...");

        webview.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.i(TAG, "Processing webview url click...");
                view.loadUrl(url);
                return true;
            }

            public void onPageFinished(WebView view, String url) {
                Log.i(TAG, "Finished loading URL: " +url);
                if (progressBar.isShowing()) {
                    progressBar.dismiss();
                }
            }

            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Log.e(TAG, "Error: " + description);
             //   Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
                alertDialog.setTitle("Error");
                alertDialog.setMessage(description);
                alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        return;
                    }
                });
                alertDialog.show();
            }

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

                alertDialog.setTitle("Error");
                alertDialog.setMessage(host+view.getUrl());
                alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        return;
                    }
                });
                alertDialog.show();
            }

        });
        webview.loadUrl("http://192.168.0.103:8080/web01/d.jsp");
    }
}

标签:Android,SDK,WebKit

收藏

0人收藏

支持

0

反对

0

发表评论