Reputation: 103
My application accesses internet to download a file from the given link. I am using my institute's internet connection which uses proxy settings.
For my application to be able to download the file from internet I need to open my internet browser, give the username and password for connection and keep the browser running, only then is my application able to download the complete file, if I don't do these things my application runs normally and creates a file in the desired place but shows the file size to be zero, which is most likely because it is unable to connect to the internet directly.
How can I make my application show a dialog to me asking for the username and password to connect to internet, if it is unable to.
Thanks :-)
Upvotes: 1
Views: 3183
Reputation: 103
I developed this logic, it takes lot's of work but it helps me in detecting if there is some other problem like proxy or other as the normal internet connectivity manager is not able to detect if there is a requirement for authentication or not.
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
}
The above code gives information on whether internet connection is available or not but it deosn't tell if the internet connection requires authentication, hence after checking the above condition I try to access a link to download and then use this logic.
The logic used by me is mentioned in this stackoverflow query ( If internet requires authentication for actually connecting to be able to download? ) .... hope the information helps the needy.
Upvotes: 0
Reputation: 9971
When you create the connection object through which the file transfer will be effected, you must specify that you are using whatever form of authentication credentials that the proxy is expecting. I don't know what you'd do in Java for Android, but in .NET I do this:
LogHandler.Log(LogHandler.LogType.Debug, "Configure proxy credentials");
// Proxy address and port
ec2Config.ProxyHost = ConfigurationManager.AppSettings["LocalProxyHost"];
ec2Config.ProxyPort = Convert.ToInt32(ConfigurationManager.AppSettings["LocalProxyPort"]);
// Create a dummy webrequest so we can set proxy credentials (to stop the proxy spitting 407s at us)
HttpWebRequest dummyRequest = (HttpWebRequest)WebRequest.Create(ec2Config.ServiceURL);
dummyRequest.Proxy.Credentials = CredentialCache.DefaultCredentials;
#pragma warning disable 168
HttpWebResponse myHttpWebResponse = (HttpWebResponse)dummyRequest.GetResponse();
#pragma warning restore 168
Upvotes: 0