Code example for DefaultHttpClient

Methods: addResponseInterceptor, setCookieStore, setRedirectHandler

0
 
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
        ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
        client = new DefaultHttpClient(cm, params);
 
        client.setCookieStore(new CookieStore() {
            private List<Cookie> m_Cookies = null;
 
            public void addCookie(Cookie cookie) {
                for (int i = 0; i < m_Cookies.size(); i++) {
                    if (m_Cookies.get(i).getName().equals(cookie.getName())) {
                        m_Cookies.remove(i);
                        break; 
                    } 
                } 
                m_Cookies.add(cookie);
            } 
 
            public List<Cookie> getCookies() {
                if (m_Cookies == null) {
                    m_Cookies = new ArrayList<Cookie>();
                    try { 
                        readExternalCookies(this);
                    } catch (IOException ignoreEx) {
                        // e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates. 
                    } 
                } 
                return m_Cookies;
            } 
 
            public boolean clearExpired(Date date) { 
                for (int i = m_Cookies.size() - 1; i >= 0; i--) {
                    if (m_Cookies.get(i).getExpiryDate() != null && date != null && date.after(m_Cookies.get(i).getExpiryDate())) {
                        m_Cookies.remove(i);
                        break; 
                    } 
                } 
                return true;  //To change body of implemented methods use File | Settings | File Templates. 
            } 
 
            public void clear() { 
                if (m_Cookies != null)
                    m_Cookies.clear();
            } 
        }); 
        // add gzip decompressor to handle gzipped content in responses 
        // (default we *do* always send accept encoding gzip cat_name in request) 
        client.addResponseInterceptor(new HttpResponseInterceptor() {
            public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException {
                HttpEntity entity = response.getEntity();
                Header contentEncodingHeader = entity.getContentEncoding();
 
                if (contentEncodingHeader != null) {
                    HeaderElement[] codecs = contentEncodingHeader.getElements();
                    for (int i = 0; i < codecs.length; i++) {
                        if (codecs[i].getName().equalsIgnoreCase(HttpHelper.GZIP)) {
                            response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                            return; 
                        } 
                    } 
                } 
            } 
        }); 
 
        client.setRedirectHandler(new DefaultRedirectHandler() {
            private static final String REDIRECT_LOCATIONS = "http.protocol.redirect-locations";
 
            public URI getLocationURI(HttpResponse response, HttpContext context) {
                if (response == null) {
                    throw new IllegalArgumentException("HTTP response may not be null");