跟踪最后重定向的url android

时间:2014-03-10 08:59:28

标签: android url redirect webview

我将网页加载到我的WebView中。这是一个登录页面。当用户输入其凭据时,页面会重定向多次。我想跟踪上次重定向的网址。

关于如何做到这一点的任何想法。

这是我的代码:

myWebView.setWebViewClient(new WebViewClient()             {

            @Override
            public void onPageFinished(WebView view, String url) 
            {
                // TODO Auto-generated method stub
                super.onPageFinished(view, url);
                if(progressBar.isShowing())
                {
                    progressBar.dismiss();
                }
                String absoluteUrl = view.getUrl();
                absoluteUrl = Uri.decode(absoluteUrl);
                int absoulteCount = absoluteUrl.length();

                String redirectedUrl = endpointHost+"Authorize/index"+deviceId;
                int redirectedCount = redirectedUrl.length();


                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Details.this);
                alertDialogBuilder.setNegativeButton("Ok", new DialogInterface.OnClickListener() 
                {
                    public void onClick(DialogInterface dialog,int id) 
                    {
                        // if this button is clicked, just close
                        // the dialog box and do nothing
                        dialog.cancel();
                    }
                });
                AlertDialog alert = alertDialogBuilder.create();
                alert.setMessage(absoluteUrl);
                alert.show();


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

                HttpClient httpClient = new DefaultHttpClient();
                URL myUrl;
                URLConnection connection;
                try
                {
                    myUrl = new URL(url);
                    connection = myUrl.openConnection();
                    connection.setConnectTimeout(3000);
                    connection.connect();

                    int size = connection.getContentLength();

                }
                catch (Exception e) {}

                String htmlContent = "";
                HttpPost httpGet = new HttpPost(url);
                HttpResponse response;
                HttpContext httpContext = new BasicHttpContext();
                try
                {
                    response = httpClient.execute(httpGet);


                    if(response.getStatusLine().getStatusCode() == 200)
                    {
                        HttpEntity entity = response.getEntity();
                        if (entity != null)
                        {
                            InputStream inputStream = entity.getContent();
                            htmlContent = convertToString(inputStream);
                        }



                    }

                }
                catch (Exception e) {}





                return true;

            }

0 个答案:

没有答案