如果网络需要实际连接到能够下载认证? [英] If internet requires authentication for actually connecting to be able to download?

查看:135
本文介绍了如果网络需要实际连接到能够下载认证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序需要使用互联网连接来下载从链接某些文件。我有一个使用代理服务器,需要身份验证的网络连接。不知怎的,当我尝试连接到互联网,它从来没有要求该认证,因此无法下载文件。

My application requires using internet connection to download some file from a link. I have a internet connection which uses proxy and needs authentication. Somehow when I try to connect to internet it never asks for this authentication hence is unable to download file.

我想问的是有没有什么办法,如果用户的互联网连接时需要验证,从网上下载文件,使情况如果需要我的应用程序可以让用户打开一个正常的检测设备的互联网浏览器,并提供所需的认证。

What I want to ask is that is there any way to detect if the internet connection of the user requires authentication to download files from internet, so that in-case if it is required my application can ask the user to open a normal internet browser of device and provide the wanted authentication.

推荐答案

这是一个非常真实的情况下,当你的应用程序试图在使用代理为让用户的机构或公司来访问互联网组织的身体像会带来麻烦连接到互联网。

This is a very genuine case and will cause trouble when your application tries to access internet in an organizational body like in an institute or company which uses proxy for letting users to connect to internet.

由于我没有收到关于这个问题,我要告诉遵循的程序由我检查这个,因为有时你的应用程序将无法访问互联网可以使死人任何回答。

As I have not received any answer on this question I am going to tell the procedure followed by me for checking this, as sometimes for your application to be unable to access internet can make it dead.

起初我检查网络连接使用可通过设备的简单的状态检查设备上的这个

At first I check if internet connection is available on the device by simple state checking of the device using this

 final ConnectivityManager conMgr =  (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
                final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();

                if (activeNetwork != null && activeNetwork.getState() == NetworkInfo.State.CONNECTED) {

                                //Do what ever you wish to do

                                } else {
                                      // Display message internet connection not available
                                      }

以上code给出了互联网连接是否可用与否的信息,但它不能告诉我们,如果互联网连接时需要验证(因此导致没有得到下载或损坏的文件或零大小的文件在下载中创建文件位置),因此检查上述条件后,我试图访问一个链接,下载然后用这个逻辑。

The above code gives information on whether internet connection is available or not but it fails to tell if the internet connection requires authentication (hence leading to nothing getting downloaded or corrupted file or zero size file to be created in the download file location), hence after checking the above condition I try to access a link to download and then use this logic.

我已经提供了从互联网到一个小的.zip文件一个链接,我的应用程序尝试下载当按钮来访问互联网是pressed(如用户点击更新数据库的应用)。我已经使用(在这个问题上给出的信息下载并在Android中提取Zip文件),然后我提取一个解压位置下载的文件。

I have provided a link to a small .zip file from internet which my application tries to download when a button to access internet is pressed (like user clicking on "update the database" of application). I have used the information given in this question ( Download and Extract Zip File in Android ), and then I extract the downloaded file in a unzipping location.

这之后,我已经创建了一个检查选项(即已经意识到.zip文件内的文件存在的确切名称),检查是否该文件由我,而解压​​提到的解压位置被创建。

After this I have created a check option (being already aware of exact name of the file that exists inside the .zip file), which checks whether this file has been created in the unzipping location mentioned by me while unzipping.

如果该文件不存在,你只是给他们一个选项,打开浏览器,并提供身份验证,然后再返回到应用程序像这样的:

If the file does not exist, you just give them an option to open browser and provide the authentication and then return back to your application like such:

 private void unableToConnectToInternetDialog() {
                // TODO Auto-generated method stub
                String Message ="CHECK IF:" +
                        "your internet connection uses proxy, if yes please follow these steps:" +
                        "\n 1 Open default Internet Browser" +
                        "\n 2 Open any webpage requiring authentication" +
                        "\n 3 Provide the authentication" +
                        "\n 4 Resume this application";

                // TODO Auto-generated method stub
                AlertDialog.Builder builderInternet = new AlertDialog.Builder(AppPage1.this);
                builderInternet.setMessage(Message)
                        .setTitle("Unable to connect to Internet")
                       .setCancelable(false)
                       .setPositiveButton("Open Browser", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {
                               // Opening mobile's default browser to set the authentication
                               String url = "http://www.google.com";
                               Intent i = new Intent(Intent.ACTION_VIEW);
                               i.setData(Uri.parse(url));
                               startActivity(i);
                           }
                       })
                       .setNegativeButton("Skip Update", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {
                               // Do the needful

                           }
                       });
                builderInternet.create().show();
            }
        }

这篇关于如果网络需要实际连接到能够下载认证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆