4

i have a secured url , if i open in browser, a pop up comes and i authenitcate with userid/password, then a pdf file is downloaded .

I want this functionality in java. It should authenticate using proxy server/port and user details then download

URL server = new URL("http://some.site.url/download.aspx?client=xyz&docid=1001");
System.setProperty("https.proxyUser", userName);
System.setProperty("https.proxyPassword", password);

System.setProperty("https.proxyHost",proxy);
System.setProperty("https.proxyPort",port);

URLConnection connection = (URLConnection)server.openConnection();
connection.connect();
InputStream is = connection.getInputStream();

//then i read this input stream and write to a pdf file in temporary folder

It gives me connection timeout error.

Then i thought adding authentication

String authentication = "Basic " + new
sun.misc.BASE64Encoder().encode("myuserid:mypassword".getBytes());
connection.setRequestProperty("Proxy-Authorization", authentication);

Still doesnt work,

Please let me know .

surya
  • 2,581
  • 3
  • 22
  • 34
  • Just an idea: could you use `new URL("http://username:password@some.site.url/download.aspx?client=xyz&docid=1001");`? – SiKing May 20 '14 at 16:35
  • i didnt get you, in my above snippet i used a URL object , it didnt work – surya May 20 '14 at 16:38
  • I think you are allowed to include the `username:password` in the URL string. Although I have never tried this, so I am not offering this as an answer, just something for you to try. – SiKing May 20 '14 at 16:42
  • url is third party, i dont want to ask them to change their code, currently if i can download pdf from browser (a pop up comes and i give credentials, then download), i believe its possible with java code as well – surya May 20 '14 at 16:45
  • Don't ask them anything, just include the username:password in the URL string. Have a read through this https://en.wikipedia.org/wiki/URL#Syntax – SiKing May 20 '14 at 16:51
  • ohh ok, now i used http , http://username:password@some.site.url/download.aspx? -- connection time out , but then i used https, and i get a response with IOException-HTTP response code: 401 – surya May 20 '14 at 17:50
  • Try _that_ URL in your browser first. – SiKing May 20 '14 at 18:04
  • sorry i should have said this, it works fine in a browser for https (pdf is directly downloaded), but http it again asks for credential. fyi, it works in chrome /firefox with a message but i get error in IE that windows can not fine https://.... – surya May 20 '14 at 18:05

1 Answers1

1

I solved this issue. I used a customized authenticator before connecting the URL, and it authenticates and downloads the document. FYI - once connected, till next server restart, it doesn't need authentication.

URL server = new URL(url); //works for https and not for http, i needed https in  my case.
Authenticator.setDefault((new MyAuthenticator()));

URLConnection connection = (URLConnection)server.openConnection();
connection.connect();
InputStream is = connection.getInputStream();
.... //write code to fetch inputstream

Define your own authenticator as given below

public class MyAuthenticator extends Authenticator {
    final PasswordAuthentication authentication;

    public MyAuthenticator(String userName, String password) {
         authentication = new PasswordAuthentication(userName, password.toCharArray());
    }
}
user812786
  • 4,302
  • 5
  • 38
  • 50
surya
  • 2,581
  • 3
  • 22
  • 34