Skip to content Skip to sidebar Skip to footer

Access .htaccess Protected File Android

I'd like to access a file that is protected by .htaccess in my Android app. Below is what I need to get just a file, but not .htaccess protected. I don't know where I can put my cr

Solution 1:

You need to use setRequestProperty on your HttpURLConnection object:

InputStream inputStream = null;
URL url = null;
try {
    url = new URL(THENEEDEDURL);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    // might not work, instead use bellow code
    // String htpasswd = Base64.encode("username:password");
    String htpasswd = Base64.encodeToString(("username:password").getBytes("UTF-8"), Base64.NO_WRAP); 
    conn.setRequestProperty("Authorization", "Basic " + htpasswd);
    conn.setReadTimeout(10000 /* milliseconds */);
    conn.setConnectTimeout(15000 /* milliseconds */);
    conn.setRequestMethod("GET");
    conn.setDoInput(true);
    // Starts the query
    conn.connect();
    inputStream = conn.getInputStream();

    InputStreamReader reader = new InputStreamReader(inputStream);
    Gson gson = new GsonBuilder().create();
    winnendeCodes = gson.fromJson(reader, WedstrijdCode[].class);
    conn.disconnect();
} catch (IOException e) {
    e.printStackTrace();
}

Post a Comment for "Access .htaccess Protected File Android"