“如何修复在Android Webview中询问10-20次的Recaptcha”

时间:2019-09-19 12:25:18

标签: java android-studio webview android-webview recaptcha

“我是android应用程序开发的初学者,我要构建一个android webView应用程序。我在我的应用程序中使用的登录网页包含google recaptcha(我不是机器人)验证。 当用户尝试验证时,每次打开活动时,验证码都会询问10-20次以上。 如果您在谷歌浏览器中打开相同的网站,则只询问一次,第二次则不要求重新输入密码(pick)

是否可以使我的网络视图像google chrome(自动点击)一样工作?”

“我尝试添加同步Cookie等。”

    static boolean ASWP_CAMUPLOAD  = SmartWebView.ASWP_CAMUPLOAD;
    static boolean ASWP_ONLYCAM     = SmartWebView.ASWP_ONLYCAM;
    static boolean ASWP_MULFILE    = SmartWebView.ASWP_MULFILE;
    private static String ASWV_F_TYPE   = SmartWebView.ASWV_F_TYPE;
    private String asw_cam_message;
    private ValueCallback<Uri> asw_file_message;
    private ValueCallback<Uri[]> asw_file_path;
    private final static int asw_file_req = 1;
    private final static int file_perm = 2;
    private static final String TAG = MainActivity.class.getSimpleName();

    public static final String USER_AGENT = "Mozilla/5.0 (Linux; Android 4.1.1; Galaxy Nexus Build/JRO03C) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19";

    @SuppressLint({"SetJavaScriptEnabled", "WrongViewCast"})
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vtop);


        AndroidNetworking.initialize(getApplicationContext());

        web = findViewById(R.id.web);
        progressBar = findViewById(R.id.progressBar);

        ActionBar ab = getSupportActionBar();
        assert ab != null;
        ab.setElevation(0);

        checkConnection();
        if (Build.VERSION.SDK_INT < 18)
        {
            web.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            web.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        } else {
            web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }


        WebSettings webSettings = web.getSettings();
        web.getSettings().setUserAgentString(USER_AGENT);
        web.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSettings.getDatabaseEnabled();
        web.getSettings().supportMultipleWindows();
        web.getSettings().setDomStorageEnabled(true);
        web.getSettings().setJavaScriptEnabled(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setSupportZoom(true);
        webSettings.setSaveFormData(true);
        web.getSettings().setAllowContentAccess(true);
        web.getSettings().setLoadWithOverviewMode(true);
        web.getSettings().setUseWideViewPort(true);

        CookieSyncManager.createInstance(this);
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);

        web.setWebChromeClient(new WebChromeClient(){
            }
            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                return super.onJsAlert(view, url, message, result);
            }
        });




        web.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                progressBar.setVisibility(View.VISIBLE);
                super.onPageStarted(view, url, favicon);
                CookieSyncManager.getInstance().startSync();
                web.setVisibility(View.INVISIBLE);

            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                web.loadUrl("about:blank");
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                progressBar.setVisibility(View.VISIBLE);
                view.loadUrl(url);
                return true;

            }

            @Override
            public void onPageFinished(WebView view, String url) {

                super.onPageFinished(view, url);
                web.setVisibility(View.VISIBLE);
                progressBar.setVisibility(View.GONE);
                CookieSyncManager.getInstance().sync();
            }
        });progressBar.setMax(100);


        web.setDownloadListener(new DownloadListener() {

            }
        });

        registerForContextMenu ( web);
        if (savedInstanceState == null) {
            web.post ( new Runnable () {
                @Override
                public void run() {
                    web.loadUrl ( "https://vtop.vitap.ac.in/vtop/login?logout/" );
                }
            } );
        }

    }

    //checking connection
    protected boolean isOnline() {
        ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        assert cm != null;
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
            return true;
        } else {
            return false;
        }
    }
    public void checkConnection(){
        if(!isOnline()){

            web.setVisibility(View.INVISIBLE);
            final AlertDialog.Builder builder = new AlertDialog.Builder ( VTOP.this );
            builder.setTitle ( "Connection" );
            builder.setMessage ( "Make sure you are connected to Internet or College Wi-Fi" );
            builder.setCancelable ( false);
            builder.setNegativeButton ( "Back", new DialogInterface.OnClickListener () {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                    finish();
                }
            } );

            builder.setPositiveButton ( "Reload", new DialogInterface.OnClickListener () {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    web.reload();
                    checkConnection();
                }
            } );

            AlertDialog alertDialog = builder.create ();
            alertDialog.show ();

        }
        else
        {
            Toasty.info(VTOP.this,"Just a second",Toasty.LENGTH_SHORT).show();
            web.setVisibility(View.VISIBLE);
            web.loadUrl("https://vtop.vitap.ac.in/vtop/login?logout/");

        }
    }```

"I expect the recaptcha would ask only one time when the webview opens first time"

0 个答案:

没有答案