0

I have to connect to a URL, and in that i have a link click here to download, when i click on it,it downloads a zip file which has multiple files in it. My requirement is to download that zip file using java application.I am trying to point to a URl and download the file.

I have taken the code from How to remotely login to a Jenkins server using Java?

Below is the code:

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URI;

    public class JenkinsScraper {

        public String scrape(String urlString, String username, String password) throws ClientProtocolException, IOException {
            URI uri = URI.create(urlString);
            HttpHost host = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(new AuthScope(uri.getHost(), uri.getPort()), new UsernamePasswordCredentials(username, password));
            // Create AuthCache instance
            AuthCache authCache = new BasicAuthCache();
            // Generate BASIC scheme object and add it to the local auth cache
            BasicScheme basicAuth = new BasicScheme();
            authCache.put(host, basicAuth);
            CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
            HttpGet httpGet = new HttpGet(uri);
            // Add AuthCache to the execution context
            HttpClientContext localContext = HttpClientContext.create();
            localContext.setAuthCache(authCache);

            HttpResponse response = httpClient.execute(host, httpGet, localContext);

            return EntityUtils.toString(response.getEntity());
        }

        public static void main(String args[]) throws IOException {
            JenkinsScraper obj = new JenkinsScraper();
           String result =  obj.scrape("https://info-build.gm.com/view/tools/job/MISC.DefectSummaryReport/lastSuccessfulBuild/artifact/","JZYH8B","may@2017");
            System.out.print("result :: " + result);
        }

    }

I'm facing below exception:

Exception in thread "main" java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64.<init>(I)V
    at org.apache.http.impl.auth.BasicScheme.authenticate(BasicScheme.java:168)

Had someone worked on this type of requirement where we will have a link in the Jenkins URL, using java code we need to hit the URL and download the zip file.Do i need tp use any other API to achieve this functionality.Any suggestions would be helpful.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user8115347
  • 55
  • 10
  • A `NoSuchMethodError` usually indicates that you are running the program with jars that are different from the ones you compiled it with. Please check your dependencies. – RealSkeptic Jun 08 '17 at 14:58
  • yeah, I do understand. But is that the correct API I'm following to get my requirement done? Can I able to download the zip file from that URL which has the link to download?How can java application recognizes that it has to download the zip file from that specific link present in that page?? – user8115347 Jun 08 '17 at 15:02
  • I have no idea if your method will work or not, because I don't know what page you are accessing, and how the link is implemented there. The problem is that this is code that you copied from somebody else (and didn't give a link and a credit - the rules on SiteOverflow say that you committed plagiarism) and therefore, you don't know what it does. You need to write your own code based on your own research. – RealSkeptic Jun 08 '17 at 15:10
  • @RealSkeptic- Sorry I was not knowing that. I have edited my post. I was trying to connect to my Jenkins URL and download a file from their. – user8115347 Jun 08 '17 at 15:17

0 Answers0