android webview 添加证书,android-webview – 带证书的Android 4.0 Webview SSL身份验证

代码是

https://github.com/potaka001/WebViewBasicAuthTest

当然,您对onReceivedHttpAuthRequest方法感兴趣.

package com.webviewbasicauthtest;

import android.app.Activity;

import android.app.AlertDialog;

import android.app.AlertDialog.Builder;

import android.content.Context;

import android.content.DialogInterface;

import android.graphics.Bitmap;

import android.net.http.SslError;

import android.webkit.CookieManager;

import android.webkit.HttpAuthHandler;

import android.webkit.SslErrorHandler;

import android.webkit.WebView;

import android.webkit.WebViewClient;

import android.widget.EditText;

import android.widget.LinearLayout;

import android.widget.TextView;

import android.widget.Toast;

public class MyWebViewClient extends WebViewClient {

private String loginCookie;

private Context mContext;

private WebView mWebView;

public MyWebViewClient(Context context,WebView webview) {

super();

mContext = context;

mWebView = webview;

}

@Override

public void onPageStarted( WebView view,String url,Bitmap favicon ) {

}

@Override

public void onPageFinished( WebView view,String url ) {

CookieManager cookieManager = CookieManager.getInstance();

cookieManager.setCookie(url,loginCookie);

}

@Override

public void onReceivedError( WebView view,int errorCode,String description,String failingUrl ) {

Toast.makeText(view.getContext(),"ページ読み込みエラー",Toast.LENGTH_LONG).show();

}

@Override

public void onLoadResource( WebView view,String url ){

CookieManager cookieManager = CookieManager.getInstance();

loginCookie = cookieManager.getCookie(url);

}

@Override

public boolean shouldOverrideUrlLoading( WebView view,String url ) {

return false;

}

@Override

public void onReceivedSslError( WebView view,SslErrorHandler handler,SslError error ) {

handler.proceed();

}

@Override

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

String userName = null;

String userPass = null;

if (handler.useHttpAuthUsernamePassword() && view != null) {

String[] haup = view.getHttpAuthUsernamePassword(host,realm);

if (haup != null && haup.length == 2) {

userName = haup[0];

userPass = haup[1];

}

}

if (userName != null && userPass != null) {

handler.proceed(userName,userPass);

}

else {

showHttpAuthDialog(handler,host,realm,null,null);

}

}

private void showHttpAuthDialog( final HttpAuthHandler handler,final String realm,final String title,final String name,final String password ) {

LinearLayout llayout = new LinearLayout((Activity)mContext);

final TextView textview1 = new TextView((Activity)mContext);

final EditText edittext1 = new EditText((Activity)mContext);

final TextView textview2 = new TextView((Activity)mContext);

final EditText edittext2 = new EditText((Activity)mContext);

llayout.setOrientation(LinearLayout.VERTICAL);

textview1.setText("username:");

textview2.setText("password:");

llayout.addView(textview1);

llayout.addView(edittext1);

llayout.addView(textview2);

llayout.addView(edittext2);

final Builder mHttpAuthDialog = new AlertDialog.Builder((Activity)mContext);

mHttpAuthDialog.setTitle("Basic Authentication")

.setView(llayout)

.setCancelable(false)

.setPositiveButton("OK",new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog,int whichButton) {

EditText etUserName = edittext1;

String userName = etUserName.getText().toString();

EditText etUserPass = edittext2;

String userPass = etUserPass.getText().toString();

mWebView.setHttpAuthUsernamePassword(host,name,password);

handler.proceed(userName,userPass);

}

})

.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog,int whichButton) {

handler.cancel();

}

})

.create().show();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值