Tôi đang gặp sự cố khi tải xuống tệp nhị phân (tệp zip) trong ứng dụng của tôi từ internet internet. Tôi phải sử dụng xác thực truy cập cơ bản để ủy quyền cho các tệp được gửi, nhưng phản hồi của máy chủ luôn là yêu cầu HTTP/1.0 400 Xấu.Sự cố với xác thực truy cập cơ bản trong trình tải xuống tệp
String authentication = this._login+":"+this._pass;
String encoding = Base64.encodeToString(authentication.getBytes(), 0);
String fileName = "data.zip";
URL url = new URL("http://10.0.2.2/androidapp/data.zip");
HttpURLConnection ucon = (HttpURLConnection) url.openConnection();
ucon.setRequestMethod("GET");
ucon.setDoOutput(true);
ucon.setRequestProperty ("Authorization", "Basic " + encoding);
ucon.connect();
/*
* Define InputStreams to read from the URLConnection.
*/
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
/*
* Read bytes to the Buffer until there is nothing more to read(-1).
*/
ByteArrayBuffer bab = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
bab.append((byte) current);
}
bis.close();
/* Convert the Bytes read to a String. */
FileOutputStream fos = this._context.openFileOutput(fileName, this._context.MODE_WORLD_READABLE);
fos.write(bab.toByteArray());
fos.close();
Có thể do mật khẩu trong mật khẩu không?
Đúng vậy! Tôi đã giải quyết vấn đề này một thời gian trước đây. – Kamil
Câu trả lời hay, phát hiện ra một cách khó khăn. Tôi không thể tìm thấy bất cứ nơi nào trong các tài liệu về dòng mới. Bạn có biết vị trí này nằm ở đâu không/tại sao nó được thực hiện? – sgarman
Tại sao họ lại làm điều này! – Xample