1

I am trying to download a file from a URL. The server runs apache httpd and requires username password login/authentication first. Then when I put this URL in a browser I get the download prompt to download this zip file.

How can I do this in Java? I am learning Java and I am from a Python background. Any help is greatly appreciated.

Edit: Server runs on HTTPS auth.

4

2 回答 2

1

或者,如果您想确保安全:

HttpResponse res;
DefaultHttpClient httpclient = new DefaultHttpClient();
String authorizationString = "Basic " + Base64.encodeToString(("admin" + ":" + "").getBytes(), Base64.NO_WRAP); //this line is diffe
authorizationString.replace("\n", "");
try {
    HttpGet request = new HttpGet(URI.create(url));
    request.addHeader("Authentication",authorizationString);
    res = httpclient.execute(request);
    return new MjpegInputStream(res.getEntity().getContent());              
} catch (ClientProtocolException e) {e.printStackTrace();}
} catch (IOException e) {e.printStackTrace();}
于 2013-06-27T20:08:03.020 回答
0

如果服务器使用 BASIC 身份验证,您应该能够通过在 URL 中指定用户名/密码来获取资源:http://user:password@hostname/path/filename.ext

浏览器也支持这一点,因此您可以在浏览器中快速尝试。

于 2013-06-27T20:06:10.400 回答