简体   繁体   中英

URL Class in java

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();

        }

        }


}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM