0

I'm pointing to a URL, where there is a hyperlink with down-arrow saying "download files in Zip format".When we click on that hyperlink, files are downloaded.I want to download the files contained in that link using java code.

I tried many ways, followed some of the samples mentioned below.

Download file by passing URL using java code

I'm getting below exception:

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL

But when i manually click on the URL, it opens the page where i can see the "download files in Zip" link where i can click and download the files.

java code:

 public class UrlDownload{
    public static void main(String[] args) throws Exception {

        URL oracle = new URL("https://sample.com/view/build/myReports");
        BufferedReader in = new BufferedReader(
                new InputStreamReader(oracle.openStream()));

        String inputLine;
        while ((inputLine = in.readLine()) != null)
            System.out.println(inputLine);
        in.close();
    }
}

I tried many ways, one of the simple way is mentioned above. The URL i gave here is dummy URL.

user8115347
  • 55
  • 10
  • If the URL you target is protected by .htpasswd or something similar, you need to supply the password and username. 403 is forbidden, most likely because you failed to supply a password and username – Zoe Jun 05 '17 at 16:22
  • Or protected by session cookies. Then you need to go through the login process, extract set-cookie responses, include those cookies in your final request. – Dave S Jun 05 '17 at 16:25
  • @DaveS - It is Jenkins URL. When i point to that URL, by default it takes username and password. I have given access to Jenkins so it wont ask username/password. Can you provide any example.thanks – user8115347 Jun 05 '17 at 16:27
  • 1
    "I have given access to Jenkins so it wont ask username/password" - that doesn't seem to be working since you are getting 403. – Dave S Jun 05 '17 at 16:29
  • @LunarWatcher - can u provide any sample links.I hae given acccess to that URL, so not required to give username and password.I guess it is taking my domian credentials by default. – user8115347 Jun 05 '17 at 16:29
  • 403 doesn't mean there is a password, and don't pass the domain credentials. Unless you (or the website owner) has placed a username and password authentication that isn't the problem. As DaveS said, it could be protected by session cookies, and there are probably a **lot more reasons** for a 403 – Zoe Jun 05 '17 at 16:31
  • @DaveS - Any links with examples to hit the Jenkins URL and download the file using java code would be helpful.I have not find one.APpreciate your help. – user8115347 Jun 05 '17 at 16:32
  • I have a link in that URL "download files in Zip format" . So by any means do i need to say in java code to point to that link which when clicked download's the file? – user8115347 Jun 05 '17 at 16:45

0 Answers0