android中webview/webviewclient截获401响应

来源:互联网 发布:asd a0421 ab软件 编辑:程序博客网 时间:2024/06/09 12:43

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

代码片段(5)[全屏查看所有代码]

1. [代码]a.jsp (结果可以触发onReceivedHttpAuthRequest事件)     

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

2. [代码]b.jsp 结果不能触发事件     

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

3. [代码]c.jsp 结果可以触发该事件!!     

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

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

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

5. [代码]Main.java     

01package com.my.example;
02  
03import android.app.Activity;
04import android.app.AlertDialog;
05import android.app.ProgressDialog;
06import android.content.DialogInterface;
07import android.content.Intent;
08import android.net.Uri;
09import android.os.Bundle;
10import android.util.Log;
11import android.view.Window;
12import android.webkit.WebSettings;
13import android.webkit.WebView;
14import android.webkit.WebViewClient;
15import android.widget.Toast;
16import android.webkit.HttpAuthHandler;
17  
18public class Main extends Activity {
19    private WebView webview;
20    private static final String TAG = "Main";
21    private ProgressDialog progressBar;
22  
23    
24    @Override
25    public void onCreate(Bundle savedInstanceState) {
26        super.onCreate(savedInstanceState);
27  
28        requestWindowFeature(Window.FEATURE_NO_TITLE);
29  
30        setContentView(R.layout.main);
31  
32        this.webview = (WebView)findViewById(R.string.webview);
33  
34        WebSettings settings = webview.getSettings();
35        settings.setJavaScriptEnabled(true);
36        webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
37  
38        final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
39  
40        progressBar = ProgressDialog.show(Main.this"Example""Loading...");
41  
42        webview.setWebViewClient(new WebViewClient() {
43            public boolean shouldOverrideUrlLoading(WebView view, String url) {
44                Log.i(TAG, "Processing webview url click...");
45                view.loadUrl(url);
46                return true;
47            }
48  
49            public void onPageFinished(WebView view, String url) {
50                Log.i(TAG, "Finished loading URL: " +url);
51                if (progressBar.isShowing()) {
52                    progressBar.dismiss();
53                }
54            }
55  
56            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
57                Log.e(TAG, "Error: " + description);
58             //   Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
59                alertDialog.setTitle("Error");
60                alertDialog.setMessage(description);
61                alertDialog.setButton("OK"new DialogInterface.OnClickListener() {
62                    public void onClick(DialogInterface dialog, int which) {
63                        return;
64                    }
65                });
66                alertDialog.show();
67            }
68            
69            
70            public void   onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
71              
72                alertDialog.setTitle("Error");
73                alertDialog.setMessage(host+view.getUrl());
74                alertDialog.setButton("OK"new DialogInterface.OnClickListener() {
75                    public void onClick(DialogInterface dialog, int which) {
76                        return;
77                    }
78                });
79                alertDialog.show();
80            }
81                      
82        });
83        webview.loadUrl("http://192.168.0.103:8080/web01/d.jsp");
84    }
85}
举报
0 0
原创粉丝点击