Redirected downloads are needed in recent projects, so finding a lot of options is inappropriate. Because the download link is not a simple address, but it needs to be forwarded multiple times,
When downloading, use a URL to open the data stream. However, multiple tests do not open a request for a link to multiple jumps. For 302http states. Only the final address is 200, which is what we want.
So we need to parse the link of the multiple jumps.
Here I use androidhttpclient this class, Baidu a bit, this class when sdk2.3 has,androidhttpclient no public constructor, only through static method Newinstance () method to obtain the Androidhttpclient object.
Androidhttpclient has made some improvements to defaulthttpclient to make it more useful for Android projects:
1. Turn off the expiration check and the self-connection can break all time limits.
2. You can set ConnectionTimeout (connection timeout) and sotimeout (read data timeout).
3. Turn off redirection.
4. Use a session buffer for SSL Sockets.
5. If the server is supported, use GZIP compression for data that is passed on the service side and the client.
6. Cookies are not retained by default.
Androidhttpclient cannot execute in the main thread, it throws an exception. Androidhttpclient through the static method newinstance obtain the instance, the parameter is the proxy, does not need the agent to fill "". Defaulthttpclient cookies are enabled by default, androidhttpclient do not enable cookies by default, you need to add a HttpContext parameter each time you execute, and Cookiestore. Don't forget close when you're done. You cannot create a new instance.
Here's the code:
1 /**2 * @return3 * This method is used to determine the final link derived4 */5 Publicstring Getredirecturl (String testurl) {6 7Androidhttpclient client = androidhttpclient.newinstance ("Android");8HttpGet HttpGet =NewHttpGet (testurl);9 Try {TenHttpResponse hr =Client.execute (httpget); One intCode =hr.getstatusline (). Getstatuscode (); ALOG.I ("ABC", "Code:" +code); - if(Code! =HTTPSTATUS.SC_OK) { -header[] hander = Hr.getheaders ("Location"); the if(Hander! =NULL&& hander.length > 0){ -String Redicturl = hander[hander.length-1].getvalue (); - //Multiple decision redirection - returnGetredirecturl (redicturl); + } - } +}Catch(IOException e) { A e.printstacktrace (); at}finally{ - if(Client! =NULL){ - client.close (); - } - } - returnTesturl; in}
Android Processing 302 Address