我想在我的Android应用中访问受.htaccess保护的文件。下面是我只需要一个文件,但不是.htaccess受保护。我不知道在哪里可以提供我的凭据......有人可以帮我添加凭据吗?或者提出另一种收集文件的方法?在此先感谢:)
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();
}
答案 0 :(得分:0)
您需要在setRequestProperty
对象上使用HttpURLConnection
:
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();
}