如何在Android应用程序中从受保护的web文件夹下载文件?
我正在寻找一些关于如何将图像文件从受密码保护的文件夹下载到我的Android应用程序的帮助。我的代码使用URLConnection和getInputStream/BufferedInputStream,但我不知道如何在其中获得用户名/密码身份验证。我看到HttpClient有UsernamePasswordCredentials——但我不知道如何使用HttpClient下载文件,所以这对我帮助不大 这是我到目前为止找到的代码,如何使用它下载文件如何在Android应用程序中从受保护的web文件夹下载文件?,android,authentication,download,Android,Authentication,Download,我正在寻找一些关于如何将图像文件从受密码保护的文件夹下载到我的Android应用程序的帮助。我的代码使用URLConnection和getInputStream/BufferedInputStream,但我不知道如何在其中获得用户名/密码身份验证。我看到HttpClient有UsernamePasswordCredentials——但我不知道如何使用HttpClient下载文件,所以这对我帮助不大 这是我到目前为止找到的代码,如何使用它下载文件 public class ClientAuthen
public class ClientAuthentication {
public static void main(String[] args) throws Exception {
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
httpclient.getCredentialsProvider().setCredentials(
new AuthScope("localhost", 443),
new UsernamePasswordCredentials("username", "password"));
HttpGet httpget = new HttpGet("https://localhost/protected");
System.out.println("executing request" + httpget.getRequestLine());
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
EntityUtils.consume(entity);
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}
}
或者,我有下载文件的代码——我如何向其中添加凭据:
谢谢
编辑:嗯,这里没有多少帮助。我找到了这个答案,我将为我的目的尝试并重新编写:好吧,这就是我提出的代码,看起来很有效。我之所以发布它,是因为我在网上的任何地方都很难找到这样的代码。我欢迎关于如何改进的建议:)
你有没有试过像
https://user:password@host.domain/page/
?是的,它给了我文件io异常。不过,这在iOS中奏效了!好问题,我开始认为这实际上是一个文件I/o问题——我可能太相信我使用的代码示例了!但是我更喜欢使用HttpClient身份验证代码,而不是内联身份验证代码,所以我想看看是否可以让它正常工作。
public void downloadHTTPC(Activity act, String imageURL, String fileName) {
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
String pathDir = act.getExternalFilesDir(null).toString() + "/" + fileName;
File file = new File(pathDir);
long startTime = System.currentTimeMillis();
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(null, -1),
new UsernamePasswordCredentials("user", "password"));
HttpGet httpget = new HttpGet(IMGURL + imageURL);
System.out.println("executing request" + httpget.getRequestLine());
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
InputStream is = entity.getContent();
BufferedInputStream bis = new BufferedInputStream(is);
/*
* Read bytes to the Buffer until there is nothing more to read(-1).
*/
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
/* Convert the Bytes read to a Stream. */
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.close();
Log.d("ImageManager", "download ready in"
+ ((System.currentTimeMillis() - startTime) / 1000)
+ " sec");
}
//EntityUtils.consume(entity);
} catch (IOException e) {
Log.d("ImageManager", "Error: " + e);
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}