访问.htaccess受保护的文件Android [英] Access .htaccess protected file Android

查看:147
本文介绍了访问.htaccess受保护的文件Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的Android应用中访问受.htaccess保护的文件.下面是我只需要一个文件,而没有.htaccess保护的文件.我不知道我可以在哪里放置凭证...有人可以帮我添加凭证吗?还是提出另一种收集文件的方式?在此先感谢:)

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 credentials... Can someone help me with adding credentials to this? Or propose another way to collect the file? Thanks in advance :)

        InputStream inputStream = null;
        URL url = null;
        try {
            url = new URL(THENEEDEDURL);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            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();
        }

推荐答案

您需要在HttpURLConnection对象上使用setRequestProperty:

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();
}

这篇关于访问.htaccess受保护的文件Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆