0

I am using this program in JAVA to get a csv file from the specified URL, but only problem is that it requires username and password that I know, how can I modify this program to include username and password?

public class TestGetCSV {


    public static void main(String[] args) {

        try {
        URL url12  = new URL("https://wd5-impl-services1.workday.com/ccx/service/customreport2/yahoo3/ISU-YINT061-ProjectsFE/YINT061.05-getWorkersForOpenText?format=csv" );
        URLConnection urlConn = url12.openConnection();
        HttpURLConnection conn = (HttpURLConnection)urlConn;
        conn.setInstanceFollowRedirects( false );


        InputStreamReader inStream = new InputStreamReader(urlConn.getInputStream());
        BufferedReader buff= new BufferedReader(inStream);
        String content2 = buff.readLine();

        while (content2 !=null) {

            System.out.println(content2);
            content2 = buff.readLine();
        }
        }
        catch (Exception e){
            e.printStackTrace();

        }

        }


}
user207421
  • 289,834
  • 37
  • 266
  • 440
Aamir
  • 2,139
  • 1
  • 18
  • 47
  • 2
    possible duplicate of [Connecting to remote URL which requires authentication using Java](http://stackoverflow.com/questions/496651/connecting-to-remote-url-which-requires-authentication-using-java) – Jens Jan 16 '15 at 08:26

1 Answers1

1

As this is basic authentication based upon HTTP headers you can just open the URL

https://<username>:<password>@wd5-impl-services1.workday.com/ccx/service/customreport2/yahoo3/ISU-YINT061-ProjectsFE/YINT061.05-getWorkersForOpenText?format=csv

instead I believe.

However, please keep in mind that this way your credentials are supplied in plaintext.

Prior99
  • 609
  • 5
  • 13
  • Might be worth testing it before you post a potentially incorrect answer. – christopher Jan 16 '15 at 08:25
  • As I got no credentials to test it with, it is hard to test. The website seems however to be basic auth with which it should work this way. – Prior99 Jan 16 '15 at 08:27
  • So are there no other testbeds you can use? Like, the website the OP is using? – christopher Jan 16 '15 at 08:27
  • @Prior99, I used your code but it shows this error: java.net.MalformedURLException: For input string: "I6UxYtIp~jLz,#6R" – Aamir Jan 16 '15 at 08:28
  • @Zawinski Did you just paste your password here? Also, You have to escape the special characters. Please refer to this http://stackoverflow.com/questions/10786042/java-url-encoding question for this. – Prior99 Jan 16 '15 at 08:30
  • 1
    @stuXnet,no I did not. – Aamir Jan 16 '15 at 08:31
  • @Zawinski Did this fix your problem? – Prior99 Jan 16 '15 at 09:17
  • @Prior99 no i used this: String userpass = "username" + ":" + "password"; String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes()); uc.setRequestProperty ("Authorization", basicAuth); – Aamir Jan 16 '15 at 09:47