Android Processing 302 Address

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.