ExampleApp/src/main/java/co/infinum/povexampleapp/NetworkWithLoginExample.java
private UpdateConfigLoader createLoader(final String loginUrl, final String contentUrl, final String username, final String password) {
        return new BaseLoader() {

            private List<String> cookies = new ArrayList<>();

            private void login(String csrf) throws IOException {
                URL url = new URL(loginUrl);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                try {
                    conn.setUseCaches(false);
                    conn.setRequestMethod("POST");
                    for (String cookie : cookies) {
                        conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
                    }
                    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                    conn.setDoOutput(true);
                    conn.setDoInput(true);
                    DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
                    String params = createLoginData(csrf);
                    dos.writeBytes(params);
                    dos.flush();
                    dos.close();
                    Log.d(TAG, "Sending login request...");
                    Log.d(TAG, "with parameters: " + params);
                    int responseCode = conn.getResponseCode();
                    Log.d(TAG, "Response code: " + responseCode);
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new IOException(e);
                } finally {
                    conn.disconnect();
                }
            }

            private String createLoginData(String csrf) {
                try {
                    return "csrf_token_login=" + URLEncoder.encode(csrf, "UTF-8")
                            + "&submit_hidden=submit_hidden"
                            + "&submit=Login"
                            + "&user_name=" + URLEncoder.encode(username, "UTF-8")
                            + "&user_password=" + URLEncoder.encode(password, "UTF-8");
                } catch (UnsupportedEncodingException impossible) {
                    return "";
                }
            }


            private String getPage(String pageUrl) throws IOException {
                URL url = new URL(pageUrl);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                try {
                    conn.setUseCaches(false);
                    for (String cookie : cookies) {
                        conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
                    }
                    Log.d(TAG, "Sending get request to url: " + pageUrl);
                    int responseCode = conn.getResponseCode();
                    Log.d(TAG, "Response code: " + responseCode);
                    StringBuilder sb = new StringBuilder();
                    String line = null;
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(
                                    conn.getInputStream(),
                                    "UTF-8"
                            )
                    );
                    while ((line = reader.readLine()) != null) {
                        sb.append(line);
                    }
                    reader.close();
                    cookies = conn.getHeaderFields().get("Set-Cookie");
                    return sb.toString();
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new IOException(e);
                } finally {
                    conn.disconnect();
                }
            }

            @Override
            public String load() throws IOException, InterruptedException {
                CookieHandler.setDefault(new CookieManager());
                String loginPage = getPage(loginUrl);
                ifTaskIsCancelledThrowInterrupt();
                Document document = Jsoup.parse(loginPage);
                document.charset(Charset.forName("UTF-8"));
                Elements selector = document.select("input[name=csrf_token_login]");
                String csrf = "";
                if (selector.size() > 0) {
                    csrf = selector.get(0).val();
                }
                Log.d(TAG, "CSRF: " + csrf);
                ifTaskIsCancelledThrowInterrupt();
                login(csrf);
                ifTaskIsCancelledThrowInterrupt();
                String content = getPage(contentUrl);
                ifTaskIsCancelledThrowInterrupt(); // if cancelled here no need to read stream at all
                return content;
            }

            @Override
            public void validate() throws LoaderValidationException {
                if (username == null || password == null) {
                    throw new LoaderValidationException("Credentials not set.");
                }
                if (contentUrl == null || loginUrl == null) {
                    throw new LoaderValidationException("Url not set.");
                }
            }
        };
    }
/**
     * Creates custom loader. Loader loads update configuration from private pastebin file using provided username and password to log in
     * into Pastebin account where private file is stored.
     *
     * @param loginUrl   URL for getting and posting to login page.
     * @param contentUrl URL for fetching content.
     * @param username   Account username.
     * @param password   Account password.
     * @return New instance of loader for loading from private Pastebin file.
     */
Creates custom loader. Loader loads update configuration from private pastebin file using provided username and password to log in
into Pastebin account where private file is stored.

@param loginUrl   URL for getting and posting to login page.
@param contentUrl URL for fetching content.
@param username   Account username.
@param password   Account password.
@return New instance of loader for loading from private Pastebin file.
Creates custom loader . Loader loads update configuration from private pastebin file using provided username and password to log in into Pastebin account where private file is stored . @param loginUrl URL for getting and posting to login page . @param contentUrl URL for fetching content . @param username Account username . @param password Account password . @return New instance of loader for loading from private Pastebin file .
private UpdateConfigLoader createLoader(final String loginUrl, final String contentUrl, final String username, final String password) {
        return new BaseLoader() {
            private List<String> cookies = new ArrayList<>();
            private void login(String csrf) throws IOException {
                URL url = new URL(loginUrl);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                try {
                    conn.setUseCaches(false);
                    conn.setRequestMethod("POST");
                    for (String cookie : cookies) {
                        conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
                    }
                    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                    conn.setDoOutput(true);
                    conn.setDoInput(true);
                    DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
                    String params = createLoginData(csrf);
                    dos.writeBytes(params);
                    dos.flush();
                    dos.close();
                    Log.d(TAG, "Sending login request...");
                    Log.d(TAG, "with parameters: " + params);
                    int responseCode = conn.getResponseCode();
                    Log.d(TAG, "Response code: " + responseCode);
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new IOException(e);
                } finally {
                    conn.disconnect();
                }
            }
            private String createLoginData(String csrf) {
                try {
                    return "csrf_token_login=" + URLEncoder.encode(csrf, "UTF-8")
                            + "&submit_hidden=submit_hidden"
                            + "&submit=Login"
                            + "&user_name=" + URLEncoder.encode(username, "UTF-8")
                            + "&user_password=" + URLEncoder.encode(password, "UTF-8");
                } catch (UnsupportedEncodingException impossible) {
                    return "";
                }
            }
            private String getPage(String pageUrl) throws IOException {
                URL url = new URL(pageUrl);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                try {
                    conn.setUseCaches(false);
                    for (String cookie : cookies) {
                        conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
                    }
                    Log.d(TAG, "Sending get request to url: " + pageUrl);
                    int responseCode = conn.getResponseCode();
                    Log.d(TAG, "Response code: " + responseCode);
                    StringBuilder sb = new StringBuilder();
                    String line = null;
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(
                                    conn.getInputStream(),
                                    "UTF-8"
                            )
                    );
                    while ((line = reader.readLine()) != null) {
                        sb.append(line);
                    }
                    reader.close();
                    cookies = conn.getHeaderFields().get("Set-Cookie");
                    return sb.toString();
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new IOException(e);
                } finally {
                    conn.disconnect();
                }
            }
            @Override
            public String load() throws IOException, InterruptedException {
                CookieHandler.setDefault(new CookieManager());
                String loginPage = getPage(loginUrl);
                ifTaskIsCancelledThrowInterrupt();
                Document document = Jsoup.parse(loginPage);
                document.charset(Charset.forName("UTF-8"));
                Elements selector = document.select("input[name=csrf_token_login]");
                String csrf = "";
                if (selector.size() > 0) {
                    csrf = selector.get(0).val();
                }
                Log.d(TAG, "CSRF: " + csrf);
                ifTaskIsCancelledThrowInterrupt();
                login(csrf);
                ifTaskIsCancelledThrowInterrupt();
                String content = getPage(contentUrl);
                ifTaskIsCancelledThrowInterrupt(); 
                return content;
            }
            @Override
            public void validate() throws LoaderValidationException {
                if (username == null || password == null) {
                    throw new LoaderValidationException("Credentials not set.");
                }
                if (contentUrl == null || loginUrl == null) {
                    throw new LoaderValidationException("Url not set.");
                }
            }
        };
    }
private UpdateConfigLoader createLoader ( final String loginUrl , final String contentUrl , final String username , final String password ) { return new BaseLoader ( ) { private List < String > cookies = new ArrayList < > ( ) ; private void login ( String csrf ) throws IOException { URL url = new URL ( loginUrl ) ; HttpURLConnection conn = ( HttpURLConnection ) url . openConnection ( ) ; try { conn . setUseCaches ( false ) ; conn . setRequestMethod ( "POST" ) ; for ( String cookie : cookies ) { conn . addRequestProperty ( "Cookie" , cookie . split ( ";" , 1 ) [ 0 ] ) ; } conn . setRequestProperty ( "Content-Type" , "application/x-www-form-urlencoded" ) ; conn . setDoOutput ( true ) ; conn . setDoInput ( true ) ; DataOutputStream dos = new DataOutputStream ( conn . getOutputStream ( ) ) ; String params = createLoginData ( csrf ) ; dos . writeBytes ( params ) ; dos . flush ( ) ; dos . close ( ) ; Log . d ( TAG , "Sending login request..." ) ; Log . d ( TAG , "with parameters: " + params ) ; int responseCode = conn . getResponseCode ( ) ; Log . d ( TAG , "Response code: " + responseCode ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; throw new IOException ( e ) ; } finally { conn . disconnect ( ) ; } } private String createLoginData ( String csrf ) { try { return "csrf_token_login=" + URLEncoder . encode ( csrf , "UTF-8" ) + "&submit_hidden=submit_hidden" + "&submit=Login" + "&user_name=" + URLEncoder . encode ( username , "UTF-8" ) + "&user_password=" + URLEncoder . encode ( password , "UTF-8" ) ; } catch ( UnsupportedEncodingException impossible ) { return "" ; } } private String getPage ( String pageUrl ) throws IOException { URL url = new URL ( pageUrl ) ; HttpURLConnection conn = ( HttpURLConnection ) url . openConnection ( ) ; try { conn . setUseCaches ( false ) ; for ( String cookie : cookies ) { conn . addRequestProperty ( "Cookie" , cookie . split ( ";" , 1 ) [ 0 ] ) ; } Log . d ( TAG , "Sending get request to url: " + pageUrl ) ; int responseCode = conn . getResponseCode ( ) ; Log . d ( TAG , "Response code: " + responseCode ) ; StringBuilder sb = new StringBuilder ( ) ; String line = null ; BufferedReader reader = new BufferedReader ( new InputStreamReader ( conn . getInputStream ( ) , "UTF-8" ) ) ; while ( ( line = reader . readLine ( ) ) != null ) { sb . append ( line ) ; } reader . close ( ) ; cookies = conn . getHeaderFields ( ) . get ( "Set-Cookie" ) ; return sb . toString ( ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; throw new IOException ( e ) ; } finally { conn . disconnect ( ) ; } } @ Override public String load ( ) throws IOException , InterruptedException { CookieHandler . setDefault ( new CookieManager ( ) ) ; String loginPage = getPage ( loginUrl ) ; ifTaskIsCancelledThrowInterrupt ( ) ; Document document = Jsoup . parse ( loginPage ) ; document . charset ( Charset . forName ( "UTF-8" ) ) ; Elements selector = document . select ( "input[name=csrf_token_login]" ) ; String csrf = "" ; if ( selector . size ( ) > 0 ) { csrf = selector . get ( 0 ) . val ( ) ; } Log . d ( TAG , "CSRF: " + csrf ) ; ifTaskIsCancelledThrowInterrupt ( ) ; login ( csrf ) ; ifTaskIsCancelledThrowInterrupt ( ) ; String content = getPage ( contentUrl ) ; ifTaskIsCancelledThrowInterrupt ( ) ; return content ; } @ Override public void validate ( ) throws LoaderValidationException { if ( username == null || password == null ) { throw new LoaderValidationException ( "Credentials not set." ) ; } if ( contentUrl == null || loginUrl == null ) { throw new LoaderValidationException ( "Url not set." ) ; } } } ; }
Creates custom loader.
Creates custom loader .
// if cancelled here no need to read stream at all