0

I'm trying to hit the https URL and download the file. Below is the code.

import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.io.*;
import java.net.*;

public class SampleTest {

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

        URL url;
        InputStream is = null;
        BufferedReader br;
        String line;
        // Install Authenticator
        MyAuthenticator.setPasswordAuthentication("myUsername", "myPassword");
        Authenticator.setDefault (new MyAuthenticator ());

        try {
            url = new URL("https://myURL/");
            is = url.openStream();   
            br = new BufferedReader(new InputStreamReader(is));
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (MalformedURLException mue) {
            mue.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            try {
                if (is != null) is.close();
            } catch (IOException ioe) {
                // nothing to see here
            }
        }

    }
    }

class MyAuthenticator extends Authenticator {
    private static String username = "";
    private static String password = "";

    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(MyAuthenticator.username,
                MyAuthenticator.password.toCharArray());
    }

    public static void setPasswordAuthentication(String username, String password) {
        MyAuthenticator.username = username;
        MyAuthenticator.password = password;
    }
}

Below is the generated exception:

java.io.IOException: Server returned HTTP response code: 403 for URL: 
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1876)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
    at java.net.URL.openStream(URL.java:1045)
    at SampleTest.main(SampleTest.java:20)

Please advice. I tried all possible ways to authenticate and access the URL but with no luck.Had any one faced the same issue before.

--EDITED--Tried with below code too..connected statement got printed then followed by exception.

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

        URLConnection connection = new URL("https://myURL/").openConnection();
        connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
        connection.connect();
        System.out.print("--connected---");

       BufferedReader r  = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8")));
       System.out.print("--rrr---" + r);
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = r.readLine()) != null) {
            sb.append(line);
        }
        System.out.println(sb.toString());
    }

Exception:

--connected---Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: https://myURL
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1876)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
    at FinalTest.main(FinalTest.java:20)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
user8115347
  • 55
  • 10
  • Did you check other question on SO that mention Java, HTTPS and 403 response code? https://stackoverflow.com/questions/13670692/403-forbidden-with-java-but-not-web-browser, for example. – vempo Jun 06 '17 at 16:04
  • Yes, i researched a lot..whatever i tried ended up with above exception – user8115347 Jun 06 '17 at 16:05
  • what happened if you hit that URL in browser? Obviously, your dealing with HTTPS which has SSL handshake. If SSL is a problem then there are parameters you can add to the JVM to debug SSL. – Minh Kieu Jun 06 '17 at 16:12
  • @MinhKieu - When i hit the URL in browser it open the webpage. By default it is taking my login domain credentials to login to that site.Its a jenkins URL to which i have given access, i'm trying to download a file from it. – user8115347 Jun 06 '17 at 16:17
  • Ok so in Java, you need to emulate the same behaviour. This POST may hep https://stackoverflow.com/questions/31337011/how-to-remotely-login-to-a-jenkins-server-using-java – Minh Kieu Jun 06 '17 at 16:22
  • got the exception Exception in thread "main" java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64.(I)V when implement using https://stackoverflow.com/questions/31337011/how-to-remotely-login-to-a-jenkins-server-using-java @MinhKieu – user8115347 Jun 06 '17 at 16:32
  • Let ask a new question and others may be able to chip in and help you with that. So I guess your going down the BASIC authentication route? – Minh Kieu Jun 06 '17 at 16:38
  • @MinhKieu- Yes, as u suggested i will raise a new question. – user8115347 Jun 06 '17 at 16:46

1 Answers1

0

I tried to simulate your issue and managed to reproduce the same error when authenticating via HTTPS but then I managed to resolve it by setting the System property, like below.

System.setProperty("http.agent", "Mozilla/4.76");

P.S My JDK is set to 1.8

Sam Kaz
  • 432
  • 3
  • 12