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.

3
  • Is that HTTP (basic) authentication or a cookie-based custom system (forms)? Commented Jun 27, 2013 at 20:04
  • It uses HTTPS instead of HTTP basic. Commented Jun 27, 2013 at 20:10
  • HTTPS is orthogonal to the question I asked. Both of those options can be used over HTTP or HTTPS. (although they should not be used with HTTP) Commented Jun 28, 2013 at 3:09

2 Answers 2

1

Or, if you want to be safe about it:

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();}
Sign up to request clarification or add additional context in comments.

Comments

0

If the server uses BASIC authentication, you should be able to get the resource by specifying username/password in the URL: http://user:password@hostname/path/filename.ext

Browsers support this too, so you can try it in your browser quickly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.