• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to Handle http redirect in code

dailton

Lurker
Hi Everybody,

I'm getting the error "Too many redirects" when accessing some sites. I've been trying to handle the responde code 302 by myself but I didn't get success.

I tried to use the following code but it doesn't work. Can anybody help?

Thanks

Dailton Menezes
Brazil

protected boolean doDownload()
{

String varname =
loterias[loto_selected].toLowerCase()+"_url";

HttpURLConnection conexao =​
null;
InputStream entrada=
null;
OutputStream saida=
null;

boolean rc=false;

try
{
Proxy proxy =
use_proxy ? new Proxy( Proxy.Type.HTTP, new InetSocketAddress(proxy_addr, Integer.parseInt(proxy_port))) : null;

URL url =
new URL(getString(getResources().getIdentifier(varname, "string", "dom.loteria")));


//SecurityManager sec = System.getSecurityManager();


HttpURLConnection.setFollowRedirects(​
true);

if (use_proxy) conexao = (HttpURLConnection)url.openConnection (proxy); // (HttpURLConnection)

else
{
conexao = (HttpURLConnection)url.openConnection();

}

if (use_proxy && proxy_user.length()>=0)
conexao.setRequestProperty(
"Proxy-Authorization", "BASIC " + Base64.encodeBytes((proxy_user+":"+proxy_pass).getBytes()));


conexao.setReadTimeout(Integer.parseInt(
proxy_timeout));
conexao.setInstanceFollowRedirects(
true);

entrada = conexao.getInputStream();

saida =
new BufferedOutputStream
(
new FileOutputStream (Environment.getExternalStorageDirectory()+"/"+getString(R.string.app_name)+"/temp.zip"));

mError = String.format(getString(R.string.done_download),copyInputStream(entrada,saida));

rc =
true;
}
catch(Exception e)
{
mError = getString(R.string.error)+e.getMessage();
rc =
false;
}
finally

{
try { if (entrada!=null) entrada.close(); } catch(Exception ex) {};
try { if (saida!=null) saida.close(); } catch(Exception ex) {};
}


return rc;


}




 
Back
Top Bottom